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
struct SimpleVariance{__T_me, __T_w, __T_corrected} <: AbstractVarianceEstimatorA 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. Ifnothingand used on a matrix, defaults toSimpleExpectedReturns.w: Observation weights vectorobservations × 1.corrected: Whether to apply Bessel's correction.
Constructors
SimpleVariance(;
me::Option{<:AbstractExpectedReturnsEstimator} = SimpleExpectedReturns(),
w::Option{<:ObsWeights} = nothing,
corrected::Bool = true
) -> SimpleVarianceKeywords correspond to the struct's fields.
Validation
If w is not nothing, !isempty(w).
Examples
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: 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(
ce::StatsBase.CovarianceEstimator,
args...;
kwargs...
) -> StatsBase.CovarianceEstimatorFallback 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
sourcefactory(
ve::SimpleVariance,
w::ObsWeights
) -> SimpleVarianceReturn a new SimpleVariance estimator with the specified observation weights.
Arguments
ve: Variance estimator.w: Observation weights vectorobservations × 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
wto the new weights.The bias correction flag is preserved from the original estimator.
Examples
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: trueRelated
sourceStatistics.std Method
Statistics.std(
ve::SimpleVariance,
X::MatNum;
dims::Int = 1,
mean = nothing,
kwargs...,
) -> ArrNumCompute 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 matrixobservations × featuresif thedimskeyword does not exist ordims = 1,features × observationswhendims = 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 ofX, reshaped to be consistent with the dimension along which the value is computed.
Examples
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.41421Related
sourceStatistics.std Method
Statistics.std(
ve::SimpleVariance,
X::VecNum;
mean = nothing
) -> NumberCompute 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 vectorobservations × 1.mean: Optional mean value to use for centering.
Returns
vr::Number: Standard deviation ofX
Examples
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.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...
) -> ArrNumCompute 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 matrixobservations × featuresif thedimskeyword does not exist ordims = 1,features × observationswhendims = 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 ofX, reshaped to be consistent with the dimension along which the value is computed.
Examples
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.0Related
sourceStatistics.var Method
Statistics.var(
ve::SimpleVariance,
X::VecNum;
mean = nothing
) -> NumberCompute 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 vectorobservations × 1.mean: Optional mean value to use for centering.
Returns
vr::Number: Variance ofX
Examples
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.61Related
std(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)var(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...)