Skip to content
13

Simple variance and standard deviation

The variance is used throughout the library, it can be used as part of the expected return, covariance estimation, 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{__T_me, __T_w, __T_corrected} <: AbstractVarianceEstimator

A flexible variance estimator for PortfolioOptimisers.jl supporting optional expected returns estimators, observation weights, and bias correction.

Fields

  • me: Optional expected returns estimator. It is not needed when used on a vector. If nothing and used on a matrix, defaults to SimpleExpectedReturns.

  • w: Observation weights vector observations × 1.

  • corrected: Whether to apply Bessel's correction.

Constructors

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

Keywords correspond to the struct's fields.

Validation

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

Examples

julia
julia> SimpleVariance()
SimpleVariance
         me ┼ SimpleExpectedReturns
            │   w ┴ nothing
          w ┼ nothing
  corrected ┴ Bool: true

julia> 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
PortfolioOptimisers.factory Method
julia
factory(
    ce::StatsBase.CovarianceEstimator,
    args...;
    kwargs...
) -> StatsBase.CovarianceEstimator

Fallback for covariance estimator factory methods.

Arguments

  • ce: Covariance estimator.

  • args...: Optional arguments (ignored).

  • kwargs...: Optional keyword arguments (ignored).

Returns

  • ce::StatsBase.CovarianceEstimator: The original covariance estimator.

Related

source
julia
factory(
    ve::SimpleVariance,
    w::ObsWeights
) -> SimpleVariance

Return a new SimpleVariance estimator with the specified observation weights.

Arguments

  • ve: Variance estimator.

  • w: Observation weights vector observations × 1.

Returns

  • ve: New variance estimator of the same type as the argument, with the new weights applied.

Details

  • The mean estimator is updated using factory(ve.me, w) for consistency.

  • Sets w to the new weights.

  • The bias correction flag is preserved from the original estimator.

Examples

julia
julia> sv = SimpleVariance()
SimpleVariance
         me ┼ SimpleExpectedReturns
            │   w ┴ nothing
          w ┼ nothing
  corrected ┴ Bool: true

julia> 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]
          w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]
  corrected ┴ Bool: true

Related

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

Compute the standard deviation using a SimpleVariance estimator for a matrix.

This method computes the standard deviation of the input matrix X using the configuration specified in ve.

Arguments

  • ve: Variance estimator.

  • X: Data matrix observations × features if the dims keyword does not exist or dims = 1, features × observations when dims = 2.

  • dims: Dimension along which to perform the computation.

  • mean: Optional mean value to use for centering.

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

Returns

  • sd::ArrNum: Standard deviation vector of X, reshaped to be consistent with the dimension along which the value is computed.

Examples

julia
julia> sv = SimpleVariance()
SimpleVariance
         me ┼ SimpleExpectedReturns
            │   w ┴ 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
) -> Number

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.

Arguments

  • ve: Variance estimator.

  • X: Data vector observations × 1.

  • mean: Optional mean value to use for centering.

Returns

  • vr::Number: Standard deviation of X

Examples

julia
julia> sv = SimpleVariance()
SimpleVariance
         me ┼ SimpleExpectedReturns
            │   w ┴ 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
          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...
) -> ArrNum

Compute the variance using a SimpleVariance estimator for a matrix.

This method computes the variance of the input matrix X using the configuration specified in ve.

Arguments

  • ve: Variance estimator.

  • X: Data matrix observations × features if the dims keyword does not exist or dims = 1, features × observations when dims = 2.

  • dims: Dimension along which to perform the computation.

  • mean: Optional mean value to use for centering.

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

Returns

  • vr::ArrNum: Variance vector of X, reshaped to be consistent with the dimension along which the value is computed.

Examples

julia
julia> sv = SimpleVariance()
SimpleVariance
         me ┼ SimpleExpectedReturns
            │   w ┴ 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
) -> Number

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.

Arguments

  • ve: Variance estimator.

  • X: Data vector observations × 1.

  • mean: Optional mean value to use for centering.

Returns

  • vr::Number: Variance of X

Examples

julia
julia> sv = SimpleVariance()
SimpleVariance
         me ┼ SimpleExpectedReturns
            │   w ┴ 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
          w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.2, 0.3, 0.5]
  corrected ┴ Bool: false

julia> var(svw, X)
0.61

Related

source