Regime Adjusted Exponential Weighted Covariance (b)
Functions
Statistics.cov — Method
Statistics.cov(
ce::RegimeAdjustedExpWeightedCovariance,
X::MatNum;
dims::Int = 1,
estimation_mask::Option{<:AbstractMatrix{<:Bool}} = nothing,
active_mask::Option{<:AbstractMatrix{<:Bool}} = nothing,
kwargs...
) -> MatNumCompute the regime-adjusted exponentially weighted covariance matrix.
Iterates over the observation dimension of X, updating an online covariance cache at each step. After the last observation, removes the damping of the zero seed and scales the result by the square of the regime multiplier.
Arguments
ce: Regime-adjusted exponentially weighted covariance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.dims: Dimension along which to perform the computation.estimation_mask: Optional boolean matrix with the same size asX. When provided, only assets whereestimation_mask[i, :](or[:, i]) istruecontribute to the regime state update for observationi.active_mask: Optional boolean matrix with the same size asX. When provided, assets that become inactive have their covariance entries and observation count reset.kwargs: Additional keyword arguments (ignored).
Validation
dims in (1, 2).- If
estimation_maskis notnothing,size(X) == size(estimation_mask). - If
active_maskis notnothing,size(X) == size(active_mask).
Returns
sigma::MatNum: Covariance matrixassets x assets.
Examples
julia> X = [0.01 -0.02; -0.015 0.03; 0.02 -0.01; -0.005 0.012];julia> ce = RegimeAdjustedExpWeightedCovariance(; decay = 0.9, min_obs = 2, regime_min_obs = 2);julia> size(cov(ce, X))(2, 2)Related
RegimeAdjustedExpWeightedCovarianceRegimeAdjustedCovarianceStateregime_adjusted_covarianceStatistics.cor(ce::RegimeAdjustedExpWeightedCovariance, X::MatNum; dims::Int = 1, estimation_mask::Option{<:AbstractMatrix{<:Bool}} = nothing, active_mask::Option{<:AbstractMatrix{<:Bool}} = nothing, kwargs...)
Statistics.cor — Method
Statistics.cor(
ce::RegimeAdjustedExpWeightedCovariance,
X::MatNum;
dims::Int = 1,
estimation_mask::Option{<:AbstractMatrix{<:Bool}} = nothing,
active_mask::Option{<:AbstractMatrix{<:Bool}} = nothing,
kwargs...
) -> MatNumCompute the regime-adjusted exponentially weighted correlation matrix.
This is the covariance of the same call, rescaled to a unit diagonal. The regime multiplier scales the whole matrix, so it cancels in the rescale and the correlation does not read it.
Arguments
ce: Regime-adjusted exponentially weighted covariance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.dims: Dimension along which to perform the computation.estimation_mask: Optional boolean matrix with the same size asX. When provided, only assets whereestimation_mask[i, :](or[:, i]) istruecontribute to the regime state update for observationi.active_mask: Optional boolean matrix with the same size asX. When provided, assets that become inactive have their covariance entries and observation count reset.kwargs: Additional keyword arguments (ignored).
Validation
dims in (1, 2).- If
estimation_maskis notnothing,size(X) == size(estimation_mask). - If
active_maskis notnothing,size(X) == size(active_mask).
Returns
rho::MatNum: Correlation matrixassets x assets.
Examples
julia> X = [0.01 -0.02; -0.015 0.03; 0.02 -0.01; -0.005 0.012];julia> ce = RegimeAdjustedExpWeightedCovariance(; decay = 0.9, min_obs = 2, regime_min_obs = 2);julia> LinearAlgebra.diag(cor(ce, X))2-element Vector{Float64}: 1.0 1.0Related
PortfolioOptimisers.partial_fit! — Method
partial_fit!(
est::Union{AbstractEstimator, CovarianceEstimator},
X::Union{AbstractMatrix{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}}, AbstractVector{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}}};
dims,
active_mask,
estimation_mask
) -> Any
Folds observations into the sample buffer an estimator carries.
This is the buffering arm of partial_fit!. Each estimator that carries a SampleBufferState reaches this method. A family that folds exactly has methods of its own, and each of them narrows the cache type parameter of its estimator to the state of that fold. So a buffer never reaches them, and it reaches this method. The type of the state selects the route, and no method refuses the step.
The method covers both arms of the interface. The families that refuse the step also declare one method over both arms, and two narrower methods here would be ambiguous against each of them. So the body selects the arm by the type of X, and the compiler resolves that branch at each call site.
A buffer carries the per-observation masks beside the observations. So a CoveragePolicy mask goes through the wrapper as it goes through the accumulator of an estimator, and the read-out gives it back to the batch verb. A wrapped estimator folded under a policy gives the answer of a batch fit over the same window under the same policy.
Algorithm
- Read the buffer out of the
cachefield withassert_sample_buffer, which refuses an estimator that was never wrapped inOnline. - Fold a matrix and its masks through the block arm of
partial_fit!, and a vector and its masks through the single-observation arm. - Rebind
est.cachewithAccessors.@reset, and return the estimator.
Arguments
est: Estimator whose buffer is folded forward.X: Observations to fold. A matrix holds one observation per row whendims == 1, and one per column whendims == 2. A vector is a single observation across the assets, anddimsis ignored.dims: Dimension along which to perform the computation.active_mask: The active mask of the block, of the shape ofX, or of one entry per asset whenXis one observation, ornothing.estimation_mask: The estimation mask, on the same terms asactive_mask.
Validation
estcarries aSampleBufferState. AnArgumentErroris thrown otherwise.- The masks, when they are not
nothing, have the shape ofX. ADimensionMismatchis thrown otherwise. - A buffer that holds observations receives the masks that it records. An
ArgumentErroris thrown otherwise. dims in (1, 2).
Returns
est: The estimator, with itscachefield rebound to the buffer after the last observation.
Related
PortfolioOptimisers.partial_fit! — Method
partial_fit!(
est::Union{AbstractEstimator, CovarianceEstimator},
X::Union{AbstractMatrix{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}}, AbstractVector{<:Union{var"#s136", var"#s53"} where {var"#s136"<:Number, var"#s53"<:AbstractJuMPScalar}}};
dims,
active_mask,
estimation_mask
) -> Any
Folds observations into the sample buffer an estimator carries.
This is the buffering arm of partial_fit!. Each estimator that carries a SampleBufferState reaches this method. A family that folds exactly has methods of its own, and each of them narrows the cache type parameter of its estimator to the state of that fold. So a buffer never reaches them, and it reaches this method. The type of the state selects the route, and no method refuses the step.
The method covers both arms of the interface. The families that refuse the step also declare one method over both arms, and two narrower methods here would be ambiguous against each of them. So the body selects the arm by the type of X, and the compiler resolves that branch at each call site.
A buffer carries the per-observation masks beside the observations. So a CoveragePolicy mask goes through the wrapper as it goes through the accumulator of an estimator, and the read-out gives it back to the batch verb. A wrapped estimator folded under a policy gives the answer of a batch fit over the same window under the same policy.
Algorithm
- Read the buffer out of the
cachefield withassert_sample_buffer, which refuses an estimator that was never wrapped inOnline. - Fold a matrix and its masks through the block arm of
partial_fit!, and a vector and its masks through the single-observation arm. - Rebind
est.cachewithAccessors.@reset, and return the estimator.
Arguments
est: Estimator whose buffer is folded forward.X: Observations to fold. A matrix holds one observation per row whendims == 1, and one per column whendims == 2. A vector is a single observation across the assets, anddimsis ignored.dims: Dimension along which to perform the computation.active_mask: The active mask of the block, of the shape ofX, or of one entry per asset whenXis one observation, ornothing.estimation_mask: The estimation mask, on the same terms asactive_mask.
Validation
estcarries aSampleBufferState. AnArgumentErroris thrown otherwise.- The masks, when they are not
nothing, have the shape ofX. ADimensionMismatchis thrown otherwise. - A buffer that holds observations receives the masks that it records. An
ArgumentErroris thrown otherwise. dims in (1, 2).
Returns
est: The estimator, with itscachefield rebound to the buffer after the last observation.
Related
Statistics.cov — Method
Statistics.cov(
ce::RegimeAdjustedExpWeightedCovariance,
state::RegimeAdjustedCovarianceState;
kwargs...
) -> MatNumRead the regime-adjusted covariance out of a state held by hand.
This is regime_adjusted_covariance under the family's public verb, so a state a caller keeps outside an estimator answers the same call as one the estimator holds. The state is read and never written.
Arguments
ce: Regime-adjusted exponentially weighted covariance estimator.state: Running state of an incremental fit.kwargs: Additional keyword arguments (ignored).
Returns
sigma::MatNum: Covariance matrixassets x assets.
Examples
julia> X = [0.01 -0.02; -0.015 0.03; 0.02 -0.01; -0.005 0.012];julia> ce = partial_fit!(RegimeAdjustedExpWeightedCovariance(; decay = 0.9, min_obs = 2, regime_min_obs = 2), X);julia> isequal(cov(ce, ce.cache), cov(ce))trueRelated
Statistics.cov — Method
Statistics.cov(ce::RegimeAdjustedExpWeightedCovariance; kwargs...) -> MatNumRead the regime-adjusted covariance out of the estimator's own state.
The one-argument form is what an incremental fit answers: partial_fit! leaves the state in the cache field, and this verb turns it into the ordinary answer. An estimator that has been given no observation carries no state, so the call is refused rather than answered with a zero.
Arguments
ce: Regime-adjusted exponentially weighted covariance estimator carrying a state.kwargs: Additional keyword arguments (ignored).
Validation
ce.cacheis notnothing. AnArgumentErroris thrown otherwise.
Returns
sigma::MatNum: Covariance matrixassets x assets.
Examples
julia> X = [0.01 -0.02; -0.015 0.03; 0.02 -0.01; -0.005 0.012];julia> ce = partial_fit!(RegimeAdjustedExpWeightedCovariance(; decay = 0.9, min_obs = 2, regime_min_obs = 2), X);julia> size(cov(ce))(2, 2)julia> cov(RegimeAdjustedExpWeightedCovariance())ERROR: ArgumentError: `ce` holds no partial-fit state, so there is nothing to read. Call `partial_fit!(ce, X)` first, or `cov(ce, X)` for a fit over a whole sample.[...]Related
partial_fit!(ce::RegimeAdjustedExpWeightedCovariance, X::MatNum; dims::Int = 1, estimation_mask::Option{<:AbstractMatrix{<:Bool}} = nothing, active_mask::Option{<:AbstractMatrix{<:Bool}} = nothing, kwargs...)Statistics.cov(ce::RegimeAdjustedExpWeightedCovariance, state::RegimeAdjustedCovarianceState; kwargs...)RegimeAdjustedCovarianceState
Statistics.cor — Method
Statistics.cor(
ce::RegimeAdjustedExpWeightedCovariance,
state::RegimeAdjustedCovarianceState;
kwargs...
) -> MatNumRead the regime-adjusted correlation out of a state held by hand.
The rescale of Statistics.cov(ce::RegimeAdjustedExpWeightedCovariance, state::RegimeAdjustedCovarianceState; kwargs...) to a unit diagonal. The state is read and never written.
Arguments
ce: Regime-adjusted exponentially weighted covariance estimator.state: Running state of an incremental fit.kwargs: Additional keyword arguments (ignored).
Returns
rho::MatNum: Correlation matrixassets x assets.
Examples
julia> X = [0.01 -0.02; -0.015 0.03; 0.02 -0.01; -0.005 0.012];julia> ce = partial_fit!(RegimeAdjustedExpWeightedCovariance(; decay = 0.9, min_obs = 2, regime_min_obs = 2), X);julia> isequal(cor(ce, ce.cache), cor(ce))trueRelated
Statistics.cor — Method
Statistics.cor(ce::RegimeAdjustedExpWeightedCovariance; kwargs...) -> MatNumRead the regime-adjusted correlation out of the estimator's own state.
The one-argument form is what an incremental fit answers: partial_fit! leaves the state in the cache field, and this verb turns it into the ordinary answer. An estimator that has been given no observation carries no state, so the call is refused rather than answered with a zero.
Arguments
ce: Regime-adjusted exponentially weighted covariance estimator carrying a state.kwargs: Additional keyword arguments (ignored).
Validation
ce.cacheis notnothing. AnArgumentErroris thrown otherwise.
Returns
rho::MatNum: Correlation matrixassets x assets.
Examples
julia> X = [0.01 -0.02; -0.015 0.03; 0.02 -0.01; -0.005 0.012];julia> ce = partial_fit!(RegimeAdjustedExpWeightedCovariance(; decay = 0.9, min_obs = 2, regime_min_obs = 2), X);julia> size(cor(ce))(2, 2)julia> cor(RegimeAdjustedExpWeightedCovariance())ERROR: ArgumentError: `ce` holds no partial-fit state, so there is nothing to read. Call `partial_fit!(ce, X)` first, or `cor(ce, X)` for a fit over a whole sample.[...]Related
partial_fit!(ce::RegimeAdjustedExpWeightedCovariance, X::MatNum; dims::Int = 1, estimation_mask::Option{<:AbstractMatrix{<:Bool}} = nothing, active_mask::Option{<:AbstractMatrix{<:Bool}} = nothing, kwargs...)Statistics.cor(ce::RegimeAdjustedExpWeightedCovariance, state::RegimeAdjustedCovarianceState; kwargs...)RegimeAdjustedCovarianceState
Statistics.cov — Method
Statistics.cov(
ce::RegimeAdjustedExpWeightedCovariance,
X::MatNum,
pnl::Option{<:AssetPanel};
dims::Int = 1,
kwargs...
) -> MatNumCompute the regime-adjusted exponentially weighted covariance from a window of an Asset Panel.
This estimator is mask-aware, so it overrides the reduce-and-expand root of the verb and reads the panel's two masks itself: the active mask drives the freeze and the reset, and the estimation mask restricts which assets feed the regime statistic. The answer therefore lives on the whole universe rather than on the Coverage Universe, and a young asset that lists inside the window is answered from the observations it has.
Arguments
ce: Regime-adjusted exponentially weighted covariance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel, whose active mask the Coverage Universe of the fit is derived from.nothingmakes the rule finiteness alone.dims: Dimension along which to perform the computation.kwargs: Additional keyword arguments (ignored).
Returns
sigma::MatNum: Covariance matrix of sizeassets × assets.
Related
Statistics.cor — Method
Statistics.cor(
ce::RegimeAdjustedExpWeightedCovariance,
X::MatNum,
pnl::Option{<:AssetPanel};
dims::Int = 1,
kwargs...
) -> MatNumCompute the regime-adjusted exponentially weighted correlation from a window of an Asset Panel.
This is the covariance of the same call, rescaled to a unit diagonal, and it reads the panel's two masks through the same override.
Arguments
ce: Regime-adjusted exponentially weighted covariance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel, whose active mask the Coverage Universe of the fit is derived from.nothingmakes the rule finiteness alone.dims: Dimension along which to perform the computation.kwargs: Additional keyword arguments (ignored).
Returns
rho::MatNum: Correlation matrix of sizeassets × assets.
Related
Statistics.var — Method
Statistics.var(
ce::RegimeAdjustedExpWeightedCovariance,
X::MatNum,
pnl::Option{<:AssetPanel};
dims::Int = 1,
kwargs...
) -> MatNumCompute the marginal variance of the regime-adjusted exponentially weighted covariance from a window of an Asset Panel.
This is the diagonal of the covariance of the same call, and it reads the panel's two masks through the same override.
Arguments
ce: Regime-adjusted exponentially weighted covariance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel, whose active mask the Coverage Universe of the fit is derived from.nothingmakes the rule finiteness alone.dims: Dimension along which to perform the computation.kwargs: Additional keyword arguments (ignored).
Returns
var::MatNum: Marginal variance, as a row wheredimsis1and as a column otherwise.
Related
Statistics.std — Method
Statistics.std(
ce::RegimeAdjustedExpWeightedCovariance,
X::MatNum,
pnl::Option{<:AssetPanel};
dims::Int = 1,
kwargs...
) -> MatNumCompute the marginal volatility of the regime-adjusted exponentially weighted covariance from a window of an Asset Panel.
This is the square root of the diagonal of the covariance of the same call, and it reads the panel's two masks through the same override.
Arguments
ce: Regime-adjusted exponentially weighted covariance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel, whose active mask the Coverage Universe of the fit is derived from.nothingmakes the rule finiteness alone.dims: Dimension along which to perform the computation.kwargs: Additional keyword arguments (ignored).
Returns
std::MatNum: Marginal volatility, as a row wheredimsis1and as a column otherwise.
Related
PortfolioOptimisers.merge_states — Method
merge_states(
a::RegimeAdjustedCovarianceState,
b::RegimeAdjustedCovarianceState
)
Refuses a pair of regime-adjusted covariance states, because this family does not merge.
A block fitted from a cold start is not what the same block contributes after another one. The regime statistic scores each observation against the state that stands before it, so a cold block loses every comparison its first min_obs observations would have made, and a correlation state that is normalised by a running variance carries that variance with it.
Fold the second block into the first with partial_fit! instead. A sequential fit is exact, and it is the route this family gives.
Algorithm
- Refuse the pair with
assert_mergeable_states, which names a type mismatch and an asset-count mismatch first, as theAbstractPartialFitStateinterface asks of every family. - Throw an
ArgumentErrornaming the reason this family does not merge.
Arguments
a: The first state.b: The second state.
Validation
aandbpassassert_mergeable_states.
Returns
- Never returns. An
ArgumentErroris thrown.
Related