rheon.corr.nearest

rheon.corr.nearest(a: Any, w: Any = None, *, tol: float = 1e-08, max_iter: int = 100) → tuple[Any, float, int][source]

Find the nearest correlation matrix to a matrix, by Higham’s alternating projections.

Use it to repair an “almost-correlation” matrix, one that is not positive semidefinite, such as one assembled from estimates of each pair made apart, while changing it as little as possible. To trade bias for variance instead, use shrink().

Parameters

a

A square, finite matrix, symmetrized on the way in; or a Gram from gram(), or a row of closed_groups(). A Gram is repaired without its intercept column, the constant a spec’s fit_intercept adds, since a constant has no correlation with anything. It is read as rheon.gram.correlation() reads it, and rheon.gram.vif() leaves the intercept out the same way. A feature the stream found constant has no correlation either, and is refused: pass the Gram without it, as rheon.gram.subset() returns it.

w

The diagonal of a weight matrix W, as a vector of one weight per column (for a Gram, per column other than the intercept), each finite and above 0. Default None, every weight 1.

tol

The stopping rule: the largest of the relative changes in X, in Y, and between them falls below it (Notes). Default 1e-8. tol sets the work, since convergence is linear.

max_iter

The most iterations, at least 1. Default 100. It stops a run that has not met tol, and returns the last iterate with no warning: iters < max_iter in the result shows that the run met tol, and iters == max_iter that it may have stopped at the limit. At 0 the input would come back unprojected with a distance of 0, which would say it was already a correlation matrix.

Returns

(X, dist, iters): the repaired matrix, the weighted Frobenius distance ||W^1/2 (A - X) W^1/2||_F, and the iterations taken. For a Gram, X has one row and one column for each of the Gram’s columns other than the intercept, in their order. X has an exactly unit diagonal and is positive semidefinite to tol: its smallest eigenvalue can be a small negative number of that order (Notes).

Raises

ValueError

An entry of a that is not finite; a Gram column, other than the intercept, with no variance; a not square; w without one weight per column, or with a weight that is not finite and above 0; max_iter below 1; tol not above 0.

Examples

almost = np.array([[1.0, 0.9, 0.7], [0.9, 1.0, 0.3], [0.7, 0.3, 1.0]])   # not PSD
fixed, dist, iters = rp.corr.nearest(almost)
print(fixed.round(3), round(dist, 4), iters)
[[1.    0.895 0.697]
 [0.895 1.    0.303]
 [0.697 0.303 1.   ]] 0.0097 17

See also

  • shrink(), the other repair, toward a structured target.

Notes

The method is Higham’s (2002) Algorithm 3.3: alternating projections with Dykstra’s correction,

dS_0 = 0,  Y_0 = A
R_k  = Y_{k-1} - dS_{k-1}          # Dykstra's correction
X_k  = P_S(R_k)                    # onto the positive semidefinite cone
dS_k = X_k - R_k
Y_k  = P_U(X_k)                    # onto the unit diagonal

with P_U setting the diagonal to 1 and, for the diagonal weight W, P_S(A) = W^-1/2 (W^1/2 A W^1/2)_+ W^-1/2, where (.)_+ clips the eigenvalues at 0. It stops on Higham’s test (4.1): the largest of the relative changes in X, in Y, and between them, below tol, each in the infinity norm, the largest absolute row sum.

Convergence is linear: the change fell by a factor of 2.4 an iteration on Higham’s 3 x 3 example and 2.7 on his 4 x 4, from tol=1e-4 to 1e-10.

The result is positive semidefinite only to tol. The iteration converges to the boundary of the cone, so the smallest eigenvalue can be a small negative number of the order of tol. Tighten tol if that matters: clipping it here would break the unit diagonal again.