polars_online.corr.loss

polars_online.corr.loss(fcst: Any, real: Any, kind: str = 'qlike', *, mu: Any = None, n: int | None = None) → float[source]

Score a forecast correlation matrix against the realized one.

Use it to compare forecasts of a correlation or covariance matrix, such as two specs’ ew_cov matrices against the matrix of the period that followed: the lower the loss, the better the forecast. Three losses are offered, each a proper scoring rule or a scaled error.

Parameters

fcst

The forecast F: a k x k matrix, or a Gram or a closed group’s row, read as polars_online.gram.correlation() reads it, without the intercept (the module’s docstring says why).

real

The realized matrix R, in the same form and of the same shape.

kind

"qlike", the default: the Gaussian quasi-likelihood loss, shifted by its forecast-free constant so that it is 0 at fcst == real and above 0 elsewhere. "z_mse": the squared Fisher-z error in units of its own sampling variance, which needs n. "minvar": Engle and Colacito’s minimum-variance loss, the realized variance of the portfolio the forecast would have held.

mu

For "minvar", the expected returns that set the portfolio. Default None, a vector of ones.

n

For "z_mse", the sample size behind the correlations.

Returns

A float, by kind:

qlike   tr(F^-1 R) - log det(F^-1 R) - k
z_mse   sum_{i<j} (z_ij(F) - z_ij(R))**2 * (n - 3)
minvar  w' R w,    w = F^-1 mu / (mu' F^-1 mu)

It is nan where the loss is undefined: a forecast with no inverse, a determinant of F^-1 R that is not above 0, or a zero denominator.

Raises

ValueError

A kind that is none of the three; matrices of different shapes; "z_mse" without n; an array that is not square; a Gram column other than the intercept with no variance.

Examples

real = np.array([[1.0, 0.5], [0.5, 1.0]])
good = np.array([[1.0, 0.4], [0.4, 1.0]])
flat = np.eye(2)
print(round(po.corr.loss(good, real), 4), round(po.corr.loss(flat, real), 4))
0.0181 0.2877

See also

  • to_z(), the transform "z_mse" reads.

Notes

"minvar" is smallest over F at F = R, which is what makes it a proper scoring rule for a covariance forecast.