mctrace
is used internally by fevcov
and
bccorr
, but has been made public since it might be useful for
other tasks as well.
For any matrix \(A\), the trace equals the sum of the diagonal elements,
or the sum of the eigenvalues. However, if the size of the matrix is very
large, we may not have a matrix representation, so the diagonal is not
immediately available. In that case we can use the formula \(tr(A) =
E(x^t A x)\) where \(x\) is a random vector with zero
expectation and \(Var(x) = I\). We estimate the expecation with sample
means. mctrace
draws \(x\) in \(\{-1,1\}^N\), and
evaluates mat
on these vectors.
If mat
is a function, it must be able to take a matrix of column
vectors as input. Since \(x^t A x = (Ax,x)\) is evaluated,
where \((\cdot,\cdot)\) is the Euclidean inner product, the function
mat
can perform this inner product itself. In that case the function
should have an attribute attr(mat,'IP') <- TRUE
to signal this.
If mat
is a list of factors, the matrix for which to estimate the
trace, is the projection matrix which projects out the factors. I.e. how
many dimensions are left when the factors have been projected out. Thus, it
is possible to estimate the degrees of freedom in an OLS where factors are
projected out.
The tolerance tol
is a relative tolerance. The iteration terminates
when the normalized standard deviation of the sample mean (s.d. divided by
absolute value of the current sample mean) goes below tol
. Specify a
negative tol
to use the absolute standard deviation. The tolerance
can also change during the iterations; you can specify
tol=function(curest) {...}
and return a tolerance based on the
current estimate of the trace (i.e. the current sample mean).