qis.compute_portfolio_vol

qis.compute_portfolio_vol(returns, weights, span=None, ewm_lambda=0.94, is_return_vol=True, mean_adj_type=MeanAdjType.NONE, init_type=InitType.ZERO, annualize=False, annualization_factor=None, nan_backfill=NanBackfill.FFILL, weight_lag=1)[source]

Computes portfolio volatility from a point-in-time EWM covariance of the returns.

On each date t of the common index of returns and weights the covariance Σ_t = (1 - λ) r_t r_t’ + λ Σ_{t-1} is updated with that date’s returns, starting from a zero matrix before the first date, and contracted with the weights weight_lag rows earlier: σ²_t = w_{t-k}’ Σ_t w_{t-k}. With the default k = 1 the weights held over (t-1, t] meet a covariance that includes the return they earn, so the result is the EWM variance of the held portfolio as of t; k = 0 pairs the weights of date t with the covariance known at t, the one-period-ahead estimate the VaR functions use. No observation after t enters the estimate at t, so the early values carry the warm-up of the zero seed (its weight at the n-th date is λ^n) rather than information from the end of the sample.

Parameters:
  • returns (DataFrame) – Asset returns DataFrame. Missing returns are set to zero, so a gap decays the covariance by λ per period.

  • weights (DataFrame) – Portfolio weights DataFrame, aligned with returns on their common dates and columns; missing weights are zero.

  • span (int | ndarray) – EWM span for covariance estimation; overrides ewm_lambda via λ = 1 - 2 / (span + 1).

  • ewm_lambda (float | ndarray) – EWM decay parameter (default 0.94), used when span is None.

  • is_return_vol (bool) – If True, returns volatility; if False, returns variance.

  • mean_adj_type (MeanAdjType) – Mean subtracted from the returns before the recursion. MeanAdjType.INSAMPLE subtracts the full-sample mean and is forward-looking.

  • init_type (InitType) – Seed of the running mean of the optional mean adjustment. It does not affect the covariance recursion, which always starts from a zero matrix.

  • annualize (bool) – Whether to annualize: the variance is multiplied by the annualisation factor, so the volatility scales by its square root.

  • annualization_factor (float) – Factor for annualizing (inferred from the index if None).

  • nan_backfill (NanBackfill) – Forwarded to the optional mean adjustment. It has no effect, because missing returns are set to zero before the mean adjustment and the recursion.

  • weight_lag (int) – Number of rows by which the weights are lagged before they meet the covariance (default 1).

Returns:

Time series of portfolio volatility or variance on the common index.

Raises:

ValueError – If weight_lag is negative, which would pair each covariance with weights decided after it.

Return type:

Series