Moment Risk Measures: private API
PortfolioOptimisers.MomentMeasureAlgorithm — Type
abstract type MomentMeasureAlgorithm <: AbstractAlgorithmSelects the reduction that turns the moment deviations of a portfolio into one risk value.
A LowOrderMoment holds a LowOrderMomentMeasureAlgorithm, and a HighOrderMoment holds a HighOrderMomentMeasureAlgorithm. The measure computes the deviations of the net portfolio returns from its Moment Target, and the algorithm states how moment_risk reduces them. Each concrete algorithm states its reduction under # Mathematical definition.
Related
PortfolioOptimisers.LowOrderMomentMeasureAlgorithm — Type
abstract type LowOrderMomentMeasureAlgorithm <: MomentMeasureAlgorithmSelects a moment of order one or two, or the root of an even moment, for a low-order moment risk measure.
LowOrderMoment holds it. The reduction of each member is homogeneous in the deviations, of degree one or two, so the model of each member is a linear, second-order cone or power cone programme. The members are FirstLowerMoment, MeanAbsoluteDeviation, SecondMoment and EvenMoment. No member gives a standardised moment. The ve of SecondMoment supplies only its divisor and its observation weights.
Related
PortfolioOptimisers.HighOrderMomentMeasureAlgorithm — Type
abstract type HighOrderMomentMeasureAlgorithm <: MomentMeasureAlgorithmSelects a moment of order three or four for a high-order moment risk measure.
HighOrderMoment holds it. HighOrderMoment is a hierarchical risk measure, and the library builds no JuMP model for it. The members are ThirdLowerMoment and FourthMoment. A member holds no variance estimator. The field ve of HighOrderMoment divides a member by a power of its second moment.
Related
PortfolioOptimisers.LoHiOrderMoment — Type
const LoHiOrderMoment{T1, T2, T3, T4} = Union{LowOrderMoment{T1, T2, T3, T4},
HighOrderMoment{T1, T2, T3, T4}}Groups LowOrderMoment and HighOrderMoment under the same four type parameters.
The two measures have the same first four fields in the same order, settings, w, mu and alg. HighOrderMoment has a fifth field, ve, and the group leaves its parameter free. The two measures select the Moment Target and the deviations in the same way. So calc_moment_target dispatches on the second and the third parameter, the types of w and mu, once for both measures. calc_deviations_vec and supports_precomputed_returns also take the group.
Related
PortfolioOptimisers.calc_moment_target — Method
calc_moment_target(::LoHiOrderMoment{<:Any, Nothing, Nothing, <:Any},
::Any, x::VecNum)Return the Moment Target of a measure whose w and mu are both nothing, the mean of x.
Arguments
r: ALowOrderMomentor aHighOrderMomentwhosewandmuarenothing.::Any: The asset weights. The method does not read them.x::VecNum: Net portfolio returns.
Returns
tgt::Number:Statistics.mean(x).
Related
PortfolioOptimisers.calc_moment_target — Method
calc_moment_target(r::LoHiOrderMoment{<:Any, <:StatsBase.AbstractWeights, Nothing, <:Any},
::Any, x::VecNum)Return the Moment Target of a measure that holds observation weights and no mu, the weighted mean of x.
Arguments
r: ALowOrderMomentor aHighOrderMomentwhosewis aStatsBase.AbstractWeightsand whosemuisnothing.::Any: The asset weights. The method does not read them.x::VecNum: Net portfolio returns.
Returns
tgt::Number:Statistics.mean(x, r.w).
Related
PortfolioOptimisers.calc_moment_target — Method
calc_moment_target(r::LoHiOrderMoment{<:Any, <:Any, <:VecNum, <:Any},
w::VecNum, ::Any)Return the gross Moment Target of a measure whose mu holds expected asset returns, the expected portfolio return.
calc_deviations_vec subtracts the mean fee per period from it, so the target that the deviations read is net.
Arguments
r: ALowOrderMomentor aHighOrderMomentwhosemuis a vector.w::VecNum: Asset weights.::Any: The net portfolio returns. The method does not read them.
Returns
tgt::Number:LinearAlgebra.dot(w, r.mu).
Related
PortfolioOptimisers.calc_moment_target — Method
calc_moment_target(r::LoHiOrderMoment{<:Any, <:Any, <:VecScalar, <:Any},
w::VecNum, ::Any)Return the gross Moment Target of a measure whose mu is a VecScalar, the expected portfolio return of mu.v plus the offset mu.s.
calc_deviations_vec subtracts the mean fee per period from it, so the target that the deviations read is net.
Arguments
r: ALowOrderMomentor aHighOrderMomentwhosemuis aVecScalar.w::VecNum: Asset weights.::Any: The net portfolio returns. The method does not read them.
Returns
tgt::Number:LinearAlgebra.dot(w, r.mu.v) + r.mu.s.
Related
PortfolioOptimisers.calc_moment_target — Method
calc_moment_target(r::LoHiOrderMoment{<:Any, <:Any, <:Number, <:Any},
::Any, ::Any)Return the Moment Target of a measure whose mu is a number, the number itself.
The number is a minimum acceptable return on the net series, so no fee applies to it.
Arguments
r: ALowOrderMomentor aHighOrderMomentwhosemuis a number.::Any: The asset weights. The method does not read them.::Any: The net portfolio returns. The method does not read them.
Returns
tgt::Number:r.mu.
Related
PortfolioOptimisers.calc_deviations_vec — Function
calc_deviations_vec(r::LoHiOrderMoment, w::VecNum, X::MatNum,
fees::Option{<:Fees} = nothing)Return the deviations of the net portfolio returns from the net Moment Target of r.
A per-asset target is gross, and the series is net of the fee. So the method subtracts the mean fee per period from such a target, and a fee that every period pays in full cancels from every deviation. A target that is the mean of the series, or a number, takes no fee.
Algorithm
- Compute the net portfolio returns
xwithcalc_net_returns. - Compute the gross Moment Target with
calc_moment_targetfromr,wandx. - Subtract
moment_target_feesfrom it, giving the net targettgt. - Return
x .- tgt.
Arguments
r::LoHiOrderMoment: The moment risk measure.w::VecNum: Asset weights.X::MatNum: Returns matrixobservations × assets.fees::Option{<:Fees}: Fees that the net returns pay, ornothing.
Returns
val::VecNum: The deviations,observations × 1.
Related
LowOrderMomentHighOrderMomentcalc_moment_targetcalc_net_returnsmoment_target_feesmoment_risk: reduces the deviations to the risk value.
PortfolioOptimisers.calc_deviations_vec — Method
calc_deviations_vec(
r::Union{LowOrderMoment{T1, T2, T3, T4}, HighOrderMoment{T1, T2, T3, T4}} where {T1, T2, T3, T4},
x::AbstractVector{<:Union{Number, AbstractJuMPScalar}}
) -> Any
Return the deviations of the net portfolio returns x from the Moment Target of r.
The functor r(x) calls this form. It reads x as a net series, so no fee applies. It passes no asset weights to calc_moment_target, so it needs a target that does not read them, a mu that is nothing or a number. A per-asset mu raises a MethodError. supports_precomputed_returns reports the condition before the call.
Related
PortfolioOptimisers.resolve_deferred_quantities — Method
resolve_deferred_quantities(x, pr::AbstractPriorResult, slv = nothing)Resolve the children that deferred_slots declared and the slots that calibration_slots declared, and return x itself when none of them changed.
This is the derived half of the resolution rule. A container declares its children once and both entry points follow: factory reaches them through @fprop, and the JuMP builders reach them through this method. Neither needs a forwarding method per container.
A type that resolves a quantity of its own overrides this with its own method, which is more specific. So the derivation carries container recursion alone, and never guesses how a matrix, a tensor or the centre a moment was taken about comes out of a fit.
Both channels end in one rebuild: a measure that carries both kinds of slot must not be rebuilt twice. resolve_calibration_slots states the calibration half and returns its resolved slots rather than a rebuilt object, and the two answers merge here. The deferred half merges last, so it wins a key both channels declare. A container names one child in both, and the child the recursion resolved is the one to keep.
slv is the effective solver, and the recursion threads it to every child. A container states no solver of its own, so it changes none: each child settles the one it was handed against the one it carries.
Algorithm
- Read the slots
xdeclares withdeferred_slots, givingslots. - Read the resolved calibration slots with
resolve_calibration_slots, givingcalibrated. - Return
xunchanged when both are empty. A type with neither kind of slot needs no method of its own. - Resolve every entry of
slotswithresolve_deferred_quantities, threadingprandslvto each, givingresolved. A child measure resolves through its own method, a vector of children through the vector method, and any other occupant comes back unchanged through this method, because it declares no slot: a stated value,nothingor a Deferred Quantity that the enclosing type resolves itself. - Refuse a slot the recursion left unresolved with
assert_declared_slot_resolver. - Hand
merge(calibrated, resolved)torebuild_with_slots, which returnsxitself when no entry moved and a rebuilt copy when one did.
Returns
xitself when no slot moved, and a rebuilt copy ofxwhen one did.
Related
resolve_deferred_quantities(
r::LowOrderMoment,
pr::AbstractPriorResult
) -> LowOrderMoment
resolve_deferred_quantities(
r::LowOrderMoment,
pr::AbstractPriorResult,
) -> LowOrderMoment
Resolve a Deferred Quantity in the mu slot of a LowOrderMoment against the prior result pr.
mu is the one slot of the measure that a prior can supply, so the slot holds the estimator itself, and no other slot reads its fit. A measure whose mu is not a Deferred Quantity returns unchanged.
Related
PortfolioOptimisers.resolve_deferred_quantities — Method
resolve_deferred_quantities(x, pr::AbstractPriorResult, slv = nothing)Resolve the children that deferred_slots declared and the slots that calibration_slots declared, and return x itself when none of them changed.
This is the derived half of the resolution rule. A container declares its children once and both entry points follow: factory reaches them through @fprop, and the JuMP builders reach them through this method. Neither needs a forwarding method per container.
A type that resolves a quantity of its own overrides this with its own method, which is more specific. So the derivation carries container recursion alone, and never guesses how a matrix, a tensor or the centre a moment was taken about comes out of a fit.
Both channels end in one rebuild: a measure that carries both kinds of slot must not be rebuilt twice. resolve_calibration_slots states the calibration half and returns its resolved slots rather than a rebuilt object, and the two answers merge here. The deferred half merges last, so it wins a key both channels declare. A container names one child in both, and the child the recursion resolved is the one to keep.
slv is the effective solver, and the recursion threads it to every child. A container states no solver of its own, so it changes none: each child settles the one it was handed against the one it carries.
Algorithm
- Read the slots
xdeclares withdeferred_slots, givingslots. - Read the resolved calibration slots with
resolve_calibration_slots, givingcalibrated. - Return
xunchanged when both are empty. A type with neither kind of slot needs no method of its own. - Resolve every entry of
slotswithresolve_deferred_quantities, threadingprandslvto each, givingresolved. A child measure resolves through its own method, a vector of children through the vector method, and any other occupant comes back unchanged through this method, because it declares no slot: a stated value,nothingor a Deferred Quantity that the enclosing type resolves itself. - Refuse a slot the recursion left unresolved with
assert_declared_slot_resolver. - Hand
merge(calibrated, resolved)torebuild_with_slots, which returnsxitself when no entry moved and a rebuilt copy when one did.
Returns
xitself when no slot moved, and a rebuilt copy ofxwhen one did.
Related
resolve_deferred_quantities(
r::HighOrderMoment,
pr::AbstractPriorResult
) -> HighOrderMoment
resolve_deferred_quantities(
r::HighOrderMoment,
pr::AbstractPriorResult,
) -> HighOrderMoment
Resolve a Deferred Quantity in the mu slot of a HighOrderMoment against the prior result pr.
mu is the one slot of the measure that a prior can supply, so the slot holds the estimator itself, and no other slot reads its fit. A measure whose mu is not a Deferred Quantity returns unchanged.
Related
PortfolioOptimisers.moment_risk — Function
moment_risk(r::LoHiOrderMoment, val::VecNum)
moment_risk(r::MedianAbsoluteDeviation, val::VecNum)
moment_risk(r::ThirdCentralMoment, val::VecNum)Reduce the moment deviations val of a moment risk measure to its risk value.
val is the output of calc_deviations_vec. Both forms of the functor call this function, so the value of r(w, X, fees) and of r(x) comes from one definition. Dispatch on r.alg selects the reduction, and the docstring of each algorithm states its closed form. The methods for a full FourthMoment overwrite val in place.
Algorithm
The steps for LowOrderMoment and HighOrderMoment:
- For a lower moment, replace
valwithmin.(val, 0).FirstLowerMoment,ThirdLowerMomentand aSemiMomentare lower moments. - For
SecondMoment, returnStatistics.var(r.alg.ve, val; mean = 0), orStatistics.stdunderSOCRiskExpr. Stop. - For a
HighOrderMomentwhoseveis a variance estimator, computesigma = Statistics.var(r.ve, val; mean = 0)before the power. - Raise each entry of
valto the power of the moment:absforMeanAbsoluteDeviation, the cube or the fourth power for a high-order moment. - Take the mean of
val, weighted byr.wwhen it is notnothing, givingres. - Negate
resforFirstLowerMomentandThirdLowerMoment, so that a larger value is more risk. - For a
HighOrderMomentwhoseveis a variance estimator, divideresbysigma^(n / 2), withnthe order ofr.alg.
EvenMoment takes the place of steps 4 and 5. It computes the norm of order 2p of val, each entry scaled by w_t^(1 / 2p) when r.w is not nothing, and returns its square divided by T^(1 / p), with T the effective sample size.
MedianAbsoluteDeviation returns the median of abs.(val), weighted by r.w when it is not nothing, and divides it by $\Phi^{-1}(3/4)$ when r.flag is true. ThirdCentralMoment returns the mean of val .^ 3, weighted by r.w when it is not nothing. When r.ve is a variance estimator, it divides that mean by the cube of Statistics.std(r.ve, val; mean = 0).
Related
calc_deviations_vecLowOrderMomentHighOrderMomentMomentRiskMeasures: the measures whose functor calls this function.