Windowed covariance

PortfolioOptimisers.WindowedCovariance — Type
struct WindowedCovariance{__T_ce, __T_w, __T_window, __T_rule, __T_cache} <: AbstractCovarianceEstimator

Fits an inner covariance estimator on a rolling or indexed window of observations.

WindowedCovariance holds another covariance estimator, and applies it to the observations of a window, with optional observation weights. So the estimate can follow the most recent observations, or give them more weight.

Fields

  • ce: Covariance estimator.

  • w: Optional observation weights vector observations × 1, or a concrete subtype of DynamicAbstractWeights. If nothing, the computation is unweighted.

  • window: Observation window. An integer selects the last window observations, and a vector of indices selects those observations.

  • rule: AbstractWindowRule that sets the observations that the window keeps on the online step. RollingWindow keeps the window at every fit, and SeedWindow keeps it at the first fit alone.

  • cache: Optional partial-fit state. It is nothing until partial_fit! writes one. When the caller gives no data matrix, the batch verb of the estimator reads it. Each propagation channel does one thing with it. factory keeps it unchanged, because a factory call resolves the configuration, not the sample. port_opt_view copies the entries of the selected assets into a new state, so the viewed estimator holds the state of those assets alone. obs_weights_view removes it, because a state has no slice on the observation axis. Only a type with an observation-weights field has that channel. A type with no such field falls through to the identity method, so it keeps its state. A family whose state has no exact asset slice removes it on both axes and names the reason.

Constructors

WindowedCovariance(;    ce::StatsBase.CovarianceEstimator = PortfolioOptimisersCovariance(),    w::Option{<:ObsWeights} = nothing,    window::Option{<:Int_VecInt} = nothing,    rule::AbstractWindowRule = RollingWindow(),    cache::Option{<:AbstractPartialFitState} = nothing) -> WindowedCovariance

Each keyword sets the struct field of the same name.

Validation

Propagated parameters

factory passes its arguments on to these @fprop fields:

  • ce: Recursively updated through factory.
  • w: Replaced with the incoming ObsWeights.
  • cache: Carried unchanged through factory.

View parameters

port_opt_view takes the view of these @vprop fields at the selected indices:

Observation weight parameters

obs_weights_view indexes these fields to the selected observations:

Examples

julia> WindowedCovariance()WindowedCovariance      ce ┼ PortfolioOptimisersCovariance         │   ce ┼ Covariance         │      │    me ┼ SimpleExpectedReturns         │      │       │   w ┴ nothing         │      │    ce ┼ GeneralCovariance         │      │       │   ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)         │      │       │    w ┴ nothing         │      │   alg ┼ FullMoment()         │      │     w ┴ nothing         │   mp ┼ MatrixProcessing         │      │     pdm ┼ Posdef         │      │         │      alg ┼ UnionAll: NearestCorrelationMatrix.Newton         │      │         │   kwargs ┴ @NamedTuple{}: NamedTuple()         │      │      dn ┼ nothing         │      │      dt ┼ nothing         │      │     alg ┼ nothing         │      │   order ┴ NTuple{4, Symbol}: (:pdm, :dn, :dt, :alg)       w ┼ nothing  window ┼ nothing    rule ┴ RollingWindow()

Related

source
Statistics.cov — Method
Statistics.cov(ce::WindowedCovariance, X::MatNum; dims::Int = 1, mean = nothing, iv::Option{<:MatNum} = nothing, kwargs...)

Compute Statistics.cov over a rolling or indexed window of observations, for a matrix input.

The method selects a window of observations from X, and applies the observation weights when the estimator holds them. Then it calls the inner covariance estimator.

Algorithm

  1. Resolve the window and the observation weights with windowed_preamble. It returns inner, a copy of ce.ce that holds the windowed weights, and the windowed X, the windowed iv, and the active_mask and estimation_mask keywords cut to the window.
  2. Call Statistics.cov on inner and the windowed X, and return its result. The inner estimator alone computes the value. This method changes only the observations and the weights that the inner estimator gets.

Arguments

  • ce: Windowed covariance estimator.
  • X: Data matrix of asset returns, observations × assets.
  • dims: Dimension along which the computation runs.
  • mean: Optional precomputed mean, passed to the inner estimator.
  • iv: Optional implied volatility matrix. The estimator reads it only when one of its inner covariance estimators is an ImpliedVolatility.
  • kwargs...: More keyword arguments, passed to the inner estimator. The method cuts the active_mask and estimation_mask keywords to the window.

Returns

  • sigma::MatNum: Covariance matrix assets x assets.

Related

source
Statistics.cov — Method
Statistics.cov(ce::WindowedCovariance, X::MatNum, pnl::Option{<:AssetPanel}; dims::Int = 1, mean = nothing, kwargs...)

Compute Statistics.cov over a rolling or indexed window of observations, for an Asset Panel input.

The method selects a window of observations from X, and applies the observation weights when the estimator holds them. Then it calls the inner covariance estimator. It cuts the Asset Panel to the same observations and gives it to the inner estimator. A mask-aware inner estimator then reads the active mask of the window, and a plain one reduces to the Coverage Universe of the window.

Algorithm

  1. Cut pnl to the observations of the window with windowed_panel.
  2. Resolve the window and the observation weights with windowed_preamble. It returns inner, a copy of ce.ce that holds the windowed weights, and the windowed X, the windowed iv, and the active_mask and estimation_mask keywords cut to the window.
  3. Call Statistics.cov on inner and the windowed X and pnl, and return its result. The inner estimator alone computes the value. This method changes only the observations and the weights that the inner estimator gets.

Arguments

  • ce: Windowed covariance estimator.
  • X: Data matrix of asset returns, observations × assets.
  • pnl: Optional AssetPanel. The Coverage Universe of the fit comes from its active mask. If it is nothing, the rule reads only the finiteness of the returns.
  • dims: Dimension along which the computation runs.
  • mean: Optional precomputed mean, passed to the inner estimator.
  • kwargs...: More keyword arguments, passed to the inner estimator. The method cuts the iv, active_mask and estimation_mask keywords to the window.

Returns

  • sigma::MatNum: Covariance matrix assets x assets.

Related

source
Statistics.cor — Method
Statistics.cor(ce::WindowedCovariance, X::MatNum; dims::Int = 1, mean = nothing, iv::Option{<:MatNum} = nothing, kwargs...)

Compute Statistics.cor over a rolling or indexed window of observations, for a matrix input.

The method selects a window of observations from X, and applies the observation weights when the estimator holds them. Then it calls the inner covariance estimator.

Algorithm

  1. Resolve the window and the observation weights with windowed_preamble. It returns inner, a copy of ce.ce that holds the windowed weights, and the windowed X, the windowed iv, and the active_mask and estimation_mask keywords cut to the window.
  2. Call Statistics.cor on inner and the windowed X, and return its result. The inner estimator alone computes the value. This method changes only the observations and the weights that the inner estimator gets.

Arguments

  • ce: Windowed covariance estimator.
  • X: Data matrix of asset returns, observations × assets.
  • dims: Dimension along which the computation runs.
  • mean: Optional precomputed mean, passed to the inner estimator.
  • iv: Optional implied volatility matrix. The estimator reads it only when one of its inner covariance estimators is an ImpliedVolatility.
  • kwargs...: More keyword arguments, passed to the inner estimator. The method cuts the active_mask and estimation_mask keywords to the window.

Returns

  • rho::MatNum: Correlation matrix assets x assets.

Related

source
Statistics.cor — Method
Statistics.cor(ce::WindowedCovariance, X::MatNum, pnl::Option{<:AssetPanel}; dims::Int = 1, mean = nothing, kwargs...)

Compute Statistics.cor over a rolling or indexed window of observations, for an Asset Panel input.

The method selects a window of observations from X, and applies the observation weights when the estimator holds them. Then it calls the inner covariance estimator. It cuts the Asset Panel to the same observations and gives it to the inner estimator. A mask-aware inner estimator then reads the active mask of the window, and a plain one reduces to the Coverage Universe of the window.

Algorithm

  1. Cut pnl to the observations of the window with windowed_panel.
  2. Resolve the window and the observation weights with windowed_preamble. It returns inner, a copy of ce.ce that holds the windowed weights, and the windowed X, the windowed iv, and the active_mask and estimation_mask keywords cut to the window.
  3. Call Statistics.cor on inner and the windowed X and pnl, and return its result. The inner estimator alone computes the value. This method changes only the observations and the weights that the inner estimator gets.

Arguments

  • ce: Windowed covariance estimator.
  • X: Data matrix of asset returns, observations × assets.
  • pnl: Optional AssetPanel. The Coverage Universe of the fit comes from its active mask. If it is nothing, the rule reads only the finiteness of the returns.
  • dims: Dimension along which the computation runs.
  • mean: Optional precomputed mean, passed to the inner estimator.
  • kwargs...: More keyword arguments, passed to the inner estimator. The method cuts the iv, active_mask and estimation_mask keywords to the window.

Returns

  • rho::MatNum: Correlation matrix assets x assets.

Related

source