Windowed covariance
PortfolioOptimisers.WindowedCovariance — Type
struct WindowedCovariance{__T_ce, __T_w, __T_window, __T_rule, __T_cache} <: AbstractCovarianceEstimatorFits 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 vectorobservations × 1, or a concrete subtype ofDynamicAbstractWeights. Ifnothing, the computation is unweighted.window: Observation window. An integer selects the lastwindowobservations, and a vector of indices selects those observations.rule:AbstractWindowRulethat sets the observations that the window keeps on the online step.RollingWindowkeeps the window at every fit, andSeedWindowkeeps it at the first fit alone.cache: Optional partial-fit state. It isnothinguntilpartial_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.factorykeeps it unchanged, because a factory call resolves the configuration, not the sample.port_opt_viewcopies the entries of the selected assets into a new state, so the viewed estimator holds the state of those assets alone.obs_weights_viewremoves 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) -> WindowedCovarianceEach keyword sets the struct field of the same name.
Validation
ceis not anAbstractVarianceEstimator. A variance estimator has nocovorcor, soassert_covariance_estimatorraises anArgumentErrorthat namesce.- If
wis notnothing,!isempty(w). - If
windowis notnothing, it must be nonempty, nonnegative and finite.
Propagated parameters
factory passes its arguments on to these @fprop fields:
ce: Recursively updated throughfactory.w: Replaced with the incomingObsWeights.cache: Carried unchanged throughfactory.
View parameters
port_opt_view takes the view of these @vprop fields at the selected indices:
ce: Recursively viewed throughport_opt_view.cache: Sliced to the selected assets throughport_opt_view.
Observation weight parameters
obs_weights_view indexes these fields to the selected observations:
ce: Recursively indexed throughobs_weights_view.w: Indexed to the selected observations throughobs_weights_view.
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
AbstractCovarianceEstimatorPortfolioOptimisersCovarianceStatistics.cov(ce::WindowedCovariance, X::MatNum)Statistics.cor(ce::WindowedCovariance, X::MatNum)Statistics.cov(ce::WindowedCovariance, X::MatNum, pnl::Option{<:AssetPanel})Statistics.cor(ce::WindowedCovariance, X::MatNum, pnl::Option{<:AssetPanel})RollingWindowSeedWindowfactoryport_opt_viewobs_weights_viewwindowed_preamble
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
- Resolve the window and the observation weights with
windowed_preamble. It returnsinner, a copy ofce.cethat holds the windowed weights, and the windowedX, the windowediv, and theactive_maskandestimation_maskkeywords cut to the window. - Call
Statistics.covoninnerand the windowedX, 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 anImpliedVolatility.kwargs...: More keyword arguments, passed to the inner estimator. The method cuts theactive_maskandestimation_maskkeywords to the window.
Returns
sigma::MatNum: Covariance matrixassets x assets.
Related
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
- Cut
pnlto the observations of the window withwindowed_panel. - Resolve the window and the observation weights with
windowed_preamble. It returnsinner, a copy ofce.cethat holds the windowed weights, and the windowedX, the windowediv, and theactive_maskandestimation_maskkeywords cut to the window. - Call
Statistics.covoninnerand the windowedXandpnl, 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: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, 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 theiv,active_maskandestimation_maskkeywords to the window.
Returns
sigma::MatNum: Covariance matrixassets x assets.
Related
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
- Resolve the window and the observation weights with
windowed_preamble. It returnsinner, a copy ofce.cethat holds the windowed weights, and the windowedX, the windowediv, and theactive_maskandestimation_maskkeywords cut to the window. - Call
Statistics.coroninnerand the windowedX, 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 anImpliedVolatility.kwargs...: More keyword arguments, passed to the inner estimator. The method cuts theactive_maskandestimation_maskkeywords to the window.
Returns
rho::MatNum: Correlation matrixassets x assets.
Related
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
- Cut
pnlto the observations of the window withwindowed_panel. - Resolve the window and the observation weights with
windowed_preamble. It returnsinner, a copy ofce.cethat holds the windowed weights, and the windowedX, the windowediv, and theactive_maskandestimation_maskkeywords cut to the window. - Call
Statistics.coroninnerand the windowedXandpnl, 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: OptionalAssetPanel. The Coverage Universe of the fit comes from its active mask. If it isnothing, 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 theiv,active_maskandestimation_maskkeywords to the window.
Returns
rho::MatNum: Correlation matrixassets x assets.
Related