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_covmatrices 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
fcstThe forecast
F: ak x kmatrix, or a Gram or a closed group’s row, read aspolars_online.gram.correlation()reads it, without the intercept (the module’s docstring says why).realThe 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 atfcst == realand above 0 elsewhere."z_mse": the squared Fisher-zerror in units of its own sampling variance, which needsn."minvar": Engle and Colacito’s minimum-variance loss, the realized variance of the portfolio the forecast would have held.muFor
"minvar", the expected returns that set the portfolio. DefaultNone, a vector of ones.nFor
"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
nanwhere the loss is undefined: a forecast with no inverse, a determinant ofF^-1 Rthat is not above 0, or a zero denominator.Raises
ValueErrorA
kindthat is none of the three; matrices of different shapes;"z_mse"withoutn; 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 overFatF = R, which is what makes it a proper scoring rule for a covariance forecast.