Non-Optimisation Risk Measures
PortfolioOptimisers.MeanReturn Type
struct MeanReturn{__T_w, __T_flag} <: NonOptimisationRiskMeasureRepresents 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):
Where:
: Arithmetic mean portfolio return. : Portfolio returns vector . : Number of observations.
For flag = true (log-return mean):
Where:
: Log-return mean portfolio return. : Portfolio returns vector . : Number of observations.
For observation-weighted samples, the weighted mean is used instead.
Fields
w: Optional portfolio weights.flag: Algorithm selection flag.
Constructors
MeanReturn(;
w::Option{<:ObsWeights} = nothing,
flag::Bool = false
) -> MeanReturnKeywords correspond to the struct's fields.
Validation
- If
wis notnothing:!isempty(w).
Functor
(r::MeanReturn)(x::VecNum)Computes the mean return of a portfolio returns vector x.
Arguments
x::VecNum: Portfolio returns vector.
Examples
julia> MeanReturn()
MeanReturn
w ┼ nothing
flag ┴ Bool: falseRelated
sourcePortfolioOptimisers.port_opt_view Method
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
sourcePortfolioOptimisers.MeanReturnRiskRatio Type
struct MeanReturnRiskRatio{__T_rt, __T_rk, __T_rf} <: NonOptimisationRiskMeasureRepresents 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
Where:
: Mean return to risk ratio. : Portfolio returns vector . : Mean portfolio return (computed by rt).: 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
MeanReturnRiskRatio(;
rt::MeanReturn = MeanReturn(),
rk::AbstractBaseRiskMeasure = ConditionalValueatRisk(),
rf::Number = 0.0
) -> MeanReturnRiskRatioKeywords 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:
Related
sourcePortfolioOptimisers.needs_previous_weights Method
needs_previous_weights(r::MeanReturnRiskRatio) -> AnyReturn whether MeanReturnRiskRatio r requires previous portfolio weights.
Delegates to the inner risk measure r.rk.
Related
sourcePortfolioOptimisers.factory Method
factory(a::Union{Nothing, <:AbstractEstimator, <:AbstractAlgorithm,
<:AbstractResult}, args...; kwargs...) -> aNo-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> factory(nothing, 1, 2; x = 3)
julia> factory(MeanValue())
MeanValue
w ┴ nothingRelated
sourcefactory(
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
sourcePortfolioOptimisers.factory Method
factory(a::Union{Nothing, <:AbstractEstimator, <:AbstractAlgorithm,
<:AbstractResult}, args...; kwargs...) -> aNo-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> factory(nothing, 1, 2; x = 3)
julia> factory(MeanValue())
MeanValue
w ┴ nothingRelated
sourcefactory(
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
sourcePortfolioOptimisers.ThirdCentralMoment Type
struct ThirdCentralMoment{__T_w, __T_mu} <: NonOptimisationRiskMeasureRepresents 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
Where:
: Third central moment of portfolio returns. : Portfolio returns vector . : Number of observations. : Specified centre of the distribution. : Centred deviation at period .
For observation-weighted samples, the weighted mean is used.
Fields
w: Optional portfolio weights.mu: Optional mean for centering.
Constructors
ThirdCentralMoment(;
w::Option{<:ObsWeights} = nothing,
mu::Option{<:Num_VecNum_VecScalar} = nothing
) -> ThirdCentralMomentKeywords correspond to the struct's fields.
Validation
If
muis aVecNum:!isempty(mu).If
wis notnothing:!isempty(w).
Functor
(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 (). 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:
mu: Sliced to the selected indices viaport_opt_view.
Examples
julia> ThirdCentralMoment()
ThirdCentralMoment
w ┼ nothing
mu ┴ nothingRelated
sourcePortfolioOptimisers.port_opt_view Method
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
sourcePortfolioOptimisers.TCM_Sk Type
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
sourcePortfolioOptimisers.calc_moment_target Method
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 ofx.VecNummu: dot product. VecScalarmu:. Numbermu: the scalarr.mudirectly.
Related
sourcePortfolioOptimisers.calc_deviations_vec Function
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}
) -> AnyCompute the vector of deviations from the centering target for ThirdCentralMoment and Skewness risk measures.
Related
sourcePortfolioOptimisers.calc_deviations_vec Method
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}}
) -> AnyCompute 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
sourcePortfolioOptimisers.supports_precomputed_returns Method
supports_precomputed_returns(r::ThirdCentralMoment) -> AnyReturn 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