Non-Optimisation Risk Measures: private API
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::ThirdCentralMoment,
pr::AbstractPriorResult
) -> ThirdCentralMoment
resolve_deferred_quantities(
r::ThirdCentralMoment,
pr::AbstractPriorResult,
) -> ThirdCentralMoment
Resolve a Deferred Quantity in ThirdCentralMoment's mu slot against prior result pr. The measure carries one prior-derived slot, so the slot itself admits the estimator and there is no fan-out to make.
Related
PortfolioOptimisers.calc_moment_target — Method
calc_moment_target(::ThirdCentralMoment{<:Any, Nothing, Nothing}, ::Any, x::VecNum)
calc_moment_target(r::ThirdCentralMoment{<:Any, <:StatsBase.AbstractWeights, Nothing}, ::Any, x::VecNum)
calc_moment_target(r::ThirdCentralMoment{<:Any, <:Any, <:VecNum}, w::VecNum, ::Any)
calc_moment_target(r::ThirdCentralMoment{<:Any, <:Any, <:VecScalar}, w::VecNum, ::Any)
calc_moment_target(r::ThirdCentralMoment{<:Any, <:Any, <:Number}, ::Any, ::Any)Compute the centre $\mu$ about which ThirdCentralMoment takes its moment.
The method dispatches on the types of r.w and r.mu. Observation weights in r.w change the mean of x, 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} \frac{1}{T} \sum_{t=1}^{T} x_t & \texttt{mu} \text{ and } \texttt{w} \text{ are } \texttt{nothing}\,, \\ \sum_{t=1}^{T} \omega_t x_t \Big/ \sum_{t=1}^{T} \omega_t & \texttt{mu} \text{ is } \texttt{nothing} \text{ and } \texttt{w} \text{ holds weights}\,, \\ \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.
- $\omega_t$: Observation weight of observation $t$, from
r.w. - $\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.
Related
PortfolioOptimisers.calc_deviations_vec — Function
calc_deviations_vec(
r::ThirdCentralMoment,
w::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}}
) -> Any
calc_deviations_vec(
r::ThirdCentralMoment,
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 a ThirdCentralMoment 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::ThirdCentralMoment,
x::AbstractVector{<:Union{Number, AbstractJuMPScalar}}
) -> Any
Compute the deviations $\delta_t$ of a precomputed series of portfolio returns x from the centre of a ThirdCentralMoment 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: the mean of x, its weighted mean, or a number. A per-asset mu raises a MethodError.
Related
PortfolioOptimisers.reader_frames — Method
reader_frames(r::MeanReturnRiskRatio, path::AbstractString,
refit::Bool) -> ReturnsFramesReturn the frames of the readers inside the risk side of a MeanReturnRiskRatio, for the frame walk of a head.
The risk side rk can hold a risk measure with a reader, such as an UncertaintySetVariance. Each of those readers belongs to the group of the head that holds the measure, so the method walks every field of r with field_frames. A field that holds no reader adds nothing.
Arguments
r: The risk measure.path: The path of the value from the head, for example"pe","cle"or"r[2].pe". A warning names a reader by its path.refit:truewhen a regression reader atx_src = nothingin the group fits its regression on the raw returns data.refits_regressionfinds the value.
Returns
fr::ReturnsFrames: The frames of the fields ofr.
Related