Simple variance

The library uses the variance of each asset in several places: in some expected returns estimators, in covariance estimation, in performance analysis and in constraint generation. The estimators below compute the variance, and the standard deviation, which is its square root.

PortfolioOptimisers.SimpleVariance — Type
struct SimpleVariance{__T_me, __T_w, __T_corrected, __T_cvg, __T_cache} <: AbstractVarianceEstimator

Computes the marginal variance and standard deviation, with optional observation weights and an optional bias correction.

me centres the data when the caller gives no mean. w weights the observations, and corrected selects the bias correction. Only the matrix methods use me. The vector methods let Statistics centre the data.

w weights the whole estimate, so it weights the centre and the deviations. The matrix methods send me through factory, which replaces the weights of me with w. Statistics centres a weighted vector on its weighted mean. The two paths give the same number for the same data, and w replaces the weights that me holds. Pass mean for a centre that w does not describe.

Fields

  • me: Optional expected returns estimator. A vector method does not use it. If it is nothing, a matrix method uses a SimpleExpectedReturns.
  • w: Observation weights vector observations × 1.
  • corrected: If true, the estimator applies the Bessel correction. Statistics or StatsBase applies it, with the divisor that each weight type defines.
  • cvg: Optional CoveragePolicy. nothing selects the reduce-and-expand path of the Coverage Universe. On that path, an asset that is not finite or not active at an observation of the window is NaN in all of the result. A policy replaces that path with available-case estimation. The fit of each cell reads the observations at which all the assets of that cell are finite and active, and each cell has its own denominator. An asset is in the result where admits admits it.
  • cache: Optional partial-fit state. It is nothing until partial_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. factory keeps it unchanged, because a factory call resolves the configuration, not the sample. port_opt_view copies the entries of the selected assets into a new state, so the viewed estimator holds the state of those assets alone. obs_weights_view removes 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

SimpleVariance(;    me::Option{<:AbstractExpectedReturnsEstimator} = SimpleExpectedReturns(),    w::Option{<:ObsWeights} = nothing,    corrected::Bool = true,    cvg::Option{<:CoveragePolicy} = nothing,    cache::Option{<:AbstractPartialFitState} = nothing) -> SimpleVariance

Keywords correspond to the struct's fields.

Validation

Propagated parameters

factory passes its arguments on to these @fprop fields:

  • me: Recursively updated through factory.
  • w: Replaced with the incoming ObsWeights.
  • cache: Carried unchanged through factory.

View parameters

port_opt_view takes the view of these @vprop fields at the selected indices:

Observation weight parameters

obs_weights_view indexes these fields to the selected observations:

Examples

julia> SimpleVariance()SimpleVariance         me ┼ SimpleExpectedReturns            │   w ┴ nothing          w ┼ nothing  corrected ┴ Bool: truejulia> SimpleVariance(; w = StatsBase.Weights([0.2, 0.3, 0.5]), corrected = false)SimpleVariance         me ┼ SimpleExpectedReturns            │   w ┴ nothing          w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]  corrected ┴ Bool: false

Related

source
Statistics.std — Method
Statistics.std(
    ve::SimpleVariance,
    X::MatNum;
    dims::Int = 1,
    mean = nothing,
    kwargs...,
) -> ArrNum

Compute the standard deviation of a matrix with a SimpleVariance estimator.

The method computes the standard deviation of each asset in X, as ve configures it.

Mathematical definition

\[\begin{align} \hat{\sigma}_j &= \sqrt{\hat{\sigma}^2_j}\,. \end{align}\]

Where:

  • $\hat{\sigma}_j$: The estimated standard deviation of asset $j$.
  • $\hat{\sigma}^2_j$: Estimated variance of asset $j$.

var(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...) defines $\hat{\sigma}^2_j$ in each of the four cases that ve.w and ve.corrected select.

Algorithm

  1. Check that dims is 1 or 2.
  2. When mean is nothing, compute the centring vector mu with ve.me, after factory writes ve.w into it. Otherwise take mu from mean.
  3. Get the observation weights w from ve.w and X.
  4. When w is nothing, take the unweighted standard deviation of X along dims, centred on mu.
  5. Otherwise take the standard deviation of X weighted by w along dims, centred on mu.

$\hat{\mu}_j$ comes from ve.me, and ve.w gets to ve.me through factory. A SimpleVariance with a w applies the same weights to the centre and to the squared deviations. A vector and its one-column matrix give the same number. Pass mean for any other centre.

Arguments

  • ve: Variance 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 the computation runs.
  • mean: Optional precomputed centre of the data.
  • kwargs...: More keyword arguments for the mean estimator ve.me and for get_observation_weights.

Validation

  • dims in (1, 2).
  • corrected = true needs a weight type that has a bias correction. A plain StatsBase.Weights has none, and StatsBase throws an ArgumentError.

Returns

  • sd::ArrNum: Standard deviation vector of X, with the shape that matches the dimension of the computation.

Examples

julia> sv = SimpleVariance()SimpleVariance         me ┼ SimpleExpectedReturns            │   w ┴ nothing          w ┼ nothing  corrected ┴ Bool: truejulia> Xmat = [1.0 2.0; 3.0 4.0];julia> std(sv, Xmat; dims = 1)1×2 Matrix{Float64}: 1.41421  1.41421

Related

source
Statistics.std — Method
Statistics.std(
    ve::SimpleVariance,
    X::VecNum;
    mean = nothing
) -> Number

Compute the standard deviation of a vector with a SimpleVariance estimator.

The method computes the standard deviation of the series X, as ve configures it.

Mathematical definition

var(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...) defines the variance in each of the four cases that ve.w and ve.corrected select. This method returns its square root.

Algorithm

  1. Get the observation weights w from ve.w and X.
  2. When w is nothing, take the unweighted standard deviation of X, centred on mean.
  3. Otherwise take the standard deviation of X weighted by w, centred on mean.

The vector methods ignore ve.me. A mean of nothing goes to Statistics.std, which centres on the mean of X. That mean is the weighted mean when w is not nothing. The matrix methods get the centre from ve.me with the same ve.w, so the two paths give the same number for the same data.

Arguments

  • ve: Variance estimator.
  • X: Data vector observations × 1.
  • mean: Optional precomputed centre of the data.

Validation

  • corrected = true needs a weight type that has a bias correction. A plain StatsBase.Weights has none, and StatsBase throws an ArgumentError.

Returns

  • sd::Number: Standard deviation of X.

Examples

julia> sv = SimpleVariance()SimpleVariance         me ┼ SimpleExpectedReturns            │   w ┴ nothing          w ┼ nothing  corrected ┴ Bool: truejulia> X = [1.0, 2.0, 3.0];julia> std(sv, X)1.0julia> svw = SimpleVariance(; w = StatsBase.Weights([0.2, 0.3, 0.5]), corrected = false)SimpleVariance         me ┼ SimpleExpectedReturns            │   w ┴ nothing          w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]  corrected ┴ Bool: falsejulia> std(svw, X)0.7810249675906654

Related

source
Statistics.var — Method
Statistics.var(
    ve::SimpleVariance,
    X::MatNum;
    dims::Int = 1,
    mean = nothing,
    kwargs...
) -> ArrNum

Compute the variance of a matrix with a SimpleVariance estimator.

The method computes the variance of each asset in X, as ve configures it.

Mathematical definition

Unweighted, corrected = true:

\[\begin{align} \hat{\sigma}^2_j &= \frac{1}{T-1} \sum_{t=1}^{T} (r_{tj} - \hat{\mu}_j)^2\,. \end{align}\]

Unweighted, corrected = false:

\[\begin{align} \hat{\sigma}^2_j &= \frac{1}{T} \sum_{t=1}^{T} (r_{tj} - \hat{\mu}_j)^2\,. \end{align}\]

Weighted:

\[\begin{align} \hat{\sigma}^2_j &= \frac{\sum_{t=1}^{T} w_t (r_{tj} - \hat{\mu}_j)^2}{\sum_{t=1}^{T} w_t - c}\,. \end{align}\]

Where:

  • $\hat{\sigma}^2_j$: Estimated variance of asset $j$.
  • $r_{tj}$: Return of asset $j$ at time $t$.
  • $\hat{\mu}_j$: Estimated mean of asset $j$.
  • $T$: Number of observations.
  • $w_{t}$: Observation weight of observation $t$.
  • $c$: Bias correction of the weighted denominator. The type of the weights sets it, and the estimator does not. corrected = false gives $c = 0$ for all types. corrected = true gives $c = 1$ for StatsBase.FrequencyWeights, $c = \sum_t w_t^2 / \sum_t w_t$ for StatsBase.AnalyticWeights and $c = \sum_t w_t / T$ for StatsBase.ProbabilityWeights.

Algorithm

  1. Check that dims is 1 or 2.
  2. When mean is nothing, compute the centring vector mu with ve.me, after factory writes ve.w into it. Otherwise take mu from mean.
  3. Get the observation weights w from ve.w and X.
  4. When w is nothing, take the unweighted variance of X along dims, centred on mu.
  5. Otherwise take the variance of X weighted by w along dims, centred on mu.

$\hat{\mu}_j$ comes from ve.me, and ve.w gets to ve.me through factory. A SimpleVariance with a w applies the same weights to the centre and to the squared deviations. A vector and its one-column matrix give the same number. Pass mean for any other centre.

Arguments

  • ve: Variance 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 the computation runs.
  • mean: Optional precomputed centre of the data.
  • kwargs...: More keyword arguments for the mean estimator ve.me and for get_observation_weights.

Validation

  • dims in (1, 2).
  • corrected = true needs a weight type that has a bias correction. A plain StatsBase.Weights has none, and StatsBase throws an ArgumentError.

Returns

  • vr::ArrNum: Variance vector of X, with the shape that matches the dimension of the computation.

Examples

julia> sv = SimpleVariance()SimpleVariance         me ┼ SimpleExpectedReturns            │   w ┴ nothing          w ┼ nothing  corrected ┴ Bool: truejulia> Xmat = [1.0 2.0; 3.0 4.0];julia> var(sv, Xmat; dims = 1)1×2 Matrix{Float64}: 2.0  2.0

Related

source
Statistics.var — Method
Statistics.var(
    ve::SimpleVariance,
    X::VecNum;
    mean = nothing
) -> Number

Compute the variance of a vector with a SimpleVariance estimator.

The method computes the variance of the series X, as ve configures it.

Mathematical definition

var(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...) defines the variance in each of the four cases that ve.w and ve.corrected select. This method returns it for one series.

Algorithm

  1. Get the observation weights w from ve.w and X.
  2. When w is nothing, take the unweighted variance of X, centred on mean.
  3. Otherwise take the variance of X weighted by w, centred on mean.

The vector methods ignore ve.me. A mean of nothing goes to Statistics.var, which centres on the mean of X. That mean is the weighted mean when w is not nothing. The matrix methods get the centre from ve.me with the same ve.w, so the two paths give the same number for the same data.

Arguments

  • ve: Variance estimator.
  • X: Data vector observations × 1.
  • mean: Optional precomputed centre of the data.

Validation

  • corrected = true needs a weight type that has a bias correction. A plain StatsBase.Weights has none, and StatsBase throws an ArgumentError.

Returns

  • vr::Number: Variance of X.

Examples

julia> sv = SimpleVariance()SimpleVariance         me ┼ SimpleExpectedReturns            │   w ┴ nothing          w ┼ nothing  corrected ┴ Bool: truejulia> X = [1.0, 2.0, 3.0];julia> var(sv, X)1.0julia> svw = SimpleVariance(; w = StatsBase.Weights([0.2, 0.3, 0.5]), corrected = false)SimpleVariance         me ┼ SimpleExpectedReturns            │   w ┴ nothing          w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]  corrected ┴ Bool: falsejulia> var(svw, X)0.61

Related

source

Incremental fit

Like the sample mean, the sample variance updates with each new block of observations. partial_fit! stores the running state in the cache field of the estimator it returns, and var(ve) and std(ve) compute the estimate from that state, with no data.

PortfolioOptimisers.partial_fit! — Method
partial_fit!(
    state::SimpleVarianceState,
    x::AbstractVector{<:Union{Number, AbstractJuMPScalar}}
) -> SimpleVarianceState

The SimpleVarianceState method of partial_fit! folds one observation into the count, the running mean and the per-asset accumulator.

Mathematical definition

\[\begin{align} n &\leftarrow n + 1\\ \boldsymbol{d} &= \boldsymbol{x} - \boldsymbol{\mu}\\ \boldsymbol{\mu} &\leftarrow \boldsymbol{\mu} + \frac{\boldsymbol{d}}{n}\\ \boldsymbol{M} &\leftarrow \boldsymbol{M} + \boldsymbol{d} \odot (\boldsymbol{x} - \boldsymbol{\mu})\, . \end{align}\]

Where:

  • $n$: The observation count.
  • $\boldsymbol{x}$: The observation.
  • $\boldsymbol{\mu}$: The running mean.
  • $\boldsymbol{d}$: The deviation of the observation from the mean before the fold.
  • $\boldsymbol{M}$: The running per-asset accumulator.

The last line reads $\boldsymbol{\mu}$ after the third line changed it, but $\boldsymbol{d}$ reads it before the change. This asymmetry is the one of Welford, and it keeps the accumulator non-negative.

Algorithm

  1. Refuse an observation whose length is not the number of assets that the state describes.
  2. Add one to the count.
  3. Take the deviation d of the observation from the mean before the fold.
  4. Add d, divided by the new count, to mu in place.
  5. Add d times the deviation from the mean after the fold to M, in place.
  6. Build the state with the new count through its positional constructor, and return it.
source
PortfolioOptimisers.partial_fit! — Method
partial_fit!(
    est::Union{AbstractEstimator, CovarianceEstimator},
    X::Union{AbstractMatrix{<:Union{Number, AbstractJuMPScalar}}, AbstractVector{<:Union{Number, AbstractJuMPScalar}}};
    dims,
    active_mask,
    estimation_mask,
    iv,
    ivpa
) -> Covariance{<:AbstractExpectedReturnsEstimator, <:CovarianceEstimator, <:AbstractMomentAlgorithm}

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 call with no data 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. The implied volatilities and ivpa go through the wrapper in the same way, so a wrapped estimator that reads them, such as an ImpliedVolatility inside a PortfolioOptimisersCovariance, gives the answer of a batch fit over the same rows.

Algorithm

  1. Read the buffer out of the cache field with assert_sample_buffer, which refuses an estimator that was never wrapped in Online.
  2. Fold a matrix, its masks and its implied volatilities through the block arm of partial_fit!, and a vector, its masks and its implied volatilities through the single-observation arm.
  3. Rebuild the estimator with the new buffer in cache through rebuild_struct, and return it.

Arguments

  • est: Estimator whose buffer is folded forward.
  • X: Observations to fold. A matrix holds one observation per row when dims == 1, and one per column when dims == 2. A vector is a single observation across the assets, and dims is ignored.
  • dims: Dimension along which the computation runs.
  • active_mask: The active mask of the block, of the shape of X, or of one entry per asset when X is one observation, or nothing.
  • estimation_mask: The estimation mask, on the same terms as active_mask.
  • iv: The implied volatilities, on the same terms as active_mask.
  • ivpa: The implied volatility risk premium adjustment, or nothing.

Validation

  • est carries a SampleBufferState. An ArgumentError is thrown otherwise.
  • The masks and iv, when they are not nothing, have the shape of X. A DimensionMismatch is thrown otherwise.
  • A buffer that holds observations receives the masks that it records, and it receives iv if and only if it records implied volatilities. An ArgumentError is thrown otherwise.
  • ivpa equals the adjustment that the first append pinned. An ArgumentError is thrown otherwise.
  • dims in (1, 2).

Returns

  • est: The estimator, with its cache field rebound to the buffer after the last observation.

Related

source
PortfolioOptimisers.partial_fit! — Method
partial_fit!(
    est::Union{AbstractEstimator, CovarianceEstimator},
    X::Union{AbstractMatrix{<:Union{Number, AbstractJuMPScalar}}, AbstractVector{<:Union{Number, AbstractJuMPScalar}}};
    dims,
    active_mask,
    estimation_mask,
    iv,
    ivpa
) -> Covariance{<:AbstractExpectedReturnsEstimator, <:CovarianceEstimator, <:AbstractMomentAlgorithm}

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 call with no data 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. The implied volatilities and ivpa go through the wrapper in the same way, so a wrapped estimator that reads them, such as an ImpliedVolatility inside a PortfolioOptimisersCovariance, gives the answer of a batch fit over the same rows.

Algorithm

  1. Read the buffer out of the cache field with assert_sample_buffer, which refuses an estimator that was never wrapped in Online.
  2. Fold a matrix, its masks and its implied volatilities through the block arm of partial_fit!, and a vector, its masks and its implied volatilities through the single-observation arm.
  3. Rebuild the estimator with the new buffer in cache through rebuild_struct, and return it.

Arguments

  • est: Estimator whose buffer is folded forward.
  • X: Observations to fold. A matrix holds one observation per row when dims == 1, and one per column when dims == 2. A vector is a single observation across the assets, and dims is ignored.
  • dims: Dimension along which the computation runs.
  • active_mask: The active mask of the block, of the shape of X, or of one entry per asset when X is one observation, or nothing.
  • estimation_mask: The estimation mask, on the same terms as active_mask.
  • iv: The implied volatilities, on the same terms as active_mask.
  • ivpa: The implied volatility risk premium adjustment, or nothing.

Validation

  • est carries a SampleBufferState. An ArgumentError is thrown otherwise.
  • The masks and iv, when they are not nothing, have the shape of X. A DimensionMismatch is thrown otherwise.
  • A buffer that holds observations receives the masks that it records, and it receives iv if and only if it records implied volatilities. An ArgumentError is thrown otherwise.
  • ivpa equals the adjustment that the first append pinned. An ArgumentError is thrown otherwise.
  • dims in (1, 2).

Returns

  • est: The estimator, with its cache field rebound to the buffer after the last observation.

Related

source
Statistics.var — Method
Statistics.var(
    ve::SimpleVariance,
    state::SimpleVarianceState
) -> VecNum
Statistics.var(
    ve::SimpleVariance
) -> VecNum

Read the variance of an incremental fit from a SimpleVarianceState.

The two-argument method reads a state that the caller holds. The one-argument method reads the state in the cache field of ve. Both methods return the per-asset variance as a vector, assets × 1. The batch method over a matrix returns a row when dims = 1.

Mathematical definition

\[\begin{align} \hat{\sigma}^2_j &= \frac{M_j}{n - c}\,. \end{align}\]

Where:

  • $\hat{\sigma}^2_j$: Estimated variance of asset $j$.
  • $M_j$: The running accumulator of asset $j$.
  • $n$: The observation count.
  • $c$: One when ve.corrected is true, and zero otherwise.

Algorithm

  1. Refuse a configuration that no incremental fit can reproduce, with assert_partial_fittable.
  2. Take the divisor n - c. Return a vector of NaN when the divisor is less than one. min_obs reads an asset with too few observations in the same way.
  3. Otherwise divide the accumulator by the divisor.

Arguments

  • ve: Variance estimator.
  • state: The state to read.

Validation

  • ve holds no observation weights. An ArgumentError is thrown otherwise.
  • ve.me is nothing, or a SimpleExpectedReturns that holds no observation weights. An ArgumentError is thrown otherwise.
  • ve.cache is not nothing, for the one-argument method. An ArgumentError is thrown otherwise.

Returns

  • vr::VecNum: The per-asset variance of the fit, assets × 1, with NaN where the state holds too few observations.

Examples

julia> ve = foldl(partial_fit!, eachrow([1.0 2.0; 3.0 4.0]); init = SimpleVariance());julia> var(ve)2-element Vector{Float64}: 2.0 2.0

Related

source
Statistics.std — Method
Statistics.std(ve::SimpleVariance, state::SimpleVarianceState) -> VecNum

Read the standard deviation of an incremental fit from a SimpleVarianceState.

std(ve) with no data reads the state in the cache field of ve through this method.

Arguments

  • ve: Variance estimator.
  • state: The state to read.

Returns

  • sd::VecNum: The per-asset standard deviation of the fit, assets × 1, with NaN where the state holds too few observations.

Related

source
PortfolioOptimisers.port_opt_view — Method
port_opt_view(
    x::SimpleVarianceState,
    i,
    args...
) -> Union{SimpleVarianceState{_A, _B, _C, Nothing} where {_A, _B, _C}, SimpleVarianceState{_A, _B, _C, __T_cvg} where {_A, _B, _C, __T_cvg<:CoverageCounts}}

Slices a SimpleVarianceState to the selected assets.

The Welford accumulator of one asset reads only the observations of that asset, so the slice of the state is the state of the sliced universe, entry for entry. All the assets share the count, so the method uses it without change. The slice copies by index and does not use a view. With a view, a later partial_fit! on the viewed estimator writes into the arrays of the original estimator.

Arguments

  • x: The state to slice.
  • i: The index or the indices of the assets to keep.
  • args...: More positional arguments, which the method ignores.

Returns

  • state::SimpleVarianceState: The state of the same sample over the selected assets.

Related

source
PortfolioOptimisers.merge_states — Method
merge_states(
    a::SimpleVarianceState,
    b::SimpleVarianceState
) -> Union{SimpleVarianceState{_A, _B, _C, Nothing} where {_A, _B, _C}, SimpleVarianceState{_A, _B, _C, __T_cvg} where {_A, _B, _C, __T_cvg<:(CoverageCounts{_A, Nothing} where _A)}}

Folds two SimpleVarianceState fitted on disjoint blocks into the state of the concatenated block.

Algorithm

  1. Refuse the pair with assert_mergeable_states.
  2. If one of the two states holds no coverage counts, fold the counts, the means and the accumulators with chan_merge. Its element-wise method reads a per-asset accumulator.
  3. Otherwise, fold the mean and the accumulator of each asset with the per-asset counts nu of the two states. An asset with no observation in the two blocks keeps a mean and an accumulator of zero. Merge the coverage counts too.

Arguments

  • a: The state of the first block of observations.
  • b: The state of the second block of observations.

Validation

Returns

  • state::SimpleVarianceState: The state of the two blocks, as if one fit read them as one block.

Related

source

Available-case fit

With a CoveragePolicy in its cvg field, the variance of each asset uses only the observations where that asset is finite and active. Without a policy, it fits on the coverage universe, the assets that are finite and active at every observation. PortfolioOptimisers.coverage_variance chooses between the two fits.

PortfolioOptimisers.partial_fit! — Method
partial_fit!(
    state::SimpleVarianceState,
    x::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
    _::Nothing,
    active_mask::Union{Nothing, AbstractVector{<:Bool}}
) -> SimpleVarianceState

This is the Nothing method of the coverage arm of partial_fit! for a SimpleVarianceState. An estimator that holds no CoveragePolicy folds through partial_fit!(state::SimpleVarianceState, x::VecNum). A plain state holds one count for all the assets, so it cannot skip an inactive cell. inactive_as_nonfinite writes NaN in that cell, and the variance of the asset is then NaN. The Asset Panel method gives the same NaN for an asset outside the Coverage Universe of the same rows.

Related

source
PortfolioOptimisers.partial_fit! — Method
partial_fit!(
    state::SimpleVarianceState,
    x::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
    cvg::CoveragePolicy,
    active_mask::Union{Nothing, AbstractVector{<:Bool}}
) -> SimpleVarianceState

This is the CoveragePolicy method of the coverage arm of partial_fit! for a SimpleVarianceState. It folds one observation into the count, the running mean and the accumulator of each asset. Each asset reads only its own observations.

Mathematical definition

\[\begin{align} \nu_j &\leftarrow \nu_j + 1\\ d_j &= r_{tj} - \mu_j\\ \mu_j &\leftarrow \mu_j + \frac{d_j}{\nu_j}\\ M_j &\leftarrow M_j + d_j (r_{tj} - \mu_j)\, , \end{align}\]

for each asset $j$ that is finite and active at observation $t$. The method applies no line to an asset that is not finite and active. Where:

  • $\nu_j$: The number of observations at which asset $j$ is finite and active.
  • $r_{tj}$: Return of asset $j$ at time $t$.
  • $\mu_j$: The running mean of asset $j$.
  • $d_j$: The deviation of asset $j$ from its mean before the fold.
  • $M_j$: The running second-moment accumulator of asset $j$.

The last line reads $\mu_j$ after the third line changed it, which is the asymmetry of Welford. The accumulator is exact for each asset, and a skipped observation has no effect on the asset.

Algorithm

  1. Refuse an observation whose length is not the number of assets that the state describes.
  2. Read the valid assets, and the assets that became inactive at this observation, with coverage_valid.
  3. Apply the fold-time rule of the coverage algorithm with fold_inactive!.
  4. Fold the return of each valid asset into the count, the mean and the accumulator of that asset.
  5. Update the per-asset records with coverage_step!. Add one to the observation count, and return the state.

Arguments

  • state: The state to fold into. The method changes it in place.
  • x: One observation, with one entry for each asset.
  • cvg: The policy that the estimator holds.
  • active_mask: The active mask of the Asset Panel at this observation, or nothing.

Validation

  • length(x) is the number of assets that the state describes. A DimensionMismatch is thrown otherwise.

Returns

  • state::SimpleVarianceState: The state after the observation.

Related

source
PortfolioOptimisers.fold_inactive! — Method
fold_inactive!(
    _::ResetCoverage,
    state::SimpleVarianceState,
    ni::AbstractVector{<:Bool}
) -> SimpleVarianceState

This is the SimpleVarianceState method of fold_inactive! under ResetCoverage. It sets to zero the count, the centre, the running mean and the accumulator of each asset that became inactive at this observation. A relisted asset then starts cold. The centre of a per-asset state is nothing, because its mu is already the centre of the cell. coverage_reset! does nothing to a nothing.

Related

source