polars_online.corr.epps_invert

polars_online.corr.epps_invert(gram_or_row: Any, *, L: int) → Any[source]

Compute the correlation over L rows from lagged co-moments, undoing the Epps attenuation.

Use it on a correlation measured over fine intervals, which is too low because two series do not move at the same instants (the Epps effect). Toth and Kertesz’s equation 12 undoes it by summing the lagged cross-covariances over a coarser interval of L rows.

Parameters

gram_or_row

A Gram from gram(), or a closed group’s row, with lags and lag_comoments holding every lag 1 .. L-1. Build it with po.spec.ew_cov(..., lags=list(range(1, L))).

L

The coarser interval, in rows, at least 1. L = 1 gives the plain correlation.

Returns

The k x k correlation matrix at scale L, over the input’s columns other than an intercept, with nan where a column has no variance.

Raises

ValueError

L below 1, or a lag 1 .. L-1 missing from the input, which the error names.

Examples

This code reads trades from Example data:

quotes = trades.filter(pl.col("price").is_null()).with_columns(
    ret=pl.col("mid").diff().over("symbol")
)
pair = quotes.pivot(on="symbol", index="ts", values="ret").fill_null(0.0)
spec = po.spec.ew_cov("cov", features=["AAA", "BBB"], half_life=float("inf"), lags=[1, 2])
bank = po.ModelBank([spec])
bank.fit_predict(pair)
g = bank.gram("cov")[0]
print(round(float(po.corr.epps_invert(g, L=1)[0, 1]), 3),
      round(float(po.corr.epps_invert(g, L=3)[0, 1]), 3))
-0.001 0.418

See also

Notes

The weights are triangular, on the numerator and on both denominators:

rho_L[a, b] = sum_x (L - |x|) C_x[a, b]
              / sqrt(sum_x (L - |x|) C_x[a, a] * sum_x (L - |x|) C_x[b, b])

over x = -(L-1) .. L-1, with C_x the co-moments at lag x and C_{-x}[a, b] = C_x[b, a]. So the numerator is L C_0[a, b] + sum_{l=1}^{L-1} (L - l) (C_l[a, b] + C_l[b, a]), and each variance term is L C_0[a, a] + 2 sum_l (L - l) C_l[a, a].