Moment Risk Measures: private API

PortfolioOptimisers.MomentMeasureAlgorithm — Type
abstract type MomentMeasureAlgorithm <: AbstractAlgorithm

Selects 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

source
PortfolioOptimisers.LowOrderMomentMeasureAlgorithm — Type
abstract type LowOrderMomentMeasureAlgorithm <: MomentMeasureAlgorithm

Selects 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

source
PortfolioOptimisers.HighOrderMomentMeasureAlgorithm — Type
abstract type HighOrderMomentMeasureAlgorithm <: MomentMeasureAlgorithm

Selects 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

source
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

source
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: A LowOrderMoment or a HighOrderMoment whose w is a StatsBase.AbstractWeights and whose mu is nothing.
  • ::Any: The asset weights. The method does not read them.
  • x::VecNum: Net portfolio returns.

Returns

  • tgt::Number: Statistics.mean(x, r.w).

Related

source
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: A LowOrderMoment or a HighOrderMoment whose mu is 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

source
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

Returns

  • tgt::Number: LinearAlgebra.dot(w, r.mu.v) + r.mu.s.

Related

source
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: A LowOrderMoment or a HighOrderMoment whose mu is 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

source
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

  1. Compute the net portfolio returns x with calc_net_returns.
  2. Compute the gross Moment Target with calc_moment_target from r, w and x.
  3. Subtract moment_target_fees from it, giving the net target tgt.
  4. Return x .- tgt.

Arguments

  • r::LoHiOrderMoment: The moment risk measure.
  • w::VecNum: Asset weights.
  • X::MatNum: Returns matrix observations × assets.
  • fees::Option{<:Fees}: Fees that the net returns pay, or nothing.

Returns

  • val::VecNum: The deviations, observations × 1.

Related

source
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

source
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

  1. Read the slots x declares with deferred_slots, giving slots.
  2. Read the resolved calibration slots with resolve_calibration_slots, giving calibrated.
  3. Return x unchanged when both are empty. A type with neither kind of slot needs no method of its own.
  4. Resolve every entry of slots with resolve_deferred_quantities, threading pr and slv to each, giving resolved. 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, nothing or a Deferred Quantity that the enclosing type resolves itself.
  5. Refuse a slot the recursion left unresolved with assert_declared_slot_resolver.
  6. Hand merge(calibrated, resolved) to rebuild_with_slots, which returns x itself when no entry moved and a rebuilt copy when one did.

Returns

  • x itself when no slot moved, and a rebuilt copy of x when one did.

Related

source
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

source
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

  1. Read the slots x declares with deferred_slots, giving slots.
  2. Read the resolved calibration slots with resolve_calibration_slots, giving calibrated.
  3. Return x unchanged when both are empty. A type with neither kind of slot needs no method of its own.
  4. Resolve every entry of slots with resolve_deferred_quantities, threading pr and slv to each, giving resolved. 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, nothing or a Deferred Quantity that the enclosing type resolves itself.
  5. Refuse a slot the recursion left unresolved with assert_declared_slot_resolver.
  6. Hand merge(calibrated, resolved) to rebuild_with_slots, which returns x itself when no entry moved and a rebuilt copy when one did.

Returns

  • x itself when no slot moved, and a rebuilt copy of x when one did.

Related

source
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

source
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:

  1. For a lower moment, replace val with min.(val, 0). FirstLowerMoment, ThirdLowerMoment and a SemiMoment are lower moments.
  2. For SecondMoment, return Statistics.var(r.alg.ve, val; mean = 0), or Statistics.std under SOCRiskExpr. Stop.
  3. For a HighOrderMoment whose ve is a variance estimator, compute sigma = Statistics.var(r.ve, val; mean = 0) before the power.
  4. Raise each entry of val to the power of the moment: abs for MeanAbsoluteDeviation, the cube or the fourth power for a high-order moment.
  5. Take the mean of val, weighted by r.w when it is not nothing, giving res.
  6. Negate res for FirstLowerMoment and ThirdLowerMoment, so that a larger value is more risk.
  7. For a HighOrderMoment whose ve is a variance estimator, divide res by sigma^(n / 2), with n the order of r.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

source