Skip to content
13

Windowed covariance

PortfolioOptimisers.WindowedCovariance Type
julia
struct WindowedCovariance{__T_ce, __T_w, __T_window} <: AbstractCovarianceEstimator

Covariance estimator that restricts computation to a rolling or indexed observation window.

WindowedCovariance wraps another covariance estimator and applies it to a subset of observations defined by a window and/or custom observation weights. This enables time-varying or recency-weighted covariance estimation.

Fields

  • ce: Covariance estimator.

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

  • window: Window specification: an integer (last window observations) or a vector of indices.

Constructors

julia
WindowedCovariance(;
    ce::StatsBase.CovarianceEstimator = PortfolioOptimisersCovariance(),
    w::Option{<:ObsWeights} = nothing,
    window::Option{<:Int_VecInt} = nothing
) -> WindowedCovariance

Keywords correspond to the struct's fields.

Validation

  • If w is not nothing, !isempty(w).

  • If window is provided, it must be nonempty, nonnegative, and finite.

Examples

julia
julia> WindowedCovariance()
WindowedCovariance
      ce ┼ PortfolioOptimisersCovariance
         │   ce ┼ Covariance
         │      │    me ┼ SimpleExpectedReturns
         │      │       │   w ┴ nothing
         │      │    ce ┼ GeneralCovariance
         │      │       │   ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
         │      │       │    w ┴ nothing
         │      │   alg ┴ Full()
         │   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

Related

source
PortfolioOptimisers.factory Method
julia
factory(ce::WindowedCovariance, w::ObsWeights) -> WindowedCovariance

Return a new WindowedCovariance estimator with observation weights w applied to the underlying covariance estimator and stored as the windowed weights.

Arguments

  • ce: Covariance estimator.

  • w: Observation weights vector observations × 1.

Returns

  • ce: New covariance estimator of the same type as the argument, with the new weights applied.

Related

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

Compute the covariance matrix using a rolling or indexed observation window.

This method selects a window of observations from X (and applies observation weights if specified), then delegates to the underlying covariance estimator.

Arguments

  • ce: Windowed covariance estimator.

  • X: Data matrix of asset returns (observations × assets).

  • dims: Dimension along which to perform the computation.

  • mean: Optional pre-computed mean passed to the underlying estimator.

  • iv: Optional implied volatility matrix. Used if any internal covariance estimator is an instance of ImpliedVolatility.

  • kwargs...: Additional keyword arguments passed to the underlying estimator.

Returns

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

Related

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

Compute the correlation matrix using a rolling or indexed observation window.

This method selects a window of observations from X (and applies observation weights if specified), then delegates to the underlying covariance estimator's cor method.

Arguments

  • ce: Windowed covariance estimator.

  • X: Data matrix of asset returns (observations × assets).

  • dims: Dimension along which to perform the computation.

  • mean: Optional pre-computed mean passed to the underlying estimator.

  • iv: Optional implied volatility matrix. Used if any internal covariance estimator is an instance of ImpliedVolatility.

  • kwargs...: Additional keyword arguments passed to the underlying estimator.

Returns

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

Related

source
PortfolioOptimisers.port_opt_view Method
julia
port_opt_view(
    ce::WindowedCovariance,
    i,
    args...
) -> WindowedCovariance{<:CovarianceEstimator}

Gets the view of the covariance estimator for the i-th element(s).

Arguments

  • ce: Covariance estimator.

  • i: Index or indices to view.

Returns

  • ce: New covariance estimator of the same type as the argument, for the new view.

Related

source