Skip to content
18

Non-Optimisation Risk Measures

PortfolioOptimisers.MeanReturn Type
julia
struct MeanReturn{__T_w, __T_flag} <: NonOptimisationRiskMeasure

Represents a simple mean return measure for use in non-optimisation contexts.

MeanReturn computes the arithmetic (or geometric, when flag = true) mean of portfolio returns. It is used as the numerator in risk-adjusted performance ratios such as MeanReturnRiskRatio.

Mathematical definition

For flag = false (arithmetic mean):

x¯=1Tt=1Txt.

Where:

  • x¯: Arithmetic mean portfolio return.

  • x: Portfolio returns vector T×1.

  • T: Number of observations.

For flag = true (log-return mean):

x¯log=1Tt=1Tlog(1+xt).

Where:

  • x¯log: Log-return mean portfolio return.

  • x: Portfolio returns vector T×1.

  • T: Number of observations.

For observation-weighted samples, the weighted mean is used instead.

Fields

  • w: Optional portfolio weights.

  • flag: Algorithm selection flag.

Constructors

julia
MeanReturn(;
    w::Option{<:ObsWeights} = nothing,
    flag::Bool = false
) -> MeanReturn

Keywords correspond to the struct's fields.

Validation

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

Functor

julia
(r::MeanReturn)(x::VecNum)

Computes the mean return of a portfolio returns vector x.

Arguments

  • x::VecNum: Portfolio returns vector.

Examples

julia
julia> MeanReturn()
MeanReturn
     w ┼ nothing
  flag ┴ Bool: false

Related

source
PortfolioOptimisers.port_opt_view Method
julia
port_opt_view(x, i, args...; kwargs...) -> nothing_scalar_array_view(x, i)

Sub-select an estimator, result, or algorithm to the asset/observation index i.

port_opt_view is the index-selection counterpart of factory: where factory threads runtime values down a composed struct tree, port_opt_view threads an index selection — restricting every data-bearing field and composed child to the subset i. It is the mechanism that makes meta-optimisers (NestedClustered, SubsetResampling) and cross-validation variants operate on subproblems with identical struct shapes.

Callers do not normally call port_opt_view directly; it is driven by meta-optimisers and cross-validation internals. It is public (not exported) because extension authors who implement a new composed estimator may need to define a method. Use @vprop on data-bearing fields to have the method generated automatically.

This universal fallback handles leaf values: arrays are sliced via nothing_scalar_array_view; scalars, nothing, estimators without data fields, and algorithms pass through unchanged. Composed structs that recurse into children define their own (more specific) method — emitted by @vprop or hand-written.

The threaded tail args... (typically the returns matrix X for the JuMP families) and any kwargs are accepted and dropped here, so a macro-threaded port_opt_view(child, i, X) never MethodErrors on a leaf field.

Related

source
PortfolioOptimisers.MeanReturnRiskRatio Type
julia
struct MeanReturnRiskRatio{__T_rt, __T_rk, __T_rf} <: NonOptimisationRiskMeasure

Represents a mean return to risk ratio measure.

MeanReturnRiskRatio computes the ratio of the mean portfolio return (minus a risk-free rate) to a risk measure, used for performance analysis and comparison. It generalises the Sharpe ratio by allowing any risk measure in the denominator.

Mathematical definition

MRRR(x)=x¯rfρ(x).

Where:

  • MRRR(x): Mean return to risk ratio.

  • x: Portfolio returns vector T×1.

  • x¯: Mean portfolio return (computed by rt).

  • rf: Risk-free rate.

  • ρ: Base risk measure (computed by rk).

Fields

  • rt: Mean return estimator.

  • rk: Risk measure for ratio computation.

  • rf: Risk-free rate.

Constructors

julia
MeanReturnRiskRatio(;
    rt::MeanReturn = MeanReturn(),
    rk::AbstractBaseRiskMeasure = ConditionalValueatRisk(),
    rf::Number = 0.0
) -> MeanReturnRiskRatio

Keywords correspond to the struct's fields.

Validation

  • isfinite(rf).

Propagated parameters

When factory is called on this type, the following @fprop-tagged fields are automatically propagated:

  • rt: Recursively updated via factory.

  • rk: Recursively updated via factory.

Related

source
PortfolioOptimisers.needs_previous_weights Method
julia
needs_previous_weights(r::MeanReturnRiskRatio) -> Any

Return whether MeanReturnRiskRatio r requires previous portfolio weights.

Delegates to the inner risk measure r.rk.

Related

source
PortfolioOptimisers.factory Method
julia
factory(a::Union{Nothing, <:AbstractEstimator, <:AbstractAlgorithm,
                 <:AbstractResult}, args...; kwargs...) -> a

No-op factory function for constructing objects with a uniform interface.

Defining methods which dispatch on the first argument allows for a consistent factory interface across different types.

factory and port_opt_view are the two propagation mechanisms in this library. They are duals: factory threads runtime values (prior moments, observation weights, previous portfolio weights) down through a composed struct tree; port_opt_view threads an index selection (a subset of assets or observations) down through the same tree.

Arguments

  • a: Indicates no object should be constructed.

  • args...: Arbitrary positional arguments (ignored).

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

Returns

  • a: The input unchanged.

Examples

julia
julia> factory(nothing, 1, 2; x = 3)

julia> factory(MeanValue())
MeanValue
  w ┴ nothing

Related

source
julia
factory(
    rs::AbstractBaseRiskMeasure,
    args...;
    kwargs...
) -> VarianceSkewKurtosis{RiskMeasureSettings{__T_scale, __T_ub, __T_rke}, _A, Skewness{__T_settings, __T_ve, __T_sk, __T_w, __T_mu}} where {__T_scale, __T_ub, __T_rke, _A, __T_settings, __T_ve, __T_sk, __T_w, __T_mu}

Return the risk measure rs unchanged.

Identity pass-through used when a risk measure is provided in a context that calls factory.

Related

source
PortfolioOptimisers.factory Method
julia
factory(a::Union{Nothing, <:AbstractEstimator, <:AbstractAlgorithm,
                 <:AbstractResult}, args...; kwargs...) -> a

No-op factory function for constructing objects with a uniform interface.

Defining methods which dispatch on the first argument allows for a consistent factory interface across different types.

factory and port_opt_view are the two propagation mechanisms in this library. They are duals: factory threads runtime values (prior moments, observation weights, previous portfolio weights) down through a composed struct tree; port_opt_view threads an index selection (a subset of assets or observations) down through the same tree.

Arguments

  • a: Indicates no object should be constructed.

  • args...: Arbitrary positional arguments (ignored).

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

Returns

  • a: The input unchanged.

Examples

julia
julia> factory(nothing, 1, 2; x = 3)

julia> factory(MeanValue())
MeanValue
  w ┴ nothing

Related

source
julia
factory(
    rs::AbstractBaseRiskMeasure,
    args...;
    kwargs...
) -> VarianceSkewKurtosis{RiskMeasureSettings{__T_scale, __T_ub, __T_rke}, _A, Skewness{__T_settings, __T_ve, __T_sk, __T_w, __T_mu}} where {__T_scale, __T_ub, __T_rke, _A, __T_settings, __T_ve, __T_sk, __T_w, __T_mu}

Return the risk measure rs unchanged.

Identity pass-through used when a risk measure is provided in a context that calls factory.

Related

source
PortfolioOptimisers.ThirdCentralMoment Type
julia
struct ThirdCentralMoment{__T_w, __T_mu} <: NonOptimisationRiskMeasure

Represents the Third Central Moment risk measure.

ThirdCentralMoment computes the third central moment of portfolio returns about a specified centre. It is used as a measure of the asymmetry (skewness) of the return distribution in higher-order portfolio optimisation.

Mathematical definition

Let μ be the specified centre and δt=xtμ the centred deviations. The third central moment is:

m3(x)=1Tt=1Tδt3.

Where:

  • m3(x): Third central moment of portfolio returns.

  • x: Portfolio returns vector T×1.

  • T: Number of observations.

  • μ: Specified centre of the distribution.

  • δt=xtμ: Centred deviation at period t.

For observation-weighted samples, the weighted mean is used.

Fields

  • w: Optional portfolio weights.

  • mu: Optional mean for centering.

Constructors

julia
ThirdCentralMoment(;
    w::Option{<:ObsWeights} = nothing,
    mu::Option{<:Num_VecNum_VecScalar} = nothing
) -> ThirdCentralMoment

Keywords correspond to the struct's fields.

Validation

  • If mu is a VecNum: !isempty(mu).

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

Functor

julia
(r::ThirdCentralMoment)(w::VecNum, X::MatNum, fees = nothing)

Computes the third central moment of the portfolio returns.

Arguments

  • w::VecNum: Portfolio weights vector.

  • X::MatNum: Asset returns matrix (T×N).

  • fees: Optional fee structure.

View parameters

When port_opt_view is called on this type, the following @vprop-tagged fields are automatically subset to the selected indices:

Examples

julia
julia> ThirdCentralMoment()
ThirdCentralMoment
   w ┼ nothing
  mu ┴ nothing

Related

source
PortfolioOptimisers.port_opt_view Method
julia
port_opt_view(x, i, args...; kwargs...) -> nothing_scalar_array_view(x, i)

Sub-select an estimator, result, or algorithm to the asset/observation index i.

port_opt_view is the index-selection counterpart of factory: where factory threads runtime values down a composed struct tree, port_opt_view threads an index selection — restricting every data-bearing field and composed child to the subset i. It is the mechanism that makes meta-optimisers (NestedClustered, SubsetResampling) and cross-validation variants operate on subproblems with identical struct shapes.

Callers do not normally call port_opt_view directly; it is driven by meta-optimisers and cross-validation internals. It is public (not exported) because extension authors who implement a new composed estimator may need to define a method. Use @vprop on data-bearing fields to have the method generated automatically.

This universal fallback handles leaf values: arrays are sliced via nothing_scalar_array_view; scalars, nothing, estimators without data fields, and algorithms pass through unchanged. Composed structs that recurse into children define their own (more specific) method — emitted by @vprop or hand-written.

The threaded tail args... (typically the returns matrix X for the JuMP families) and any kwargs are accepted and dropped here, so a macro-threaded port_opt_view(child, i, X) never MethodErrors on a leaf field.

Related

source
PortfolioOptimisers.TCM_Sk Type
julia
const TCM_Sk{T1, T2} = Union{<:ThirdCentralMoment{T1, T2}, <:Skewness{<:Any, <:Any, T1, T2}}

Parameterised union of ThirdCentralMoment and Skewness sharing the same observation-weight (T1) and target-mean (T2) type parameters.

Used for unified dispatch on moment-target calculation methods.

Related

source
PortfolioOptimisers.calc_moment_target Method
julia
calc_moment_target(::TCM_Sk{Nothing, Nothing}, ::Any, x::VecNum)
calc_moment_target(r::TCM_Sk{<:StatsBase.AbstractWeights, Nothing}, ::Any, x::VecNum)
calc_moment_target(r::TCM_Sk{<:Any, <:VecNum}, w::VecNum, ::Any)
calc_moment_target(r::TCM_Sk{<:Any, <:VecScalar}, w::VecNum, ::Any)
calc_moment_target(r::TCM_Sk{<:Any, <:Number}, ::Any, ::Any)

Compute the centering target for ThirdCentralMoment and Skewness risk measures.

Dispatches on the observation-weight type T1 and mean type T2 of TCM_Sk:

  • No weights, no mu: arithmetic mean of x.

  • AbstractWeights, no mu: weighted mean of x.

  • VecNum mu: dot product wμ.

  • VecScalar mu: wμv+μs.

  • Number mu: the scalar r.mu directly.

Related

source
PortfolioOptimisers.calc_deviations_vec Function
julia
calc_deviations_vec(
    r::Union{ThirdCentralMoment{T1, T2}, Skewness{<:Any, <:Any, <:Any, T1, T2}} where {T1, T2},
    w::AbstractVector{<:Union{var"#s29", var"#s28"} where {var"#s29"<:Number, var"#s28"<:AbstractJuMPScalar}},
    X::AbstractMatrix{<:Union{var"#s29", var"#s28"} where {var"#s29"<:Number, var"#s28"<:AbstractJuMPScalar}}
) -> Any
calc_deviations_vec(
    r::Union{ThirdCentralMoment{T1, T2}, Skewness{<:Any, <:Any, <:Any, T1, T2}} where {T1, T2},
    w::AbstractVector{<:Union{var"#s29", var"#s28"} where {var"#s29"<:Number, var"#s28"<:AbstractJuMPScalar}},
    X::AbstractMatrix{<:Union{var"#s29", var"#s28"} where {var"#s29"<:Number, var"#s28"<:AbstractJuMPScalar}},
    fees::Union{Nothing, Fees}
) -> Any

Compute the vector of deviations from the centering target for ThirdCentralMoment and Skewness risk measures.

Related

source
PortfolioOptimisers.calc_deviations_vec Method
julia
calc_deviations_vec(
    r::Union{ThirdCentralMoment{T1, T2}, Skewness{<:Any, <:Any, <:Any, T1, T2}} where {T1, T2},
    x::AbstractVector{<:Union{var"#s29", var"#s28"} where {var"#s29"<:Number, var"#s28"<:AbstractJuMPScalar}}
) -> Any

Compute the vector of deviations from the centering target for a precomputed returns series for ThirdCentralMoment and Skewness risk measures.

Single-argument form used by the precomputed-returns functor r(x::VecNum) (ADR 0007).

Related

source
PortfolioOptimisers.supports_precomputed_returns Method
julia
supports_precomputed_returns(r::ThirdCentralMoment) -> Any

Return whether ThirdCentralMoment r supports precomputed-return evaluation.

Delegates to weight_independent_target on r.mu: true iff the target is Nothing, a Number, or a MedianCenteringFunction; false for per-asset targets.

Related

source