Randomised, and the guarantee that changes kind

Counting what cannot be looked at

The trace is n additions and one of the most expensive quantities in the subject to estimate, because the matrices whose trace is wanted are never stored. Hutchinson's estimator is unbiased with one line of algebra — and its variance depends on which random vector is used, by a factor that is a property of the matrix, and on a diagonal matrix one choice is exact from the first probe and the other is not.

Worth reading first: A bound that holds with probability · The dimension does not appear.

The trace is the cheapest quantity in linear algebra. Add up the diagonal: n additions, no multiplications, exact.

It is also one of the most expensive to estimate, because the matrices whose trace is wanted are never the ones a caller has.

  • tr(A⁻¹) needs n solves, and appears in every uncertainty quantification that wants the variance of an estimator.
  • tr(log A) = log det A is the log-determinant, which is the normalising constant of a Gaussian and therefore in the likelihood of every Gaussian process.
  • tr(f(A)) for other f — the number of eigenvalues in an interval, the Estrada index of a graph, a partition function.
  • And tr(H(λ)), the effective number of parameters a regularisation leaves, which is the denominator this site has already measured in generalised cross-validation.

In every one of those, a product with the operator is available and the entries are not.

The identity

Let z have mean zero and identity covariance — E[z] = 0, E[zzᵀ] = I. Then

E[zᵀAz]  =  E[tr(zᵀAz)]  =  E[tr(A zzᵀ)]  =  tr(A · E[zzᵀ])  =  tr(A)

for every such z, and every A. No bias, no assumption on the matrix, no asymptotics. Take s independent probes, average, done.

What it has is variance, and the variance is the whole subject.

The running estimate of a diagonal 8×8 matrix's trace, from the two probe distributionsTwo curves of the running average against the number of probes. The ±1 probe returns 18.5 — the exact trace — from its first draw and never moves, because zᵀAz is Σ a_ii z_i² and every z_i² is 1. The normal probe starts at 14.259 and is still 0.099 away after 60 of them.111213141511114.714918.429722.144625.8595probes takenrunning estimate of the tracenormal±1one probe, no errorthe exact trace19±1 variance, this matrix0±1 variance, rotated10normal variance97the same spectrum in a general basiscosts the ±1 probe its whole advantage
Fig. 1 The extreme case at its smallest: eight diagonal entries, one ±1 probe, and the exact answer. The flat line is flat at every size.

Two distributions, and one is never worse

For z with independent ±1 entries — a Rademacher vector — the variance of one probe is

Var  =  2 · Σ_{i≠j} a_ij²  =  2(‖A‖_F² − Σ a_ii²)

and for standard normals it is 2‖A‖_F². Both are exact for every n, not asymptotic.

The ratio is 1 − Σa_ii²/‖A‖_F², a number between 0 and 1 that is a property of the matrix. So the Rademacher probe is never worse and usually better, and the reason is one line: `zᵀAz = Σ_i a_ii z_i²

  • Σ_{i≠j} a_ij z_i z_j, and for a ±1 vector z_i² = 1`, so the diagonal contributes a constant rather than a random variable. The diagonal is estimated exactly and the off-diagonal is what is being sampled.
160 single-probe estimates of one 40×40 trace, from the two standard probe distributionsTwo clouds of points, each one estimate of the trace from one random vector. Both are centred on the true value of 171.14 — the estimator has no bias at all — and the dashed bands are the standard deviation each distribution is known in closed form to have: 59.14 for the ±1 probe and 72.01 for the normal one. Measured over these draws they come out 56.68 and 68.57.027548110813544119.306194.613269.919345.226420.532drawzᵀAz from one probenormal±1two routes to one spreadthe trace171±1 spread, predicted59±1 spread, measured57normal ÷ ±11.2no bias in either cloudand one of them is narrower for a reason
Fig. 2 160 single-probe estimates of one 40×40 trace, from each distribution, with the standard deviation each is known in closed form to have drawn across the cloud as a pair of bands. Both are centred on the true value; one cloud is 18% narrower, and the 18% is a property of this matrix. Drag the size.

assertTheVarianceIsTheFormula checks both formulas against the empirical variance over 4,000 probes, and checks the ratio against the quantity computed from the matrix at 10⁻¹² — because the ratio has no sampling in it at all and is an algebraic identity between two closed forms.

160 single-probe estimates of one 120×120 trace, from the two standard probe distributionsTwo clouds of points, each one estimate of the trace from one random vector. Both are centred on the true value of 644.26 — the estimator has no bias at all — and the dashed bands are the standard deviation each distribution is known in closed form to have: 198.6 for the ±1 probe and 217.1 for the normal one. Measured over these draws they come out 227.1 and 205.3.0275481108135243534.65826.31117.951409.61701.25drawzᵀAz from one probenormal±1two routes to one spreadthe trace644±1 spread, predicted199±1 spread, measured227normal ÷ ±11.1no bias in either cloudand one of them is narrower for a reason
Fig. 3 The two clouds at n = 120. Both spreads grow with the matrix and their ratio does not: the advantage is a property of the shape rather than of the size.

The extreme case

Push that observation to its end. On a diagonal matrix there is no off-diagonal to sample: zᵀAz = Σ a_ii z_i² = Σ a_ii = tr(A) for every ±1 vector.

One probe returns the exact trace. The variance formula says the same thing — ‖A‖_F² = Σa_ii² for a diagonal matrix, so it returns exactly zero — and the two routes are asserted against each other.

The running estimate of a diagonal 40×40 matrix's trace, from the two probe distributionsTwo curves of the running average against the number of probes. The ±1 probe returns 98.5 — the exact trace — from its first draw and never moves, because zᵀAz is Σ a_ii z_i² and every z_i² is 1. The normal probe starts at 177.66 and is still 0.029 away after 60 of them.1112131415193111.365129.731148.096166.461probes takenrunning estimate of the tracenormal±1one probe, no errorthe exact trace99±1 variance, this matrix0±1 variance, rotated57normal variance545the same spectrum in a general basiscosts the ±1 probe its whole advantage
Fig. 4 The running estimate for a diagonal 40×40. The ±1 probe returns 98.5 — the exact trace — from its first draw and never moves. The normal probe starts 80% away and is still 2.9% away after sixty. Drag the size; the flat line is flat at every one.

The Gaussian probe on the same matrix has variance 2Σa_ii² = 545 and needs thousands of samples for two digits. The estimator that is exact on the easiest class is the one nobody’s notation distinguishes, and the difference between the two is one line in an implementation.

There is a control beside it, and it matters: the same spectrum in a general basis — QΛQᵀ with the same eigenvalues — has a Rademacher variance of 56.8. The advantage is a property of the basis, not of the spectrum, and a reader who took away “±1 probes are free” would have taken away something false.

The zero that was not zero

A small thing worth recording, because it is this site’s own subject appearing inside its own library.

Evaluating 2(‖A‖_F² − Σa_ii²) on a diagonal matrix subtracts two quantities that are equal, and returned 1.14·10⁻¹³ where the answer is zero. The formula is written as a difference and the two terms cancel completely; what is left is the rounding of the two sums.

rademacherVariance now sums the off-diagonal entries directly — which is what the formula is counting — and returns exactly 0. It is cancellation takes the answer in a routine whose whole purpose is to compute a variance, and it is the kind of thing that would have been invisible had the assertion been written with a tolerance instead of as an equality.

The running estimate of a diagonal 200×200 matrix's trace, from the two probe distributionsTwo curves of the running average against the number of probes. The ±1 probe returns 498.5 — the exact trace — from its first draw and never moves, because zᵀAz is Σ a_ii z_i² and every z_i² is 1. The normal probe starts at 620.39 and is still 0.0086 away after 60 of them.11121314151489517.279545.557573.836602.114probes takenrunning estimate of the tracenormal±1one probe, no errorthe exact trace499±1 variance, this matrix0±1 variance, rotated297normal variance2785the same spectrum in a general basiscosts the ±1 probe its whole advantage
Fig. 5 Two hundred diagonal entries. The normal probe’s relative error settles rather than improving, because its variance grows with n exactly as the trace does.

The rate, and the one thing that beats it

The relative error falls like s^{-1/2} and nothing about a better random variable changes the exponent. assertTheRateIsMinusAHalf fits it over five budgets and 40 seeds and gets −0.500.

That is the reason trace estimation has a reputation for being expensive: two more digits costs ten thousand times the work, and a Monte Carlo rate is a Monte Carlo rate whatever the variance constant is.

Hutch++ changes the exponent. Spend a third of the budget on a sketch, orthonormalise it, compute tr(QᵀAQ) exactly at another third, and Hutchinson the deflated remainder (I − QQᵀ)A(I − QQᵀ) with what is left. The deflated matrix has a far smaller Frobenius norm whenever the spectrum decays, and the variance is proportional to that norm squared.

Two trace estimators against their budget, on a 80×80 matrix whose spectrum decays at 0.85Two curves of relative error against the number of products with A, both axes logarithmic, as medians over 32 seeds. Hutchinson's fitted exponent is -0.54 and Hutch++'s is -2.55. The deflation changes the exponent rather than the constant, which is what makes it worth two thirds of the budget.10¹10²10⁻⁴10⁻³10⁻²10⁻¹1products with Arelative errorHutchinsonHutch++measured at equal costfitted rate, Hutchinson-0.54fitted rate, Hutch++-2.5error at 96, Hutchinson0.024error at 96, Hutch++4.4·10⁻⁴both axes count products with Aso the sketch is paid for in the picture
Fig. 6 Relative error against the number of products with A — not against the sample count, because two thirds of Hutch++'s budget is the sketch and the exact head. On a spectrum decaying at 0.85 the fitted exponents are −0.54 and −2.55. Drag the decay; the last stop has none.

The comparison is at equal products with A, and the distinction is not pedantry. Compared at equal sample counts, Hutch++ would be an algorithm competing against itself with three times the information, and the figure would be a picture of arithmetic.

Two trace estimators against their budget, on a 80×80 matrix whose spectrum decays at 0.7Two curves of relative error against the number of products with A, both axes logarithmic, as medians over 32 seeds. Hutchinson's fitted exponent is -0.31 and Hutch++'s is -5.07. The deflation changes the exponent rather than the constant, which is what makes it worth two thirds of the budget.10¹10²10⁻⁷10⁻⁶10⁻⁵10⁻⁴10⁻³10⁻²10⁻¹1products with Arelative errorHutchinsonHutch++measured at equal costfitted rate, Hutchinson-0.31fitted rate, Hutch++-5.1error at 96, Hutchinson0.051error at 96, Hutch++2.1·10⁻⁶both axes count products with Aso the sketch is paid for in the picture
Fig. 7 A faster decay, where the deflation has more to remove: a fitted exponent of −5.1 against Hutchinson’s −0.31, on the same budget of products.

And the flat spectrum, for the third time

Two trace estimators against their budget, on a 80×80 matrix whose spectrum does not decayTwo curves of relative error against the number of products with A, both axes logarithmic, as medians over 32 seeds. Hutchinson's fitted exponent is -0.32 and Hutch++'s is -0.67. With no decay there is no dominant subspace to project out, so the two exponents are the same and the third of the budget Hutch++ spends on a sketch is simply lost.10¹10²10⁻²10⁻¹1products with Arelative errorHutchinsonHutch++measured at equal costfitted rate, Hutchinson-0.32fitted rate, Hutch++-0.67error at 96, Hutchinson0.055error at 96, Hutch++0.072both axes count products with Aso the sketch is paid for in the picture
Fig. 8 The same comparison on a spectrum with no decay at all — half the eigenvalues at +1 and the rest at −1, so the trace is small beside the Frobenius norm and there is something to estimate. The two fitted exponents are the same, and Hutch++ is behind at every budget by the third of the budget it spent looking for a subspace that is not there.

Randomisation does not create structure made this argument for the randomised SVD, and a bound that holds with probability made it for the HMT bound. This is the third method to reach it, and it is asserted here rather than referred to, because a claim that holds for a low-rank approximation is not a claim about a trace estimator.

The reason is measured too: at k = 8 out of n = 120 the leading directions hold k/n of the Frobenius norm and no more, which is what a flat spectrum means, so the deflation removes 6.7% of the variance for 67% of the budget.

One detail is worth keeping rather than smoothing away: on the decaying spectrum Hutch++ is also behind at the smallest budget. Two thirds of a budget of twelve is a sketch of four columns, which deflates almost nothing while removing eight probes. Hutch++ has a break-even and it is not at zero, and the assertion says so in both directions.

Two trace estimators against their budget, on a 80×80 matrix whose spectrum decays at 0.95Two curves of relative error against the number of products with A, both axes logarithmic, as medians over 32 seeds. Hutchinson's fitted exponent is -0.50 and Hutch++'s is -1.03. The deflation changes the exponent rather than the constant, which is what makes it worth two thirds of the budget.10¹10²10⁻³10⁻²10⁻¹1products with Arelative errorHutchinsonHutch++measured at equal costfitted rate, Hutchinson-0.5fitted rate, Hutch++-1error at 96, Hutchinson0.0086error at 96, Hutch++0.0055both axes count products with Aso the sketch is paid for in the picture
Fig. 9 And a slow one, where the two exponents are −0.50 and −1.03. The deflation still changes the exponent and the margin is small, which is what the boundary between the two regimes looks like.

What this does to a parameter-choice rule

Choosing without knowing measured generalised cross-validation against three other rules for picking a regularisation parameter, and the step that stops mattering found its denominator to be a trace over a Krylov subspace, bounded by k + 1 whatever n is.

GCV’s whole objective is

G(λ)  =  ‖(I − H(λ))b‖²  /  (tr(I − H(λ)))²

and the denominator is exactly the kind of trace this essay is about: H(λ) = A(AᵀA + λ²I)⁻¹Aᵀ is never formed, a product with it is a solve, and n solves to get the diagonal defeats the purpose of choosing λ cheaply.

So GCV as it is actually implemented on a large problem is GCV with an estimated denominator, and the estimate is Hutchinson’s. That puts a random quantity inside the objective being minimised, which has two consequences worth naming.

The objective is noisy, so its minimiser is noisy. A rule that selects λ by minimising a function computed to two digits selects a λ known to about two digits, and the site’s measurement that GCV’s λ barely moves — a factor of 2.15 while its denominator moved by 27 — is the reason that is survivable here and would not be everywhere.

And the noise is not independent across λ. Using the same probe vectors at every λ makes the estimated objective a smooth function of λ, wrong by a consistent amount; drawing fresh probes at every λ makes it unbiased and jagged, and a minimiser of a jagged function is a minimiser of the jaggedness. Every implementation reuses the probes, which is the right choice and is a decision about correlation rather than about variance.

That is worth sitting with. The parameter-choice rule this site measured against an oracle is, at scale, a rule whose objective contains a Monte Carlo estimate held deliberately correlated so that its error cancels in the argmin. None of that appears in the formula.

The L-curve, and where four rules put λThe norm of the solution against the norm of its residual, on logarithmic axes, as λ sweeps eight decades. The curve has a corner: to the left of it the noise is being amplified and to the right the signal is being thrown away. Four points are marked — the three rules that use only the data, and the oracle, which requires the exact answer and is not a method.10⁻²10⁻¹110¹10²10³10⁴‖Ax − b‖‖x‖the oraclediscrepancyL-curvegeneralisedscored against a truth none hasoracle, relative error0.11discrepancy principle, as a multiple1.1L-curve corner, as a multiple2.3generalised cross-validation, as a multiple1the oracle needs the exact answer and is not a methodit is the reference the others are scored on
Fig. 10 The four rules measured against an oracle. On a problem large enough to need it, one of these four has a randomised quantity in its objective and the other three do not.

Where the flat spectrum is not the pathological case

It is worth resisting the reading that a flat spectrum is a contrived difficulty. For the traces this essay opened with, it is often the typical one.

tr(A⁻¹) for a discretised elliptic operator has a spectrum that decays like k^{-2/d} — slowly, and more slowly the higher the dimension. The influence matrix H(λ) whose trace is the effective degrees of freedom has eigenvalues σ_i²/(σ_i² + λ²), which are near 1 for the retained directions and near 0 for the rest: a step, not a decay, and a step has a dominant subspace exactly the size of the step, which is what makes it the one case where deflation is spectacular.

So whether to deflate is a question about the operator, and it is usually answerable in advance from what the operator is, which is the useful form of the result.

What a rank-10 approximation can achieve, by spectrumA semi-logarithmic plot of singular value against index for three spectra — geometric decay, algebraic decay, and flat — with the rank-ten approximation error marked on each.1112131415110⁻³10⁻²10⁻¹1index jσⱼσ11geometric, 0.85ʲalgebraic, j^−0.60flatmeasured, and equal to σ₁₁rank-10 error, geometric0.2rank-10 error, algebraic0.24rank-10 error, flat160×60, spectrum chosen rather than the entriesthe matrix decides, not the method
Fig. 11 The site’s standing picture of what a decaying spectrum is and is not. Everything about whether a deflation is worth two thirds of a budget is a question about which of these curves the operator has.
160 single-probe estimates of one 10×10 trace, from the two standard probe distributionsTwo clouds of points, each one estimate of the trace from one random vector. Both are centred on the true value of 29.29 — the estimator has no bias at all — and the dashed bands are the standard deviation each distribution is known in closed form to have: 11.27 for the ±1 probe and 17.61 for the normal one. Measured over these draws they come out 10.64 and 20.47.0275481108135-129.514160.028290.5423121.056151.57drawzᵀAz from one probenormal±1two routes to one spreadthe trace29±1 spread, predicted11±1 spread, measured11normal ÷ ±11.6no bias in either cloudand one of them is narrower for a reason
Fig. 12 Ten dimensions, where a single probe is a tenth of the whole matrix and both clouds are wide. The two closed forms are exact at every n, which is why the agreement here is a sampling question and nothing else.
The condition number a sketched preconditioner leaves, against the condition number it was givenTwo curves against κ(A), both axes logarithmic. The matrix's own condition number climbs the diagonal from 100 to 10¹⁰; κ(AR⁻¹), where R comes from a QR of a 4n-row sketch, is 2.2284 at every one of them — the same number to ten digits, not a similar one. The reason is two lines of algebra: with G = SU the preconditioned singular values are those of (GᵀG)⁻¹, which has no spectrum of A in it at all.10²10⁴10⁶10⁸10¹⁰110²10⁴10⁶10⁸10¹⁰condition number of the matrixcondition number seen by the iterationκ(A), unchangedκ(AR⁻¹)a bound with no κ(A) in itκ(AR⁻¹), every κ(A)2.2κ(SU), the other route2.2κ(A), across the sweep10⁸κ(AR⁻¹), across the sweep1the sketch never sees the spectrumand the spectrum cancels out of the answer
Fig. 13 The other use of random vectors in this field, and the one where the randomness does not reach the answer. A sketch used as a preconditioner moves the iteration count; a probe used as an estimate moves the estimate.

Why the trace and not the diagonal

A question worth answering directly, because the diagonal is what the trace is made of and estimating it entrywise is a different and harder problem.

e_iᵀA e_i gives a_ii exactly, in one product with A, so n products give the whole diagonal and the trace with it. The estimator in this essay uses far fewer than n and returns only the sum. What it is buying is precisely the s ≪ n, and what it gives up is any statement about the individual entries.

The estimator for the diagonal — the same probes, but forming z ⊙ Az and averaging entrywise rather than summing — is the natural extension, and its variance per entry is set by the off-diagonal mass in that entry’s row rather than by the whole matrix’s. So a matrix with a heavy row is a matrix whose diagonal is estimated badly in exactly one place, and there is no averaging over n to rescue it.

That asymmetry is why the trace is the quantity with a literature and the diagonal is a special request. The sum of n things each estimated to relative accuracy ε is not estimated to ε; the sum estimated directly is, because the cancellation the probes suffer is over the off-diagonal and the diagonal contributes to every probe identically.

Where the variance actually comes from

One more decomposition, because it makes both findings in this essay obvious rather than surprising.

Write zᵀAz = Σ_i a_ii z_i² + Σ_{i≠j} a_ij z_i z_j. The first term is what the probe measures exactly in the ±1 case and noisily in the normal case; the second is the sampling.

  • For ±1, z_i² = 1, so the first term is the constant tr(A) and the whole variance is the second: 2Σ_{i≠j} a_ij².
  • For normal, E[z_i²] = 1 but Var[z_i²] = 2, so the first term contributes 2Σ a_ii² on top.

Everything else follows. A diagonal matrix has no second term, so the ±1 variance is zero and the normal one is 2Σa_ii². A matrix with a large diagonal relative to its Frobenius norm has a large advantage; a hollow matrix has none at all, because there is no first term for either probe to get right.

That last case is worth naming: for a matrix with zero diagonal the two distributions have identical variance, and the ±1 probe’s whole advantage disappears. An adjacency matrix is such a matrix, and counting triangles in a graph is tr(A³)/6, so the one application where the trace estimator is best known is the one where the choice of probe does not matter.

What the budget is measured in

The horizontal axis of the rate figures is products with A, and it is worth saying why that is the honest unit rather than a fussy one.

For a dense matrix a product costs n² operations and every method here costs the same per product, so products and flops agree up to a constant. For the matrices this estimator exists for they do not. A sparse operator’s product costs its number of nonzeros; a matrix-free operator’s product is a PDE solve, an FFT, or a simulation, and can cost anything at all.

So a comparison in flops would be a comparison that assumed a matrix, and the whole premise is that there is not one. A comparison in probes would be worse: it would let Hutch++ take three times as much information as Hutchinson and then report the difference as an improvement.

Counting products is also what makes the break-even visible. Hutch++'s sketch and exact head cost two thirds of its budget before a single probe is taken, so at a budget of twelve it is behind, and the figure says so rather than starting the axis past the crossing.

What is worth carrying

An unbiased estimator is exactly right on average and wrong every time, and the whole of the engineering is in the variance rather than in the bias.

The choice of probe distribution is a decision with a closed form behind it. ±1 is never worse than normal, better by a factor that is a property of the matrix, and on a diagonal matrix it is exact.

The rate is Monte Carlo and no random variable improves the exponent. Deflation does, and only where there is a dominant subspace to deflate, and it has a break-even.

And a flat spectrum defeats it, for the third time on this site. What randomisation buys is fewer passes over the data, and on a matrix with no structure to find it buys nothing.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A flat tag is an object no other essay names yet.

CancellationGeneralised cross validationHutchinson's estimatorInfluence matrixMatrix-freeProbabilistic boundsRandom projectionSpectral decayTrace estimation