# Don’t Use Gaussians in Stochastic Trace Estimation

Suppose we are interested in estimating the trace of an matrix that can be only accessed through matrix–vector products . The classical method for this purpose is the GirardHutchinson estimator

where the vectors are independent, identically distributed (iid) random vectors satisfying the isotropy condition

Examples of vectors satisfying this condition include

Stochastic trace estimation has a number of applications: log-determinant computations in machine learningpartition function calculations in statistical physicsgeneralized cross validation for smoothing splines, and triangle counting in large networks. Several improvements to the basic Girard–Hutchinson estimator have been developed recently. I am partial to XTrace, an improved trace estimator that I developed with my collaborators.

This post is addressed at the question:

Which distribution should be used for the test vectors for stochastic trace estimation?

Since the Girard–Hutchinson estimator is unbiased , the variance of is equal to the mean-square error. Thus, the lowest variance trace estimate is the most accurate. In my previous post on trace estimation, I discussed formulas for the variance of the Girard–Hutchinson estimator with different choices of test vectors. In that post, I stated the formulas for different choices of test vectors (Gaussian, random signs, sphere) and showed how those formulas could be proven.

In this post, I will take the opportunity to editorialize on which distribution to pick. The thesis of this post is as follows:

The sphere distribution is essentially always preferable to the Gaussian distribution for trace estimation.

To explain why, let’s focus on the case when is real and symmetric.1The same principles hold in the general case, but the variance formulas are more delicate to state. See my previous post for the formulas. Let be the eigenvalues of and define the eigenvalue mean

Then the variance of the Girard–Hutchinson estimator with Gaussian vectors is

For vectors drawn from the sphere, we have

The sphere distribution improves on the Gaussian distribution in two ways. First, the variance of is smaller than by a factor of . This improvement is quite minor. Second, and more importantly, is proportional to the sum of ‘s squared eigenvalues whereas is proportional to the sum of ‘s squared eigenvalues after having been shifted to be mean-zero!

The difference between Gaussian and sphere test vectors can be large. To see this, consider a matrix with eigenvalues uniformly distributed between and with a (Haar orthgonal) random matrix of eigenvectors. For simplicity, since the variance of all Girard–Hutchinson estimates is proportional to , we take . Below show the variance of Girard–Hutchinson estimator for different distributions for the test vector. We see that the sphere distribution leads to a trace estimate which has a variance 300× smaller than the Gaussian distribution. For this example, the sphere and random sign distributions are similar.

## Which Distribution Should You Use: Signs vs. Sphere

The main point of this post is to argue against using the Gaussian distribution. But which distribution should you use: Random signs? The sphere distribution? The answer, for most applications, is one of those two, but exactly which depends on the properties of the matrix .

The variance of the Girard–Hutchinson estimator with the random signs estimator is

Thus, depends on the size of the off-diagonal entries of ; does not depend on the diagonal of at all! For matrices with small off-diagonal entries (such as diagonally dominant matrices), the random signs distribution is often the best.

However, for other problems, the sphere distribution is preferable to random signs. The sphere distribution is rotation-invariant, so is independent of the eigenvectors of the (symmetric) matrix , depending only on ‘s eigenvalues. By contrast, the variance of the Girard–Hutchinson estimator with the random signs distribution can significantly depend on the eigenvectors of the matrix . For a given set of eigenvalues and the worst-case choice of eigenvectors, will always be smaller than . In fact, is the minimum variance distribution for Girard–Hutchinson trace estimation for a matrix with fixed eigenvalues and worst-case eigenvectors; see this section of my previous post for details.

In my experience, random signs and the sphere distribution are both perfectly adequate for trace estimation and either is a sensible default if you’re developing software. The Gaussian distribution on the other hand… don’t use it unless you have a good reason to.

# Don’t Solve the Normal Equations

The (ordinary) linear least squares problem is as follows: given an matrix and a vector of length , find the vector such that is as close to as possible, when measured using the two-norm . That is, we seek to

(1)

From this equation, the name “least squares” is self-explanatory: we seek which minimizes the sum of the squared discrepancies between the entries of and .

The least squares problem is ubiquitous in science, engineering, mathematics, and statistics. If we think of each row of as an input and its corresponding entry of as an output, then the solution to the least squares model gives the coefficients of a linear model for the input–output relationship. Given a new previously unseen input , our model predicts the output is approximately . The vector consists of coefficients for this linear model. The least squares solution satisfies the property that the average squared difference between the output and the prediction is as small as it could possibly be for all choices of coefficient vectors .

How do we solve the least squares problem? A classical solution approach, ubiquitous in textbooks, is to solve a system of linear equations known as the normal equations. The normal equations associated with the least squares problem (1) are given by

(2)

This system of equations always has a solution. If has full column-rank, then is invertible and the unique least squares solution to (1) is given by . We assume that has full column-rankQ for the rest of this discussion. To solve the normal equations in software, we compute and and solve (2) using a linear solver like MATLAB’s “\”.1Even better, we could us a Cholesky decomposition since the matrix is positive definite. (As is generally true in matrix computations, it is almost never a good idea to explicitly form the inverse of the matrix , or indeed any matrix.) We also can solve the normal equations using an iterative method like (preconditioned) conjugate gradient.

The purpose of the article is to advocate against the use of the normal equations for solving the least squares problems, at least in most cases. So what’s wrong with the normal equations? The problem is not that the normal equations aren’t mathematically correct. Instead, the problem is that the normal equations often lead to poor accuracy for the least squares solution using computer arithmetic.

Most of the time when using computers, we store real numbers as floating point numbers.2One can represent rational numbers on a computer as fractions of integers and operations can be done exactly. However, this is prone to gross inefficiencies as the number of digits in the rational numbers can grow to be very large, making the storage and time to solve linear algebra problems with rationals dramatically more expensive. For these reasons, the vast majority of numerical computations use floating point numbers which store only a finite number of digits for any given real number. In this model, except for extremely rare circumstances, rounding errors during arithmetic operations are a fact of life. At a coarse level, the right model to have in your head is that real numbers on a computer are stored in scientific notation with only 16 decimal digits after the decimal point.3This is a simplification in multiple ways. First, computers store numbers in binary and thus, rather than storing 16 decimal digits after the decimal point, they store 52 binary digits. This amounts to roughly 16 decimal digits. Secondly, there are different formats for storing real numbers as floating point on a computer with different amounts of stored digits. The widely used IEEE double precision format has about 16 decimal digits of accuracy; the IEEE single precision format has roughly 8. When two numbers are added, subtracted, multiplied, and divided, the answer is computed and then rounded to 16 decimal digits; any extra digits of information are thrown away. Thus, the result of our arithmetic on a computer is the true answer to the arithmetic problem plus a small rounding error. These rounding errors are small individually, but solving an even modestly sized linear algebra problem requires thousands of such operations. Making sure many small errors don’t pile up into a big error is part of the subtle art of numerical computation.

To make a gross simplification, if one solves a system of linear equations on a computer using a well-designed piece of software, one obtains an approximate solution which is, after accounting for the accumulation of rounding errors, close to . But just how close the computed solution and the true solution are depends on how “nice” the matrix is. The “niceness” of a matrix is quantified by a quantity known as the condition number of , which we denote .4In fact, there are multiple definitions of the condition number depending on the norm which one uses the measure the sizes of vectors. Since we use the 2-norm, the appropriate 2-norm condition number is the ratio of the largest and smallest singular values of . As a rough rule of thumb, the relative error between and is roughly bounded as

(3)

The “ corresponds to the fact we have roughly 16 decimal digits of accuracy in double precision floating point arithmetic. Thus, if the condition number of is roughly , then we should expect around digits of accuracy in our computed solution.

The accuracy of the least squares problem is governed by its own condition number . We would hope that we can solve the least squares problem with an accuracy like the rule-of-thumb error bound (3) we had for linear systems of equations, namely a bound like . But this is not the kind of accuracy we get for the least squares problem when we solve it using the normal equations. Instead, we get accuracy like

(4)

By solving the normal equations we effectively square the condition number! Perhaps this is not surprising as the normal equations also more-or-less square the matrix by computing . This squared condition number drastically effects the accuracy of the computed solution. If the condition number of is , then the normal equations give us absolute nonsense for ; we expect to get no digits of the answer correct. Contrast this to above, where we were able to get correct digits in the solution to despite the condition number of being times larger than !

All of this would be just a sad fact of life for the least squares problem if the normal equations and their poor accuracy properties were the best we could do for the least squares problem. But we can do better! One can solve linear least squares problems by computing a so-called QR factorization of the matrix .5In MATLAB, the least squares problem can be solved with QR factorization by calling “A\b”. Without going into details, the upshot is that the least squares solution by a well-designed6One way of computing the QR factorization is by Gram–Schmidt orthogonalization, but the accuracy properties of this are poor too. A gold-standard way of computing the QR factorization by means of Householder reflectors, which has excellent accuracy properties. QR factorization requires a similar amount of time to solving the normal equations and has dramatically improved accuracy properties, achieving the desirable rule-of-thumb behavior7More precisely, the rule of thumb is like . So even if we solve the least squares problem with QR factorization, we still get a squared condition number in our error bound, but this condition number squared is multiplied by the residual , which is small if the least squares fit is good. The least squares solution is usually only interesting when the residual is small, thus justifying dropping it in the rule of thumb.

(5)

I have not described how the QR factorization is accurately computed nor how to use the QR factorization to solve least squares problems nor even what the QR factorization is. All of these topics are explained excellently by the standard textbooks in this area, as well as by publicly available resources like Wikipedia. There’s much more that can be said about the many benefits of solving the least squares problem with the QR factorization,8E.g., it can work for sparse matrices while the normal equations often do not, it has superior accuracy to Gaussian elimination with partial pivoting even for solving linear systems, the “” matrix in the QR factorization can be represented implicitly as a product of easy-to-compute-with Householder reflectors which is much more efficient when, etc. but in the interest of brevity let me just say this: TL;DR when presented in the wild with a least squares problem, the solution method one should default to is one based on a well-implemented QR factorization, not solving the normal equations.

Suppose for whatever reason we don’t have a high quality QR factorization algorithm at our disposal. Must we then resort to the normal equations? Even in this case, there is a way we can reduce the problem of solving a least squares problems to a linear system of equations without squaring the condition number! (For those interested, to do this, we recognize the normal equations as a Schur complement of a somewhat larger system of linear equations and then solve that. See Eq. (7) in this post for more discussion of this approach.)

The title of this post Don’t Solve the Normal Equations is deliberately overstated. There are times when solving the normal equations is appropriate. If is well-conditioned with a small condition number, squaring the condition number might not be that bad. If the matrix is too large to store in memory, one might want to solve the least squares problem using the normal equations and the conjugate gradient method.

However, the dramatically reduced accuracy of solving the normal equations should disqualify the approach from being the de-facto way of solving least squares problems. Unless you have good reason to think otherwise, when you see , solve a different way.

# The Better Way to Convert an SVD into a Symmetric Eigenvalue Problem

A singular value decomposition of an matrix is a factorization of the form , where and are square, orthogonal matrices and is a diagonal matrix with th entry .1Everything carries over essentially unchanged for complex-valued matrices with and being unitary matrices and every being replaced by for the Hermitian transpose. The diagonal entries of are referred to as the singular values of and are conventionally ordered . The columns of the matrices and are referred to as the right- and left- singular vectors of and satisfy the relations and .

One can obtain the singular values and right and left singular vectors of from the eigenvalues and eigenvectors of and . This follows from the calculations and . In other words, the nonzero singular values of are the square roots of the nonzero eigenvalues of and . If one merely solves one of these problems, computing along with or , one can obtain the other matrix or by computing or . (These formulas are valid for invertible square matrices , but similar formulas hold for singular or rectangular to compute the singular vectors with nonzero singular values.)

This approach is often unundesirable for several reasons. Here are a few I’m aware of:

1. Accuracy: Roughly speaking, in double-precision arithmetic, accurate stable numerical methods can resolve differences on the order of 16 orders of magnitude. This means an accurately computed SVD of can resolve the roughly 16 orders of magnitude of decaying singular values, with singular values smaller than that difficult to compute accurately. By computing , we square all of our singular values, so resolving 16 orders of magnitude of the eigenvalues of means we only resolve 8 orders of magnitude of the singular values of .2Relatedly, the two-norm condition number of is twice that of . The dynamic range of our numerical computations has been cut in half!
2. Loss of orthogonality: While and are valid formulas in exact arithmetic, they fair poorly when implemented numerically. Specifically, the numerically computed values and may not be orthogonal matrices with, for example, not even close to the identity matrix. One can, of course, orthogonalize the computed or , but this doesn’t fix the underlying problem that or have not been computed accurately.
3. Loss of structure: If possesses additional structure (e.g. sparsity), this structure may be lost or reduced by computing the product .
4. Nonlinearity: Even if we’re not actually computing the SVD numerically but doing analysis with pencil and paper, finding the SVD of from has the disadvantage of performing a nonlinear transformation on . This prevents us from utilizing additive perturbation theorems for sums of symmetric matrices in our analysis.3For instance, one cannot prove Weyl’s perturbation theorem for singular values by considering and applying Weyl’s perturbation theorem for symmetric eigenvalues.

There are times where these problems are insignificant and this approach is sensible: we shall return to this point in a bit. However, these problems should disqualify this approach from being the de facto way we reduce SVD computation to a symmetric eigenvalue problem. This is especially true since we have a better way.

The better way is by constructing the so-called Hermitian dilation4As Stewart and Sun detail in Section 4 of Chapter 1 of their monograph Matrix Perturbation Theory, the connections between the Hermitian dilation and the SVD go back to the discovery of the SVD itself, as it is used in Jordan’s construction of the SVD in 1874. (The SVD was also independently discovered by Beltrami the year previous.) Stewart and Sun refer to this matrix as the Jordan-Wiedlant matrix associated with , as they attribute the widespread use of the matrix today to the work of Wiedlant. We shall stick to the term Hermitian dilation to refer to this matrix. of , which is defined to be the matrix

(1)

One can show that the nonzero eigenvalues of are precisely plus-or-minus the singular values of . More specifically, we have

(2)

All of the remaining eigenvalues of not of this form are zero.5This follows by noting and thus for account for all the nonzero eigenvalues of . Thus, the singular value decomposition of is entirely encoded in the eigenvalue decomposition of .

This approach of using the Hermitian dilation to compute the SVD of fixes all the issues identified with the “” approach. We are able to accurately resolve a full 16 orders of magnitude of singular values. The computed singular vectors are accurate and numerically orthogonal provided we use an accurate method for the symmetric eigenvalue problem. The Hermitian dilation preserves important structural characteristics in like sparsity. For purposes of theoretical analysis, the mapping is linear.6The linearity of the Hermitian dilation gives direct extensions of most results about the symmetric eigenvalues to singular values; see Exercise 22.

Often one can work with the Hermitian dilation only implicitly: the matrix need not actually be stored in memory with all its extra zeros. The programmer designs and implements an algorithm with in mind, but deals with the matrix directly for their computations. In a pinch, however, forming directly in software and utilizing symmetric eigenvalue routines directly is often not too much less efficient than a dedicated SVD routine and can cut down on programmer effort significantly.

As with all things in life, there’s no free lunch here. There are a couple of downsides to the Hermitian dilation approach. First, is, except for the trivial case , an indefinite symmetric matrix. By constast, and are positive semidefinite, which can be helpful in some contexts.7This is relevant if, say, we want to find the small singular values by inverse iteration. Positive definite linear systems are easier to solve by either direct or iterative methods. Further, if (respectively, ), then (respectively, ) is tiny compared to , so it might be considerably cheaper to compute an eigenvalue decomposition of (or ) than .

Despite the somewhat salacious title of this article, the and Hermitian dilation approaches both have their role, and the purpose of this article is not to say the approach should be thrown in the dustbin. However, in my experience, I frequently hear the approach stated as the definitive way of converting an SVD into an eigenvalue problem, with the Hermitian dilation approach not even mentioned. This, in my opinion, is backwards. For accuracy reasons alone, the Hermitian dilation should be the go-to tool for turning SVDs into symmetric eigenvalue problems, with the approach only used when the problem is known to have singular values which don’t span many orders of magnitude or is tall and skinny and the computational cost savings of the approach are vital.