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(
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.
Mathematical definition
Unweighted:
Where:
: Standard deviation of asset . : Variance of asset .
For corrected = true:
For corrected = false:
Weighted:
Where:
: Estimated variance of asset . : Return of asset at time . : Estimated mean of asset . : Number of observations. : Observation weight at time . : Bias correction factor.
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
std(
ve::SimpleVariance{Nothing},
X::AbstractMatrix{<:Union{var"#s20", var"#s19"} where {var"#s20"<:Number, var"#s19"<:AbstractJuMPScalar}};
dims,
mean,
kwargs...
) -> AnySimpleVariance{Nothing} overload of std(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...). Uses SimpleExpectedReturns to compute the mean when none is provided, ignoring the me field.
Statistics.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.
Mathematical definition
Unweighted, corrected = true:
Unweighted, corrected = false:
Weighted:
Where:
: Estimated variance of asset . : Return of asset at time . : Estimated mean of asset . : Number of observations. : Observation weight at time . : Bias correction factor.
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
var(
ve::SimpleVariance{Nothing},
X::AbstractMatrix{<:Union{var"#s20", var"#s19"} where {var"#s20"<:Number, var"#s19"<:AbstractJuMPScalar}};
dims,
mean,
kwargs...
) -> AnySimpleVariance{Nothing} overload of var(ve::SimpleVariance, X::MatNum; dims::Int = 1, mean = nothing, kwargs...). Uses SimpleExpectedReturns to compute the mean when none is provided, ignoring the me field.
Statistics.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...)
PortfolioOptimisers.port_opt_view Method
port_opt_view(
ve::SimpleVariance,
i,
args...
) -> SimpleVariance{__T_me, __T_w, Bool} where {__T_me, __T_w}Gets the view of the simple variance for the i-th element(s).
Arguments
ve: Variance estimator.i: Index or indices to view.
Returns
ve: New variance estimator of the same type as the argument, for the new view.
Related
source