Skip to content
11

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
julia
struct SimpleVariance{T1, T2, T3} <: AbstractVarianceEstimator
    me::T1
    w::T2
    corrected::T3
end

A 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. If nothing, the mean is not estimated.

  • w: Optional observation weights. If nothing, the estimator is unweighted.

  • corrected: Whether to apply Bessel's correction (unbiased variance).

Constructor

julia
SimpleVariance(;
               me::Option{<:AbstractExpectedReturnsEstimator} = SimpleExpectedReturns(),
               w::Option{<:StatsBase.AbstractWeights} = nothing, corrected::Bool = true)

Keyword arguments correspond to the fields above.

Validation

  • If w is not nothing, !isempty(w).

Examples

julia
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: false

Related

source
PortfolioOptimisers.factory Method
julia
factory(ve::SimpleVariance, w::StatsBase.AbstractWeights)

Return a new SimpleVariance estimator with the specified observation weights.

Arguments

  • ve: The original SimpleVariance estimator to update.

  • w: Observation weights to use in the new estimator.

Returns

  • ve::SimpleVariance: A new SimpleVariance estimator with the same mean estimator and bias correction as ve, but with the provided weights.

Details

  • Constructs a new SimpleVariance estimator 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
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: true
source
Statistics.std Method
julia
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 using ve.me.

  • kwargs...: Additional keyword arguments passed to the mean estimator.

Returns

  • sd::VecNum: Standard deviation vector of X.

Examples

julia
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.41421

Related

source
Statistics.std Method
julia
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 of X.

Examples

julia
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.7810249675906654

Related

source
Statistics.var Method
julia
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 using ve.me.

  • kwargs...: Additional keyword arguments passed to the mean estimator.

Returns

  • v::VecNum: Variance vector of X.

Examples

julia
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.0

Related

source
Statistics.var Method
julia
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 of X.

Examples

julia
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.61

Related

source