quantlab.risk.base.ReturnCovariance#

class quantlab.risk.base.ReturnCovariance(*args, **kwargs)[source]#

Bases: Protocol

The covariance of one-bar returns at a bar, over the symbols it covers: what a covariance estimator returns.

What a rule holding a covariance estimator may ask of its estimate, whatever the estimator. Three types satisfy it and stay separate types (ADR 0028): the dense quantlab.portfolio.base.CovarianceEstimate, the low-rank quantlab.portfolio.base.LowRankCovarianceEstimate (Ledoit-Wolf) and a factor risk model’s FactorRiskForecast. A rule asks the estimate and does not tell the types apart.

Attributes:
symbolsnp.ndarray

The n symbols covered, in the order of every per-symbol array.

covariancenp.ndarray

The dense [n, n] covariance.

variancenp.ndarray

Each symbol’s variance, [n].

Examples

>>> forecast = FactorRiskForecast(
...     symbols=np.array(["AAA", "BBB"]),
...     factor_names=("market",),
...     exposures=np.array([[1.0], [0.5]]),
...     factor_covariance=np.array([[0.04]]),
...     specific_variance=np.array([0.01, 0.02]),
... )
>>> isinstance(forecast, ReturnCovariance)
True
factor_exposure(name, symbols)[source]#

Return each of symbols’ exposure to risk factor name, [len(symbols)].

NaN for a symbol the estimate does not cover. An estimate without risk factors raises ValueError.

factor_form()[source]#

Return (B, F, D) when the covariance is B F B' + diag(D), else None.

From the factor form an optimiser prices risk without the dense matrix.

scaled(factor)[source]#

Return the estimate with every variance multiplied by factor (one bar to factor bars).

subset(rows)[source]#

Return the estimate over the symbols at positions rows, in that order.