polars_online.corr.nearest¶
- polars_online.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
aA square, finite matrix, symmetrized on the way in; or a Gram from
gram(), or a row ofclosed_groups(). A Gram is repaired without its intercept column, the constant a spec’sfit_interceptadds, since a constant has no correlation with anything. It is read aspolars_online.gram.correlation()reads it, andpolars_online.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, aspolars_online.gram.subset()returns it.wThe 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. DefaultNone, every weight 1.tolThe stopping rule: the largest of the relative changes in
X, inY, and between them falls below it (Notes). Default 1e-8.tolsets the work, since convergence is linear.max_iterThe 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_iterin the result shows that the run mettol, anditers == max_iterthat 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,Xhas one row and one column for each of the Gram’s columns other than the intercept, in their order.Xhas an exactly unit diagonal and is positive semidefinite totol: its smallest eigenvalue can be a small negative number of that order (Notes).Raises
ValueErrorAn entry of
athat is not finite; a Gram column, other than the intercept, with no variance;anot square;wwithout one weight per column, or with a weight that is not finite and above 0;max_iterbelow 1.
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 = po.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 diagonalwith
P_Usetting the diagonal to 1 and, for the diagonal weightW,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 inX, inY, and between them, belowtol, 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-4to1e-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 oftol. Tightentolif that matters: clipping it here would break the unit diagonal again.