Median Absolute Deviation Risk: private API
PortfolioOptimisers.MedianCenteringFunction — Type
abstract type MedianCenteringFunction <: AbstractAlgorithmSelects the centre about which a median absolute deviation takes the deviations of the portfolio returns.
A subtype centres the portfolio return series at the point of use, so the centre follows the weights of each call. MedianCentering takes the median of the series, and MeanCentering takes its mean. The two give different values on a skewed series. Each subtype takes the observation weights of the measure when the measure holds them.
Related
PortfolioOptimisers.MedAbsDevMu — Type
const MedAbsDevMu = Union{MuSlot, MedianCenteringFunction}Union of valid centring-target types for MedianAbsoluteDeviation.
The union admits a number, a vector of expected asset returns, an estimator that computes such a vector, or a MedianCenteringFunction. The estimator is a Deferred Quantity, which MuSlot states.
The field has two resolution points, and the type of the value selects one. A centring function resolves in calc_moment_target, at the point of use, because it centres the portfolio series and so needs the asset weights. A Deferred Quantity resolves in factory, because it needs the returns matrix, which calc_moment_target never sees. The field has no nothing state, and the default is MedianCentering().
The two resolutions give different quantities. The median is not linear, so median(w'X) ≠ w' * median(X), and MedianCentering() does not agree with mu = MedianExpectedReturns(). A resolved vector is a gross expected return, and the series is net of the fee. So moment_target_fees subtracts the mean fee per period from the target, and the target is the net mean. MeanCentering() then agrees with a vector that is the mean of the same returns, with a fee or without one.
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::MedianAbsoluteDeviation,
pr::AbstractPriorResult
) -> Any
resolve_deferred_quantities(
r::MedianAbsoluteDeviation,
pr::AbstractPriorResult,
) -> Any
Resolve a Deferred Quantity in MedianAbsoluteDeviation's mu slot against prior result pr.
This is the only change that factory makes to mu. The field is tagged @vprop and not @pprop, so the prior never fills it. A bare MedianAbsoluteDeviation() keeps its MedianCentering after factory, and does not take pr.mu.
Related
PortfolioOptimisers.nothing_scalar_array_view — Method
nothing_scalar_array_view(
x::MedianCenteringFunction,
_
) -> MedianCenteringFunction
Return the MedianCenteringFunction x unchanged.
A centring function holds no value for each asset, so a view of a subset of the assets has nothing to slice.
Related
PortfolioOptimisers.calc_moment_target — Method
calc_moment_target(::MedianAbsoluteDeviation{<:Any, Nothing, <:MeanCentering, ...}, ::Any, x::VecNum)
calc_moment_target(r::MedianAbsoluteDeviation{<:Any, <:ObsWeights, <:MeanCentering, ...}, ::Any, x::VecNum)
calc_moment_target(::MedianAbsoluteDeviation{<:Any, Nothing, <:MedianCentering, ...}, ::Any, x::VecNum)
calc_moment_target(r::MedianAbsoluteDeviation{<:Any, <:ObsWeights, <:MedianCentering, ...}, ::Any, x::VecNum)
calc_moment_target(r::MedianAbsoluteDeviation{<:Any, <:Any, <:VecNum, ...}, w::VecNum, ::Any)
calc_moment_target(r::MedianAbsoluteDeviation{<:Any, <:Any, <:Number, ...}, ::Any, ::Any)
calc_moment_target(r::MedianAbsoluteDeviation{<:Any, <:Any, <:VecScalar, ...}, w::VecNum, ::Any)Compute the centre $\mu$ about which MedianAbsoluteDeviation takes the absolute deviations.
The method dispatches on the types of r.w and r.mu. A centring function reads the net series x, and a stated mu reads the asset weights. Observation weights in r.w change the centre of a centring function, and do not change a stated mu. A per-asset mu is gross, so calc_deviations_vec subtracts the mean fee per period from it.
Mathematical definition
\[\begin{align} \mu &= \begin{cases} \underset{1 \leq t \leq T}{\mathrm{median}} \, x_t & \texttt{MedianCentering}\,, \\ \frac{1}{T} \sum_{t=1}^{T} x_t & \texttt{MeanCentering}\,, \\ \boldsymbol{w}^\intercal \boldsymbol{\mu} & \texttt{mu} \text{ is a vector}\,, \\ \boldsymbol{w}^\intercal \boldsymbol{v} + s & \texttt{mu} \text{ is a } \texttt{VecScalar}\,, \\ \mu_0 & \texttt{mu} \text{ is a number}\,. \end{cases} \end{align}\]
Where:
- $\boldsymbol{x}$: Portfolio returns vector $T \times 1$.
- $T$: Number of observations.
- $\boldsymbol{w}$: Portfolio weights vector $N \times 1$.
- $\boldsymbol{\mu}$: Vector
mu, one centre for each asset. - $\boldsymbol{v}$, $s$: The fields
vandsof aVecScalarmu. - $\mu_0$: Number
mu.
When r.w holds observation weights $\omega_t$, the first case is the weighted median Statistics.median(x, w), and the second case is $\sum_{t} \omega_t x_t / \sum_{t} \omega_t$.
Related
PortfolioOptimisers.calc_deviations_vec — Function
calc_deviations_vec(
r::MedianAbsoluteDeviation,
w::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}}
) -> Any
calc_deviations_vec(
r::MedianAbsoluteDeviation,
w::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
fees::Union{Nothing, Fees}
) -> Any
Compute the deviations $\delta_t$ of the net portfolio returns from the centre of MedianAbsoluteDeviation r.
Algorithm
- Compute
x, the portfolio returns net offees, withcalc_net_returns. - Compute
tgt, the centre ofcalc_moment_targetless the mean fee per observation ofmoment_target_fees. The fee is not zero only for a per-assetmu, which states a gross expected return. - Return
x .- tgt.
Related
PortfolioOptimisers.calc_deviations_vec — Method
calc_deviations_vec(
r::MedianAbsoluteDeviation,
x::AbstractVector{<:Union{Number, AbstractJuMPScalar}}
) -> Any
Compute the deviations $\delta_t$ of a precomputed series of portfolio returns x from the centre of MedianAbsoluteDeviation r.
The precomputed-returns functor r(x::VecNum) calls this method. The series carries no weights, so the centre is one that supports_precomputed_returns accepts: a MedianCenteringFunction or a number. A per-asset mu raises a MethodError.
Related
PortfolioOptimisers.weight_independent_target — Method
weight_independent_target(
_::MedianCenteringFunction
) -> Bool
Return true. A MedianCenteringFunction reads the return series and no asset weights, so a bare return series is enough to evaluate it.
Related