Notebooks · lib/randla/trace
Trace estimation from matrix-vector products by Hutchinson and Hutch++
Both estimators see the matrix only through apply(X), which returns A X
for an n-by-s block X: a def or a lambda, so A can be an operator never
stored (a Hessian, a matrix function applied by Lanczos, an inverse applied
by a solver).
hutchinson_fn(apply, n, m, seed, stream): (1/m) sum of w^T A w over m Rademacher vectors w. Unbiased, with variance 2 (|A|_F^2 - sum a_ii^2) / m, so the error falls like 1/sqrt(m): 100 times the products for one more digit.hutchpp_fn(apply, n, m, seed, stream): Hutch++ (Meyer, Musco, Musco and Woodruff, SOSA 2021). A third of the budget finds the dominant range Q of A (from A S), the trace of Q^T A Q is taken exactly, and Hutchinson runs on the rest, (I - Q Q^T) A (I - Q Q^T), whose Frobenius norm is small when A's spectrum decays. For PSD A the error falls like 1/m.
When not to use them: for a matrix you hold, trace(A) is exact and costs
n reads. When the spectrum is flat (A near a multiple of I), Hutch++ spends
a third of its products on a range that captures nothing, and plain
Hutchinson with all m products does better.
Used with from randla.trace import ....
Look inside this program
- Atlas
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.