Introducing the matrix trace
The trace is a quantity that only applies to square matrices, such as the covariance matrix often encountered in ML. It is denoted as tr(A) for a square matrix, A, and is calculated as the sum of the diagonal elements in a square matrix. Let’s take a look:
- In the following code snippet, we are creating a 3x3 matrix, A, and using the
diag()
function to extract the diagonal elements and sum them up to obtain the trace of the matrix. Note that we first create a DataFrame consisting of three columns, each having three elements, and then convert it into a matrix format to store inA
:A = as.matrix(data.frame("c1"=c(1,2,3),"c2"=c(2,5,2),"c3"=c(-1,8,3))) >>> A c1 c2 c3 [1,] 1 2 -1 [2,] 2 5 8 [3,] 3 2 3 >>> diag(A) [1] 1 5 3 >>> sum(diag(A)) [1] 9
- Since there is no built-in...