Regime adjusted exp weighted variance: private API
Types
PortfolioOptimisers.RegimeAdjustedVarianceState — Type
struct RegimeAdjustedVarianceState{__T_ret_buffer, __T_variance, __T_X2, __T_X_old_i, __T_z2, __T_location, __T_obs_count, __T_old_obs_count, __T_active, __T_regime_state, __T_n_regime_obs, __T_bias, __T_lag_records} <: AbstractPartialFitStateHolds the running state of an incremental fit of a RegimeAdjustedExpWeightedVariance.
partial_fit! builds it and keeps it in the cache field of the estimator, and var(ce, state) reads it. A caller can hold a state outside the estimator, but only the forward pass of the estimator writes it. The fold mutates the arrays in place, and it builds a new state with the two new scalar fields, regime_state and n_regime_obs, through the positional constructor.
Fields
ret_buffer: Optional circular buffer of recent centred returns for HAC kernel correction.
variance: Running per-asset variance vector.
X2: Working array for the terms of the current observation, with the HAC products wherehac_lagsis set.
X_old_i: Working array for lagged centred returns.
z2: Standardised squared returns of the current observation, which the regime statistic reads.
location: Exponentially smoothed location (mean) vector.
obs_count: Per-asset count of observations processed.
old_obs_count: Per-asset observation count from the previous step.
active: Boolean mask of the assets that are active now.
regime_state: Current smoothed regime state, ornothingbefore the first scored observation.
n_regime_obs: Count of the observations that advanced the regime state.
bias: Table of the bias factors of the regime statistic, one per count of observations, which grows as the counts grow. It isnothingwhere the estimator hasdebias = nothingor has no regime method, and a target that reads no such table leaves it empty.
lag_records: Circular buffer of the records of the recent observations, the counts, the valid assets and, for a covariance, the overlap of the locations before each one, which the exact factor of a HAC product reads. It isnothingunderPreCentredor without HAC.
Related
PortfolioOptimisers.RegimeTermMoments — Type
struct RegimeTermMoments{__T_method}Names the table of the moments of one term of a sum of regime statistics, which the table of a DiagonalTarget holds.
A term $u_{i}^{2} / \hat{v}_{i}$ reads one estimated variance $\hat{v}_{i} = \sigma_{i}^{2} Q_{i}$. The estimator divides the term by the factor of the mean, $f = \mathbb{E}[Q^{-1}]$, so the term carries the noise $a = 1 / (f Q)$, with $\mathbb{E}[a] = 1$.
The root and the log of the sum read that noise through a variable of their own. In that variable the statistic is linear along the direction where every term carries the same noise. The variable is $a^{1/2}$ for FirstMomentRegimeAdjusted and $\ln a$ for LogRegimeAdjusted. For each count, regime_bias_factor gives the triple $(f, b, v)$. These are the factor of the mean, the scale $b$ at which the variable of the method has its mean, and the relative variance $v$ of that variable.
| Method | $b$ | $v$ |
|---|---|---|
FirstMomentRegimeAdjusted | $\mathbb{E}[Q^{-1/2}]^{2} / f$ | $f / \mathbb{E}[Q^{-1/2}]^{2} - 1$ |
LogRegimeAdjusted | $\exp(-\mathbb{E}[\ln Q]) / f$ | $\mathrm{Var}[\ln Q]$ |
| any other method | $1$ | $0$ |
Fields
method: Regime adjustment method whose moments of one term the table holds.
Related
Functions
PortfolioOptimisers.assert_regime_debias — Function
assert_regime_debias(
regime_method::Union{Nothing, RegimeAdjustedMethod},
debias::Union{Nothing, AbstractRegimeDebias},
name::Symbol
)
Checks that the bias correction of debias can read the moment of regime_method.
ExactDebias and LawDebias divide the regime statistic by the bias of the moment that the method reads, and the library computes that bias for its own three methods alone. A regime method that a caller defines therefore takes debias = nothing. The library methods, and a regime_method of nothing, take every rule.
Arguments
regime_method::Option{<:RegimeAdjustedMethod}: Regime adjustment method of the estimator.debias::Option{<:AbstractRegimeDebias}: Bias correction of the estimator.name::Symbol: Name of the estimator, for the message.
Validation
- Where
regime_methodis not a library method,debiascorrects no bias. AnArgumentErroris thrown otherwise.
Returns
nothing.
Related
PortfolioOptimisers.regime_bias_factor — Function
regime_bias_factor(
_::RootMeanSquaredAdjusted,
s::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
G::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
c::Number,
h::Number
) -> Any
Computes the bias factor that the root-mean-squared adjustment reads, $\mathbb{E}[Q^{-1}]$.
Q is the estimated variance of one return over its true variance, and regime_bias_table states it. The method smooths $z^{2} = u^{2} / \hat{v}$ and its calibration assumes $\mathbb{E}[z^{2}] = 1$, but $\mathbb{E}[z^{2}] = \mathbb{E}[Q^{-1}]$.
Arguments
::RootMeanSquaredAdjusted: Root-mean-squared regime adjustment method.s::VecNum: Grid of the integration variable $s = e^{x}$.G::VecNum: $G(s)$, the Laplace transform ofQon the grid.c::Number: Normalised weight of the newest observation.h::Number: Step of the grid in $x$.
Returns
factor::Number: $\int_{0}^{\infty} G(s)\, \mathrm{d}s / (2 c)$.
Related
regime_bias_factor(
_::FirstMomentRegimeAdjusted,
s::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
G::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
c::Number,
h::Number
) -> Any
Computes the bias factor that the first-moment adjustment reads, $\mathbb{E}[Q^{-1/2}]^{2}$.
The method smooths $|z| = |u| / \sqrt{\hat{v}}$, and $\mathbb{E}[|z|] = \mathbb{E}[|u|]\, \mathbb{E}[Q^{-1/2}]$, so the statistic $z^{2}$ is divided by the square of that moment.
Arguments
::FirstMomentRegimeAdjusted: First-moment regime adjustment method.s::VecNum: Grid of the integration variable $s = e^{x}$.G::VecNum: $G(s)$, the Laplace transform ofQon the grid.c::Number: Normalised weight of the newest observation.h::Number: Step of the grid in $x$.
Returns
factor::Number: $\left(\int_{0}^{\infty} s^{-1/2} G(s)\, \mathrm{d}s\right)^{2} / (2 \pi c)$.
Related
regime_bias_factor(
_::LogRegimeAdjusted,
s::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
G::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
c::Number,
h::Number
) -> Any
Computes the bias factor that the log adjustment reads, $\exp(-\mathbb{E}[\ln Q])$.
The method smooths $\ln z^{2} = \ln u^{2} - \ln Q$, so the statistic $z^{2}$ is divided by $\exp(-\mathbb{E}[\ln Q])$, whatever the parameters of the method.
Arguments
::LogRegimeAdjusted: Log regime adjustment method.s::VecNum: Grid of the integration variable $s = e^{x}$.G::VecNum: $G(s)$, the Laplace transform ofQon the grid.c::Number: Normalised weight of the newest observation.h::Number: Step of the grid in $x$.
Returns
factor::Number: $\exp\left(-\int_{0}^{\infty} (e^{-s} - G(s))\, s^{-1}\, \mathrm{d}s - \ln(2 c)\right)$.
Related
regime_bias_factor(
_::RegimeTermMoments,
s::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
G::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
c::Number,
h::Number
) -> Tuple{Any, Any, Any}
Returns the moments of one term for a method that reads the mean of the sum: the mean's factor, a scale of one and no variance.
Arguments
::RegimeTermMoments: Table of the moments of one term.s::VecNum: Grid of the integration variable $s = e^{x}$.G::VecNum: $G(s)$, the Laplace transform ofQon the grid.c::Number: Normalised weight of the newest observation.h::Number: Step of the grid in $x$.
Returns
moments::Tuple: $(\mathbb{E}[Q^{-1}], 1, 0)$.
Related
regime_bias_factor(
m::RegimeTermMoments{<:FirstMomentRegimeAdjusted},
s::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
G::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
c::Number,
h::Number
) -> Tuple{Any, Any, Any}
Returns the moments of one term for the first-moment method, which reads the root of the noise $a = 1 / (f Q)$.
Arguments
m::RegimeTermMoments{<:FirstMomentRegimeAdjusted}: Table of the moments of one term.s::VecNum: Grid of the integration variable $s = e^{x}$.G::VecNum: $G(s)$, the Laplace transform ofQon the grid.c::Number: Normalised weight of the newest observation.h::Number: Step of the grid in $x$.
Returns
moments::Tuple: $(f, q / f, f / q - 1)$, with $f = \mathbb{E}[Q^{-1}]$ and $q = \mathbb{E}[Q^{-1/2}]^{2}$, so $b = \mathbb{E}[a^{1/2}]^{2}$ and $v$ is the relative variance of $a^{1/2}$.
Related
regime_bias_factor(
m::RegimeTermMoments{<:LogRegimeAdjusted},
s::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
G::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
c::Number,
h::Number
) -> Tuple{Any, Any, Any}
Returns the moments of one term for the log method, which reads the log of the noise $a = 1 / (f Q)$.
Arguments
m::RegimeTermMoments{<:LogRegimeAdjusted}: Table of the moments of one term.s::VecNum: Grid of the integration variable $s = e^{x}$.G::VecNum: $G(s)$, the Laplace transform ofQon the grid.c::Number: Normalised weight of the newest observation.h::Number: Step of the grid in $x$.
Returns
moments::Tuple: $(f, e^{-\mathbb{E}[\ln Q]} / f, \mathrm{Var}[\ln Q])$, with $f = \mathbb{E}[Q^{-1}]$, so $b = \exp(\mathbb{E}[\ln a])$, fromregime_log_variance.
Related
PortfolioOptimisers.regime_log_variance — Function
regime_log_variance(
s::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
G::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
h::Number
) -> Any
Computes the variance of the log of an estimated variance, from the Laplace transform of Q.
The Mellin transform of $Q' = 2 c Q$ expands about zero as $\mathbb{E}[Q'^{-\sigma}] = 1 + \sigma I_{0} + \sigma^{2} (I_{1} + \gamma I_{0}) + O(\sigma^{3})$, so its first two coefficients are the first two moments of $-\ln Q'$. A scale does not move a variance of a log, so the variance of $\ln Q'$ is that of $\ln Q$. At equal weights it agrees with the trigamma function $\psi'(K / 2)$.
Mathematical definition
\[\begin{align} I_{0} &= \int_{0}^{\infty} \left(G(s) - e^{-s}\right) s^{-1}\, \mathrm{d}s\,, \quad I_{1} = \int_{0}^{\infty} \ln s \left(G(s) - e^{-s}\right) s^{-1}\, \mathrm{d}s\,, \\ \mathrm{Var}[\ln Q] &= 2 (I_{1} + \gamma I_{0}) - I_{0}^{2}\,. \end{align}\]
Where $\gamma$ is the Euler-Mascheroni constant.
Arguments
s::VecNum: Grid of the integration variable $s = e^{x}$.G::VecNum: $G(s)$, the Laplace transform ofQon the grid.h::Number: Step of the grid in $x$.
Returns
v::Number: $\mathrm{Var}[\ln Q]$.
Related
PortfolioOptimisers.regime_bias! — Function
regime_bias!(
bias::AbstractVector,
method::Union{RegimeAdjustedMethod, RegimeTermMoments},
decay::Number,
K::Integer
) -> Any
regime_bias!(
bias::AbstractVector,
method::Union{RegimeAdjustedMethod, RegimeTermMoments},
decay::Number,
K::Integer,
hac_lags::Union{Nothing, Integer, AbstractVector{<:Union{Number, AbstractJuMPScalar}}}
) -> Any
regime_bias!(
bias::AbstractVector,
method::Union{RegimeAdjustedMethod, RegimeTermMoments},
decay::Number,
K::Integer,
hac_lags::Union{Nothing, Integer, AbstractVector{<:Union{Number, AbstractJuMPScalar}}},
centring::AbstractCentring
) -> Any
regime_bias!(
bias::AbstractVector,
method::Union{RegimeAdjustedMethod, RegimeTermMoments},
decay::Number,
K::Integer,
hac_lags::Union{Nothing, Integer, AbstractVector{<:Union{Number, AbstractJuMPScalar}}},
centring::AbstractCentring,
debias::Union{Nothing, AbstractRegimeDebias}
) -> Any
Returns the bias factor of a regime statistic at a count of observations, and grows the table of the state where the count is past its end.
The table grows to twice the count, and to 64 entries at the least. It stops at the count $K_{\mathrm{sat}}$ where $\lambda^{K}$ falls below the machine epsilon of the table. Past that count the factor does not change, so the last entry serves every larger count. A fit over $T$ rows thus makes the table once or a few times, and the table holds no more than $\min(\max(2 T, 64), K_{\mathrm{sat}})$ entries.
Arguments
bias::AbstractVector: Table of the state (mutated where it grows).method::Union{RegimeAdjustedMethod, RegimeTermMoments}: Regime adjustment method, which names the moment, or the table of the moments of one term.decay::Number: Decay of the weights.K::Integer: Count of observations in the estimate.hac_lags::Option{<:Union{Integer, VecNum}}: Count of HAC lags of the estimate, or the weight of each lag, whose tableregime_bias_tablecomputes from its banded weight matrix, ornothing.centring::AbstractCentring: The centring of the estimate. UnderEstimatedCentringthe table reads the exact law of the estimated deviations. The default,PreCentred, reads the law of pre-centred terms.debias::Option{<:AbstractRegimeDebias}: Whether the table reads the dependence of the deviation on the estimate, byreads_dependence.ExactDebiasby default.
Returns
factor: The factor atK, or the moments ofRegimeTermMomentsatK.
Related
regime_bias!(::Nothing, , decay::Number, ::Integer) -> Any
regime_bias!(::Nothing, , decay::Number, ::Integer, ) -> Any
regime_bias!(
::Nothing,
,
decay::Number,
::Integer,
,
) -> Any
regime_bias!(
::Nothing,
,
decay::Number,
::Integer,
,
,
) -> Any
Returns one, the factor of a state that takes no bias correction.
Arguments
::Nothing: The table of a state whose estimator hasdebias = nothingor no regime method.::Any: Ignored regime adjustment method.decay::Number: Decay of the weights.::Integer: Ignored count of observations.::Any: Ignored count of HAC lags.::Any: Ignored centring.::Any: Ignored debias rule.
Returns
one(decay).
Related
PortfolioOptimisers.regime_bias_open — Function
regime_bias_open(
debias::Union{Nothing, AbstractRegimeDebias},
n::Integer,
decay::Number,
K::Integer,
hac_lags::Union{Nothing, Integer, AbstractVector{<:Union{Number, AbstractJuMPScalar}}}
) -> Any
Returns whether an estimate of K observations is old enough for its regime statistic to be scored.
The statistic of n directions reads the inverse of an estimate, and the variance of that inverse is finite from $K > n + 3$. Before that count, one observation can dominate the smoothed regime state for many half-lives. A HAC estimate has a weight matrix with negative eigenvalues, so it can fail to be positive definite at any count, and no moment of its inverse is finite. Its effective count of observations, $1 / \operatorname{tr}(A^{2})$ from exp_weight_cross_sum, must also pass $n + 1$. That is the count at which the mean of the inverse of a plain estimate is finite.
On pre-centred normal returns at 12 assets, a half-life of 10 and two lags, K > n + 3 alone scores the estimate at $K = 16$, where 19 % of draws are not positive definite. With the effective count the first scored row is $K = 54$, and no draw of 20000 fails there. Over half-lives of 5, 10 and 40, one to five lags and one to 12 assets, the first scored row is not positive definite in at most $1.3 \times 10^{-3}$ of draws at a half-life of 5. The share is at most $4 \times 10^{-4}$ at 10, and no draw of 5000 fails at 40. The gate never scores an estimate whose steady state has too few effective observations, such as 12 assets at a half-life of 5. Without the correction this gate opens for every estimate, and min_obs alone gates it.
Arguments
debias::Option{<:AbstractRegimeDebias}: Whether the estimator corrects the bias of its statistic.n::Integer: Count of directions that the statistic reads.decay::Number: Decay of the weights.K::Integer: Count of the terms in the estimate.hac_lags::Option{<:Union{Integer, VecNum}}: Count of HAC lags, the weight of each lag, ornothing.
Returns
open::Bool: Whether the statistic is scored.
Related
PortfolioOptimisers.hac_squared_returns! — Function
hac_squared_returns!(
cache::RegimeAdjustedVarianceState,
ce::RegimeAdjustedExpWeightedVariance,
X::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
valid::AbstractVector{<:Bool},
dvalid::AbstractVector{<:Bool},
f::AbstractVector{<:Union{Number, AbstractJuMPScalar}}
) -> Any
Computes the terms of the current observation, and stores them in cache.X2.
A term is the squared deviation, plus the HAC products where hac_lags is set, over its exact factor.
The factor is the mean of the term in units of the variance, for returns that are independent in time: centring_factor of the square plus centring_lag_factor of the lagged products. Both are one and zero under PreCentred. The floor rule of ce.hac_floor, if it is not nothing, then acts on the terms of the valid assets.
Arguments
cache::RegimeAdjustedVarianceState: Online variance computation cache.ce::RegimeAdjustedExpWeightedVariance: Variance estimator configuration.X::VecNum: The deviations of the current observation.valid::AbstractVector{<:Bool}: Mask of the valid assets.dvalid::AbstractVector{<:Bool}: Mask of the assets whose deviation exists.f::VecNum: The factor of each squared deviation.
Returns
X2::VecNum: The terms, stored incache.X2.
Related
PortfolioOptimisers.blend_regime_state — Function
blend_regime_state(prev::Nothing, new::Number, decay::Number) -> Number
blend_regime_state(prev::Number, new::Number, decay::Number) -> NumberBlends the regime state of the last step with the regime state of one new observation.
process_observation! and the regime update of RegimeAdjustedExpWeightedCovariance call it. The first step that advances the regime has no state before it, so prev is nothing, and the function returns new. A later step weights prev by decay and new by one minus decay.
Arguments
prev: The regime state of the last step, ornothingbefore the first step.new: The regime state of the new observation.decay: The decay of the regime state, theregime_decayof the estimator.
Returns
s::Number: The blended regime state.
Related
PortfolioOptimisers.process_observation! — Method
process_observation!(
cache::RegimeAdjustedVarianceState,
ce::RegimeAdjustedExpWeightedVariance,
X::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
estimation_mask::Union{Nothing, AbstractVector{<:Bool}},
active_mask::Union{Nothing, AbstractVector{<:Bool}}
) -> RegimeAdjustedVarianceState
Folds one observation into the online variance cache.
The function steps the location, the variance and the standardised squared returns in cache, and then it advances the smoothed regime state. A regime_method of nothing advances no regime state, so the variance stays the plain exponentially weighted recursion. A row with no valid asset changes no estimate and no count. Under HAC it still enters the buffer as one lag, so a lag counts observations whatever the assets hold.
Algorithm
- Mark the assets whose return is finite, and that
active_maskholds where it is set, givingvalid. - Reset the variance, the count, the location, the HAC buffer and the lagged records of each asset that becomes inactive, and store the mask in
cache.active. - Copy
cache.obs_counttocache.old_obs_count. Step the location withcentring_location!, giving the deviationsXiand the maskdvalidof the assets whose deviation exists. - Compute the exact factor
fof each squared deviation, and the countKof the terms in each variance. - Form
regime_mask, the assets ofdvalidthatmin_obsadmits and thatregime_bias_openscores. The mask is empty whereregime_methodisnothing. - Write $z^{2}$ to
cache.z2for each asset ofregime_maskwhose variance is at leastmin_val, with the weight of the zero seed and the factor ofregime_bias!. - Compute the terms
X2withhac_squared_returns!. Step the variance of each asset ofdvalid, and add one to the count of each asset ofvalid. - Under HAC, push the deviations to the buffer, with
NaNwhere no deviation exists, and push the lagged record. - Keep the assets of
regime_maskthatestimation_maskholds. Return the cache where no asset is left. - Compute the statistic with
get_regime_state, and blend it into the regime state withblend_regime_state. - Return a new state that holds the blended regime state and one more regime observation.
Arguments
cache::RegimeAdjustedVarianceState: Online variance computation cache (mutated).ce::RegimeAdjustedExpWeightedVariance: Variance estimator configuration.X::VecNum: Returns vector for the current observation.estimation_mask::Option{<:AbstractVector{<:Bool}}: Optional mask of the assets that the regime state reads.active_mask::Option{<:AbstractVector{<:Bool}}: Optional mask of the active assets. An asset that becomes inactive restarts its variance and its counts from zero.
Returns
cache::RegimeAdjustedVarianceState: The cache to read, and to pass to the next observation. The function mutates the arrays in place.regime_stateandn_regime_obsare immutable fields, so a new state holds them, and the caller must rebind the cache to this return value. A caller that discards it freezes the regime state at its initial value.
Related
PortfolioOptimisers.regime_adjusted_variance_pass! — Function
regime_adjusted_variance_pass!(
f,
ce::RegimeAdjustedExpWeightedVariance,
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
dims::Int64,
estimation_mask::Union{Nothing, AbstractMatrix{<:Bool}},
active_mask::Union{Nothing, AbstractMatrix{<:Bool}}
) -> RegimeAdjustedVarianceState
regime_adjusted_variance_pass!(
f,
ce::RegimeAdjustedExpWeightedVariance,
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
dims::Int64,
estimation_mask::Union{Nothing, AbstractMatrix{<:Bool}},
active_mask::Union{Nothing, AbstractMatrix{<:Bool}},
state::Union{Nothing, RegimeAdjustedVarianceState}
) -> RegimeAdjustedVarianceState
Run one forward pass of the online variance update over the observations of X, and call f after each observation.
The pass holds the argument validation, the orientation and the cache, so the two verbs that read it, var and variance_series, state the recursion once. f receives the observation index and the cache after that observation. So a point-in-time series is one forward pass, and not one refit for each observation. A verb that wants the last cache alone, as var and partial_fit! do, passes Returns(nothing) as f.
Arguments
f: Function of(i, cache)called after observationiis processed. A caller that wants only the final cache reads the method that takes nof.ce: Regime-adjusted exponentially weighted variance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.dims: Dimension along which the computation runs.estimation_mask: Optional boolean matrix of the size ofX. Where it is set, the regime state at observationireads only the assets whereestimation_mask[i, :](or[:, i]) istrue.active_mask: Optional boolean matrix of the size ofX. Where it is set, an asset that becomes inactive restarts its variance and its observation count from zero.state: Optional cache to continue from. The defaultnothingbuilds the cold cache, which is what a fit over a whole sample needs.partial_fit!passes the estimator's own state instead, so the pass continues where the last call stopped.
Validation
dims in (1, 2).- If
estimation_maskis notnothing,size(X) == size(estimation_mask). - If
active_maskis notnothing,size(X) == size(active_mask). - If
stateis notnothing, it holds as many assets asX.
Returns
cache::RegimeAdjustedVarianceState: The cache after the last observation.
Related
PortfolioOptimisers.regime_adjusted_variance — Function
regime_adjusted_variance(
cache::RegimeAdjustedVarianceState,
ce::RegimeAdjustedExpWeightedVariance
) -> Any
Read the regime-adjusted variance out of a cache, as it stands.
A HAC state is a quadratic form with an indefinite weight matrix, so it can fall below zero. The function floors the variance at zero here, and not in the recursion, so the recursion stays unbiased. The function reads the cache and never writes it, so the same cache answers this call after every observation of a forward pass.
Where ce.regime_method is nothing, process_observation! advances no regime state, so cache.n_regime_obs stays at zero, which is below every admissible regime_min_obs. The multiplier is then one, and the variance is the plain recursion.
Where ce.regime_lohi_mult is not nothing, the function clamps the multiplier to that (lo, hi) range, and then squares it. Where it is nothing, no clamp runs. The clamp bounds an estimate, so the multiplier of one before regime_min_obs holds even where lo > 1 or hi < 1.
Algorithm
- Floor the raw variance of each asset at zero, giving
variance. - Divide the variance of each asset that holds a term by
1 - decay^K, the weight that the zero seed leaves. - Set the variance to
NaNfor each asset that is not active, or thatmin_obsdoes not admit. - Compute the multiplier
factor. It is one beforeregime_min_obsregime observations, and otherwiseregime_multiplierof the regime state, clamped whereregime_lohi_multis set. - Return
variance * factor^2.
Arguments
cache::RegimeAdjustedVarianceState: Online variance computation cache.ce::RegimeAdjustedExpWeightedVariance: Variance estimator configuration.
Returns
variance::Vector{<:Number}: Per-asset regime-adjusted variance vector of lengthassets. An asset with fewer thance.min_obsobservations, or one that is not active, isNaN.
Related
PortfolioOptimisers.variance_series — Method
variance_series(
ce::RegimeAdjustedExpWeightedVariance,
X::MatNum;
dims::Int = 1,
estimation_mask::Option{<:AbstractMatrix{<:Bool}} = nothing,
active_mask::Option{<:AbstractMatrix{<:Bool}} = nothing,
kwargs...
) -> Matrix{<:Number}Compute the point-in-time regime-adjusted exponentially weighted variance series.
Row t holds what var 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 replaces the expanding-window fallback with one forward pass. It reads the cache after each observation, and it does not refit.
Arguments
ce: Regime-adjusted exponentially weighted variance estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.dims: Dimension along which the computation runs.estimation_mask: Optional boolean matrix of the size ofX. Where it is set, the regime state at observationireads only the assets whereestimation_mask[i, :](or[:, i]) istrue.active_mask: Optional boolean matrix of the size ofX. Where it is set, an asset that becomes inactive restarts its variance and its observation count from zero.kwargs: More keyword arguments, which the method ignores.
Validation
dims in (1, 2).- If
estimation_maskis notnothing,size(X) == size(estimation_mask). - If
active_maskis notnothing,size(X) == size(active_mask).
Returns
val::Matrix{<:Number}: Variance series, shaped as(T, N)ifdims == 1or(N, T)ifdims == 2. An asset with fewer thance.min_obsobservations at rowtisNaNthere.
Related
Base.copy — Method
copy(
x::RegimeAdjustedVarianceState
) -> Union{RegimeAdjustedVarianceState{Nothing}, RegimeAdjustedVarianceState{__T_ret_buffer} where __T_ret_buffer<:DataStructures.CircularBuffer}
Copies a RegimeAdjustedVarianceState, so the copy shares no array with the original.
This is the copy method of the AbstractPartialFitState interface, and partial_fit calls it before it folds. The method copies every array field, and it passes the two scalar fields through. It rebuilds the circular buffer of recent centred returns at the same capacity, and it copies each observation that the buffer holds. So a fold on the copy pushes into a buffer of its own.
This family answers copy and refuses merge_states. The two methods are independent. The merge asks whether two blocks fold into one. The copy asks only that a fold on one estimator leaves another estimator as it is.
Algorithm
- Rebuild the circular buffer at the capacity of
x.ret_buffer, and push a copy of each observation it holds. Takenothingwhenx.ret_bufferisnothing, which is the estimator that runs no HAC correction. - Name the constructor, and pass a copy of each array field and the two scalar fields unchanged. The table of bias factors and the buffer of lagged records are each
nothingor a container, anddeepcopycopies a container and returnsnothingunchanged.
Arguments
x: The cache to copy.
Returns
state::RegimeAdjustedVarianceState: A fresh cache, equal tox, whose arrays are fresh.
Related