Exponentially Weighted Covariance: private API

Types

PortfolioOptimisers.ExpWeightedCovarianceStateType
struct ExpWeightedCovarianceState{__T_covariance, __T_location, __T_obs_count, __T_active} <: AbstractPartialFitState

Internal mutable cache for the online covariance update in ExpWeightedCovariance.

This type is an implementation detail and is not intended for direct use.

Fields

  • covariance: Running exponentially weighted covariance matrix, seeded at zero.
  • location: Exponentially smoothed location (mean) vector.
  • obs_count: Per-asset count of observations processed.
  • active: Boolean mask indicating which assets are currently active.

Related

source

Functions

PortfolioOptimisers.process_observation!Method
process_observation!(
    cache::ExpWeightedCovarianceState,
    ce::ExpWeightedCovariance,
    X::AbstractVector{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}},
    active_mask::Union{Nothing, AbstractVector{<:Bool}}
) -> ExpWeightedCovarianceState

Processes a single observation row (or column) to update the online covariance cache.

An asset is valid when its return is finite and the active mask admits it, and the update touches the sub-block of the valid assets alone. An active asset with a non-finite return freezes every entry that touches it, and an asset that has just become inactive has its whole row and column reset to the cold state so that the bias correction restarts if it lists again.

Arguments

  • cache::ExpWeightedCovarianceState: Online covariance computation cache (mutated).
  • ce::ExpWeightedCovariance: Covariance estimator configuration.
  • X::VecNum: Returns vector for the current observation.
  • active_mask::Option{<:AbstractVector{<:Bool}}: Optional mask of currently active assets. With nothing every asset is active, so a non-finite return reads as a holiday.

Returns

  • cache::ExpWeightedCovarianceState: The cache to read on and to pass to the next observation. Every field is an array that is mutated in place.

Related

source
PortfolioOptimisers.exp_weighted_pass!Function
exp_weighted_pass!(
    f,
    est::ExpWeightedCovariance,
    X::AbstractMatrix{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}},
    dims::Int64,
    active_mask::Union{Nothing, AbstractMatrix{<:Bool}}
) -> ExpWeightedCovarianceState
exp_weighted_pass!(
    f,
    est::ExpWeightedCovariance,
    X::AbstractMatrix{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}},
    dims::Int64,
    active_mask::Union{Nothing, AbstractMatrix{<:Bool}},
    state::Union{Nothing, ExpWeightedCovarianceState}
) -> ExpWeightedCovarianceState

Covariance method of exp_weighted_pass!. Runs one forward pass of the online covariance update over the observations of X, and calls f after each observation.

Related

source
PortfolioOptimisers.exp_weighted_pass!Function
exp_weighted_pass!(
    est::ExpWeightedCovariance,
    X::AbstractMatrix{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}},
    dims::Int64,
    active_mask::Union{Nothing, AbstractMatrix{<:Bool}}
) -> ExpWeightedCovarianceState
exp_weighted_pass!(
    est::ExpWeightedCovariance,
    X::AbstractMatrix{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}},
    dims::Int64,
    active_mask::Union{Nothing, AbstractMatrix{<:Bool}},
    state::Union{Nothing, ExpWeightedCovarianceState}
) -> ExpWeightedCovarianceState

Covariance method of exp_weighted_pass!. Runs one forward pass of the online covariance update over the observations of X, and reads no intermediate cache.

Related

source
PortfolioOptimisers.exp_weighted_momentMethod
exp_weighted_moment(
    cache::ExpWeightedCovarianceState,
    est::ExpWeightedCovariance
) -> Any

Covariance method of exp_weighted_moment. Reads the exponentially weighted covariance out of a cache, as it stands.

Applies the congruence correction, blanks the whole row and column of every asset that is not ready, and re-symmetrises the block that is left, so no transpose can pull a NaN into it. The cache is read, never written, so the same cache answers this call after every observation of a forward pass.

Returns

  • sigma::MatNum: Covariance matrix. The row and the column of an asset that is inactive, or that carries fewer than est.min_obs valid observations, are NaN.

Related

source
PortfolioOptimisers.gap_fill_valueMethod
gap_fill_value(ce::ExpWeightedCovariance) -> Float64

Answer NaN, so a gapped sample reaches the recursion with its gaps intact.

The recursion updates only the sub-block of the assets that are valid at each observation, so a gap never reaches an entry it did not touch, and a fill would decay a block the mask freezes. The consumer therefore hands the sample as it stands, together with the active mask that explains the gap.

Arguments

  • ce: Covariance estimator.

Returns

  • fv::Float64: NaN.

Related

source
PortfolioOptimisers.variance_seriesMethod
variance_series(
    ce::ExpWeightedCovariance,
    X::MatNum;
    dims::Int = 1,
    active_mask::Option{<:AbstractMatrix{<:Bool}} = nothing,
    kwargs...
) -> Matrix{<:Number}

Compute the point-in-time exponentially weighted variance series.

Row t holds the diagonal of what cov returns for the first t observations of X, so no row reads an observation after its own. The update is a recursion over one observation, so this method overrides the expanding-window fallback with a single forward pass: it reads the cache after each observation instead of refitting.

The fallback cannot answer this estimator. It slices X once per row and passes every keyword unsliced, so a mask of the whole window meets a window of t observations and the size check refuses the call.

Arguments

  • ce: Exponentially weighted covariance estimator.
  • X: Data matrix observations × assets if the dims keyword does not exist or dims = 1, assets × observations when dims = 2.
  • dims: Dimension along which to perform the computation.
  • active_mask: Optional boolean matrix with the same size as X.
  • kwargs: Additional keyword arguments (ignored).

Validation

  • dims in (1, 2).
  • If active_mask is not nothing, size(X) == size(active_mask).

Returns

  • val::Matrix{<:Number}: Variance series, shaped as (T, N) if dims == 1 or (N, T) if dims == 2. An asset with fewer than ce.min_obs observations at row t is NaN there.

Related

source
PortfolioOptimisers.variance_seriesMethod
variance_series(
    ce::ExpWeightedCovariance,
    X::MatNum,
    pnl::Option{<:AssetPanel};
    dims::Int = 1,
    kwargs...
) -> Matrix{<:Number}

Compute the point-in-time exponentially weighted variance series from a window of an Asset Panel.

This is the diagonal of the covariance of the same call, read after each observation, and it reads the panel's active mask through the same override. A Descriptor that holds this estimator therefore keeps a number for a young asset, where the reduce-and-expand root would reduce every window and answer NaN.

Arguments

  • ce: Exponentially weighted covariance estimator.
  • X: Data matrix observations × assets if the dims keyword does not exist or dims = 1, assets × observations when dims = 2.
  • pnl: Optional AssetPanel, whose active mask the Coverage Universe of the fit is derived from. nothing makes the rule finiteness alone.
  • dims: Dimension along which to perform the computation.
  • kwargs: Additional keyword arguments (ignored).

Returns

  • val::Matrix{<:Number}: Variance series on the full asset universe, shaped as (T, N) if dims == 1 or (N, T) if dims == 2.

Related

source
Base.copyMethod
copy(
    x::ExpWeightedCovarianceState
) -> ExpWeightedCovarianceState

Copies an ExpWeightedCovarianceState, so the copy shares no array with the original.

The copy method of the AbstractPartialFitState interface, which partial_fit calls before it folds. Every field is an array, and every one is copied.

Arguments

  • x: The cache to copy.

Returns

  • state::ExpWeightedCovarianceState: A fresh cache, equal to x, whose arrays are fresh.

Related

source