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} <: AbstractVarianceEstimatorComputes 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 isnothing, a matrix method uses aSimpleExpectedReturns.
w: Observation weights vectorobservations × 1.
corrected: Iftrue, the estimator applies the Bessel correction.StatisticsorStatsBaseapplies it, with the divisor that each weight type defines.
cvg: OptionalCoveragePolicy.nothingselects 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 isNaNin 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 whereadmitsadmits it.
cache: Optional partial-fit state. It isnothinguntilpartial_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.factorykeeps it unchanged, because a factory call resolves the configuration, not the sample.port_opt_viewcopies the entries of the selected assets into a new state, so the viewed estimator holds the state of those assets alone.obs_weights_viewremoves 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) -> SimpleVarianceKeywords correspond to the struct's fields.
Validation
- If
wis notnothing,!isempty(w). - If
cvgis aCoveragePolicy,meisnothingor aSimpleExpectedReturns, throughassert_coverage_fittable. The available-case fit centres each asset on the mean of its own observations, and on no other centre. With now, the fit folds, someholds no weights. Withw, the fit centres on the weighted mean, andwreplaces the weights ofme. AnArgumentErroris thrown otherwise. corrected = trueneeds a weight type that has a bias correction. The constructor does not check it, andStatsBasethrows anArgumentErrorat the fit. See the bias-correction bullet ofvar(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...).
Propagated parameters
factory passes its arguments on to these @fprop fields:
me: Recursively updated throughfactory.w: Replaced with the incomingObsWeights.cache: Carried unchanged throughfactory.
View parameters
port_opt_view takes the view of these @vprop fields at the selected indices:
me: Recursively viewed throughport_opt_view.cache: Sliced to the selected assets throughport_opt_view.
Observation weight parameters
obs_weights_view indexes these fields to the selected observations:
me: Recursively indexed throughobs_weights_view.w: Indexed to the selected observations throughobs_weights_view.cache: Removed byobs_weights_view, because a state has no slice on the observation axis.
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: falseRelated
AbstractVarianceEstimatorAbstractExpectedReturnsEstimatorSimpleExpectedReturnsStatsBase.AbstractWeightsstd(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)std(ve::SimpleVariance, X::VecNum; mean = nothing, kwargs...)var(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)var(ve::SimpleVariance, X::VecNum; mean = nothing)SimpleVarianceStatepartial_fit!factoryport_opt_viewobs_weights_view
Statistics.std — Method
Statistics.std(
ve::SimpleVariance,
X::MatNum;
dims::Int = 1,
mean = nothing,
kwargs...,
) -> ArrNumCompute 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
- Check that
dimsis1or2. - When
meanisnothing, compute the centring vectormuwithve.me, afterfactorywritesve.winto it. Otherwise takemufrommean. - Get the observation weights
wfromve.wandX. - When
wisnothing, take the unweighted standard deviation ofXalongdims, centred onmu. - Otherwise take the standard deviation of
Xweighted bywalongdims, centred onmu.
$\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 matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.dims: Dimension along which the computation runs.mean: Optional precomputed centre of the data.kwargs...: More keyword arguments for the mean estimatorve.meand forget_observation_weights.
Validation
dims in (1, 2).corrected = trueneeds a weight type that has a bias correction. A plainStatsBase.Weightshas none, andStatsBasethrows anArgumentError.
Returns
sd::ArrNum: Standard deviation vector ofX, 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.41421Related
Statistics.std — Method
std(
ve::SimpleVariance{Nothing},
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}};
dims,
mean,
kwargs...
) -> Any
This is the SimpleVariance{Nothing} method of std(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...). When the caller gives no mean, it computes the mean with a SimpleExpectedReturns, because the me field is nothing.
Statistics.std — Method
Statistics.std(
ve::SimpleVariance,
X::VecNum;
mean = nothing
) -> NumberCompute 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
- Get the observation weights
wfromve.wandX. - When
wisnothing, take the unweighted standard deviation ofX, centred onmean. - Otherwise take the standard deviation of
Xweighted byw, centred onmean.
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 vectorobservations × 1.mean: Optional precomputed centre of the data.
Validation
corrected = trueneeds a weight type that has a bias correction. A plainStatsBase.Weightshas none, andStatsBasethrows anArgumentError.
Returns
sd::Number: Standard deviation ofX.
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.7810249675906654Related
Statistics.var — Method
Statistics.var(
ve::SimpleVariance,
X::MatNum;
dims::Int = 1,
mean = nothing,
kwargs...
) -> ArrNumCompute 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 = falsegives $c = 0$ for all types.corrected = truegives $c = 1$ forStatsBase.FrequencyWeights, $c = \sum_t w_t^2 / \sum_t w_t$ forStatsBase.AnalyticWeightsand $c = \sum_t w_t / T$ forStatsBase.ProbabilityWeights.
Algorithm
- Check that
dimsis1or2. - When
meanisnothing, compute the centring vectormuwithve.me, afterfactorywritesve.winto it. Otherwise takemufrommean. - Get the observation weights
wfromve.wandX. - When
wisnothing, take the unweighted variance ofXalongdims, centred onmu. - Otherwise take the variance of
Xweighted bywalongdims, centred onmu.
$\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 matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.dims: Dimension along which the computation runs.mean: Optional precomputed centre of the data.kwargs...: More keyword arguments for the mean estimatorve.meand forget_observation_weights.
Validation
dims in (1, 2).corrected = trueneeds a weight type that has a bias correction. A plainStatsBase.Weightshas none, andStatsBasethrows anArgumentError.
Returns
vr::ArrNum: Variance vector ofX, 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.0Related
Statistics.var — Method
var(
ve::SimpleVariance{Nothing},
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}};
dims,
mean,
kwargs...
) -> Any
This is the SimpleVariance{Nothing} method of var(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...). When the caller gives no mean, it computes the mean with a SimpleExpectedReturns, because the me field is nothing.
Statistics.var — Method
Statistics.var(
ve::SimpleVariance,
X::VecNum;
mean = nothing
) -> NumberCompute 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
- Get the observation weights
wfromve.wandX. - When
wisnothing, take the unweighted variance ofX, centred onmean. - Otherwise take the variance of
Xweighted byw, centred onmean.
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 vectorobservations × 1.mean: Optional precomputed centre of the data.
Validation
corrected = trueneeds a weight type that has a bias correction. A plainStatsBase.Weightshas none, andStatsBasethrows anArgumentError.
Returns
vr::Number: Variance ofX.
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.61Related
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
- Refuse an observation whose length is not the number of assets that the state describes.
- Add one to the count.
- Take the deviation
dof the observation from the mean before the fold. - Add
d, divided by the new count, tomuin place. - Add
dtimes the deviation from the mean after the fold toM, in place. - Build the state with the new count through its positional constructor, and return it.
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
- Read the buffer out of the
cachefield withassert_sample_buffer, which refuses an estimator that was never wrapped inOnline. - 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. - Rebuild the estimator with the new buffer in
cachethroughrebuild_struct, and return it.
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 the computation runs.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.iv: The implied volatilities, on the same terms asactive_mask.ivpa: The implied volatility risk premium adjustment, ornothing.
Validation
estcarries aSampleBufferState. AnArgumentErroris thrown otherwise.- The masks and
iv, when they are notnothing, have the shape ofX. ADimensionMismatchis thrown otherwise. - A buffer that holds observations receives the masks that it records, and it receives
ivif and only if it records implied volatilities. AnArgumentErroris thrown otherwise. ivpaequals the adjustment that the first append pinned. AnArgumentErroris 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{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
- Read the buffer out of the
cachefield withassert_sample_buffer, which refuses an estimator that was never wrapped inOnline. - 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. - Rebuild the estimator with the new buffer in
cachethroughrebuild_struct, and return it.
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 the computation runs.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.iv: The implied volatilities, on the same terms asactive_mask.ivpa: The implied volatility risk premium adjustment, ornothing.
Validation
estcarries aSampleBufferState. AnArgumentErroris thrown otherwise.- The masks and
iv, when they are notnothing, have the shape ofX. ADimensionMismatchis thrown otherwise. - A buffer that holds observations receives the masks that it records, and it receives
ivif and only if it records implied volatilities. AnArgumentErroris thrown otherwise. ivpaequals the adjustment that the first append pinned. AnArgumentErroris thrown otherwise.dims in (1, 2).
Returns
est: The estimator, with itscachefield rebound to the buffer after the last observation.
Related
Statistics.var — Method
Statistics.var(
ve::SimpleVariance,
state::SimpleVarianceState
) -> VecNum
Statistics.var(
ve::SimpleVariance
) -> VecNumRead 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.correctedistrue, and zero otherwise.
Algorithm
- Refuse a configuration that no incremental fit can reproduce, with
assert_partial_fittable. - Take the divisor
n - c. Return a vector ofNaNwhen the divisor is less than one.min_obsreads an asset with too few observations in the same way. - Otherwise divide the accumulator by the divisor.
Arguments
ve: Variance estimator.state: The state to read.
Validation
veholds no observation weights. AnArgumentErroris thrown otherwise.ve.meisnothing, or aSimpleExpectedReturnsthat holds no observation weights. AnArgumentErroris thrown otherwise.ve.cacheis notnothing, for the one-argument method. AnArgumentErroris thrown otherwise.
Returns
vr::VecNum: The per-asset variance of the fit,assets × 1, withNaNwhere 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.0Related
Statistics.std — Method
Statistics.std(ve::SimpleVariance, state::SimpleVarianceState) -> VecNumRead 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, withNaNwhere the state holds too few observations.
Related
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
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
- Refuse the pair with
assert_mergeable_states. - 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. - Otherwise, fold the mean and the accumulator of each asset with the per-asset counts
nuof 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
aandbpassassert_mergeable_states.
Returns
state::SimpleVarianceState: The state of the two blocks, as if one fit read them as one block.
Related
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
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
- Refuse an observation whose length is not the number of assets that the state describes.
- Read the valid assets, and the assets that became inactive at this observation, with
coverage_valid. - Apply the fold-time rule of the coverage algorithm with
fold_inactive!. - Fold the return of each valid asset into the count, the mean and the accumulator of that asset.
- 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, ornothing.
Validation
length(x)is the number of assets that the state describes. ADimensionMismatchis thrown otherwise.
Returns
state::SimpleVarianceState: The state after the observation.
Related
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