Variance and standard deviation
The variance is used throughout the library, it can be used as part of the expected return and covariance estimation as well as in performance analysis and constraint generation. It is trivial to compute the standard deviation from the variance, so we provide those too.
PortfolioOptimisers.SimpleVariance Type
struct SimpleVariance{T1, T2, T3} <: AbstractVarianceEstimator
me::T1
w::T2
corrected::T3
endA flexible variance estimator for PortfolioOptimisers.jl supporting optional expected returns estimators, observation weights, and bias correction.
SimpleVariance enables users to specify an expected returns estimator (for mean-centering), optional observation weights, and whether to apply bias correction (Bessel's correction). This type is suitable for both unweighted and weighted variance estimation workflows.
Fields
me: Optional expected returns estimator. Ifnothing, the mean is not estimated.w: Optional observation weights. Ifnothing, the estimator is unweighted.corrected: Whether to apply Bessel's correction (unbiased variance).
Constructor
SimpleVariance(;
me::Option{<:AbstractExpectedReturnsEstimator} = SimpleExpectedReturns(),
w::Option{<:StatsBase.AbstractWeights} = nothing, corrected::Bool = true)Keyword arguments correspond to the fields above.
Validation
- If
wis notnothing,!isempty(w).
Examples
julia> SimpleVariance()
SimpleVariance
me ┼ SimpleExpectedReturns
│ w ┼ nothing
│ idx ┴ nothing
w ┼ nothing
corrected ┴ Bool: true
julia> SimpleVariance(; w = StatsBase.Weights([0.2, 0.3, 0.5]), corrected = false)
SimpleVariance
me ┼ SimpleExpectedReturns
│ w ┼ nothing
│ idx ┴ nothing
w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]
corrected ┴ Bool: falseRelated
std(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...)
PortfolioOptimisers.factory Method
factory(ve::SimpleVariance, w::StatsBase.AbstractWeights)Return a new SimpleVariance estimator with the specified observation weights.
Arguments
ve: The originalSimpleVarianceestimator to update.w: Observation weights to use in the new estimator.
Returns
ve::SimpleVariance: A newSimpleVarianceestimator with the same mean estimator and bias correction asve, but with the provided weights.
Details
Constructs a new
SimpleVarianceestimator with updated weights.The mean estimator is updated using
factory(ve.me, w)for consistency.The bias correction flag is preserved from the original estimator.
Related
Examples
julia> sv = SimpleVariance()
SimpleVariance
me ┼ SimpleExpectedReturns
│ w ┼ nothing
│ idx ┴ nothing
w ┼ nothing
corrected ┴ Bool: true
julia> svw = factory(sv, StatsBase.Weights([0.2, 0.3, 0.5]))
SimpleVariance
me ┼ SimpleExpectedReturns
│ w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]
│ idx ┴ nothing
w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]
corrected ┴ Bool: trueStatistics.std Method
Statistics.std(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)Compute the standard deviation using a SimpleVariance estimator for an array.
This method computes the standard deviation of the input array X using the configuration specified in ve, including optional mean-centering (via ve.me), observation weights (ve.w), and bias correction (ve.corrected). If a mean is not provided, it is estimated using the expected returns estimator in ve.me.
Arguments
ve: Variance estimator specifying the mean estimator, weights, and bias correction.X: Data array (vector or matrix) for which to compute the standard deviation.dims: Dimensions along which to perform the computation.mean: Optional mean value or vector for centering. If not provided, estimated usingve.me.kwargs...: Additional keyword arguments passed to the mean estimator.
Returns
sd::VecNum: Standard deviation vector ofX.
Examples
julia> sv = SimpleVariance()
SimpleVariance
me ┼ SimpleExpectedReturns
│ w ┼ nothing
│ idx ┴ nothing
w ┼ nothing
corrected ┴ Bool: true
julia> Xmat = [1.0 2.0; 3.0 4.0];
julia> std(sv, Xmat; dims = 1)
1×2 Matrix{Float64}:
1.41421 1.41421Related
sourceStatistics.std Method
Statistics.std(ve::SimpleVariance, X::VecNum; mean = nothing)Compute the standard deviation using a SimpleVariance estimator for a vector.
This method computes the standard deviation of the input vector X using the configuration specified in ve, including optional observation weights (ve.w) and bias correction (ve.corrected). If a mean is not nothing, it is used for centering; otherwise, the default mean is used.
Arguments
ve: Variance estimator specifying weights and bias correction.X: Data vector for which to compute the standard deviation.mean: Optional Mean value for centering. If not provided, the default mean is used.
Returns
sd::Number: Standard deviation ofX.
Examples
julia> sv = SimpleVariance()
SimpleVariance
me ┼ SimpleExpectedReturns
│ w ┼ nothing
│ idx ┴ nothing
w ┼ nothing
corrected ┴ Bool: true
julia> X = [1.0, 2.0, 3.0];
julia> std(sv, X)
1.0
julia> svw = SimpleVariance(; w = StatsBase.Weights([0.2, 0.3, 0.5]), corrected = false)
SimpleVariance
me ┼ SimpleExpectedReturns
│ w ┼ nothing
│ idx ┴ nothing
w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]
corrected ┴ Bool: false
julia> std(svw, X)
0.7810249675906654Related
std(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)var(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)
Statistics.var Method
Statistics.var(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)Compute the variance using a SimpleVariance estimator for an array.
This method computes the variance of the input array X using the configuration specified in ve, including optional mean-centering (via ve.me), observation weights (ve.w), and bias correction (ve.corrected). If a mean is not provided, it is estimated using the expected returns estimator in ve.me.
Arguments
ve: Variance estimator specifying the mean estimator, weights, and bias correction.X: Data array (vector or matrix) for which to compute the variance.dims: Dimensions along which to perform the computation.mean: Optional mean value or vector for centering. If not provided, estimated usingve.me.kwargs...: Additional keyword arguments passed to the mean estimator.
Returns
v::VecNum: Variance vector ofX.
Examples
julia> sv = SimpleVariance()
SimpleVariance
me ┼ SimpleExpectedReturns
│ w ┼ nothing
│ idx ┴ nothing
w ┼ nothing
corrected ┴ Bool: true
julia> Xmat = [1.0 2.0; 3.0 4.0];
julia> var(sv, Xmat; dims = 1)
1×2 Matrix{Float64}:
2.0 2.0Related
sourceStatistics.var Method
Statistics.var(ve::SimpleVariance, X::VecNum; mean = nothing)Compute the variance using a SimpleVariance estimator for a vector.
This method computes the variance of the input vector X using the configuration specified in ve, including optional observation weights (ve.w) and bias correction (ve.corrected). If a mean is not nothing, it is used for centering; otherwise, the default mean is used.
Arguments
ve: Variance estimator specifying weights and bias correction.X: Data vector for which to compute the variance.mean: Optional mean value for centering. If not provided, the default mean is used.
Returns
v::Number: Variance ofX.
Examples
julia> sv = SimpleVariance()
SimpleVariance
me ┼ SimpleExpectedReturns
│ w ┼ nothing
│ idx ┴ nothing
w ┼ nothing
corrected ┴ Bool: true
julia> X = [1.0, 2.0, 3.0];
julia> var(sv, X)
1.0
julia> svw = SimpleVariance(; w = StatsBase.Weights([0.2, 0.3, 0.5]), corrected = false)
SimpleVariance
me ┼ SimpleExpectedReturns
│ w ┼ nothing
│ idx ┴ nothing
w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]
corrected ┴ Bool: false
julia> var(svw, X)
0.61Related
std(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)var(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)