Conditional X at Risk: private API
PortfolioOptimisers.RMCVaR — Type
const RMCVaR{T} = Union{...}Groups the two measures whose functor computes the plain CVaR of a return series.
The robust measure cannot evaluate its robust term on a return series, so it shares the functor of the plain measure, and this union is the type that functor dispatches on. T is the type of the observation weights, so RMCVaR{Nothing} selects the unweighted method.
Related
PortfolioOptimisers.RMCVaRRg — Type
const RMCVaRRg{T} = Union{...}Groups the two measures whose functor computes the plain CVaR Range of a return series.
The robust range cannot evaluate its robust terms on a return series, so it shares the functor of the plain range, and this union is the type that functor dispatches on. T is the type of the observation weights, so RMCVaRRg{Nothing} selects the unweighted method.
Related
PortfolioOptimisers.RMCDaR — Type
const RMCDaR{T} = Union{...}Groups the two measures whose functor computes the plain CDaR of a return series.
The robust measure cannot evaluate its robust term on a return series, so it shares the functor of the plain measure, and this union is the type that functor dispatches on. T bounds the type of the observation weights. The functor dispatches on the whole union and lets conditional_drawdown_at_risk select the weighted or the unweighted method.
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(
x::DistributionallyRobustConditionalValueatRisk,
pr::AbstractPriorResult
) -> DistributionallyRobustConditionalValueatRisk
resolve_deferred_quantities(
x::DistributionallyRobustConditionalValueatRisk,
pr::AbstractPriorResult,
slv
) -> DistributionallyRobustConditionalValueatRisk
Resolve the significance level alpha, the tail weight l and the ambiguity radius r of a DistributionallyRobustConditionalValueatRisk against prior result pr.
Each of the three slots takes a Calibration Rule in place of a number. The rebuild calls the inner constructor, so a rule that returns a value the slot does not admit meets the same check as a number the caller writes, at fold time.
alpha resolves before l, because TailTermParity prices the tail term at the measure's own significance level. The two form a travelling pair, and the resolved alpha reaches the l slot in its CalibrationContext. A number, a plain function and a rule that reads no sibling all ignore that field. The radius reads neither slot.
Both contexts also carry the series that the measure prices. For this measure it is the returns, which is the default of calibration_series. The method passes it all the same, because the marker belongs to the measure, and a rule carries no marker of its own.
Both contexts also carry n, the count of the empirical measure that the ball is centred on. The programme holds the rows of pr.X, weighted by ws, so scenario_sample_size gives that count. Under a Scenario Cap it is the rows the result carries, and not the observations behind the moments. A radius rule that reads a count reads this one, so the ball has the size that the concentration bound states for the rows it is drawn around.
The measure holds no solver, so the rule receives the slv that the caller passes, which is nothing by default.
Algorithm
- Compute the effective observation weights
ws, the measure's ownwor else the weights ofpr. - Read
s, the series marker, withcalibration_series. - Compute
n, the count of the rows that the programme prices, withscenario_sample_size. - Resolve
alphawithresolve_calibration_slot. - Resolve
l, with a context that carries the resolvedalpha,sandn. - Resolve
r, with a context that carriessandn. - Rebuild the measure with
rebuild_with_slots. It returnsxitself when all three slots already held numbers, so the common case allocates nothing.
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(
x::DistributionallyRobustConditionalValueatRiskRange,
pr::AbstractPriorResult
) -> DistributionallyRobustConditionalValueatRiskRange
resolve_deferred_quantities(
x::DistributionallyRobustConditionalValueatRiskRange,
pr::AbstractPriorResult,
slv
) -> DistributionallyRobustConditionalValueatRiskRange
Resolve the two significance levels, the two tail weights and the two ambiguity radii of a DistributionallyRobustConditionalValueatRiskRange against prior result pr.
Each tail keeps its own level, weight and radius, so six slots resolve here. The method keeps the rules of the scalar measure. The rebuild re-runs every check of the constructor, and the method returns a measure whose six slots all hold numbers unchanged.
Each tail weight reads the level of its own tail. l_a reads alpha and l_b reads beta, so on a skewed sample TailTermParity gives the two tails two different weights, which is what the rule is for on a range measure. A radius reads neither level, and each radius resolves independently of the other.
Both tails price one series, the returns, so the same marker stands in all three contexts. The series belongs to the measure, and the level belongs to a tail.
Both balls are centred on the empirical measure of the same rows, so the same count n stands in all three contexts too. scenario_sample_size gives it, for the reason that the scalar measure states.
Algorithm
- Compute the effective observation weights
ws, the measure's ownwor else the weights ofpr. - Read
se, the series marker, withcalibration_series. - Resolve
alphaandbetawithresolve_calibration_slot. - Compute
n, the count of the rows that the programme prices, withscenario_sample_size. - Build three contexts, each with
seandn:cscarries them alone,cacarries the resolvedalphatoo, andcbcarries the resolvedbetatoo. - Resolve
l_ainca,r_aincs,l_bincbandr_bincs. - Rebuild the measure with
rebuild_with_slots, which returnsxitself when every slot already held a number.
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(
x::DistributionallyRobustConditionalDrawdownatRisk,
pr::AbstractPriorResult
) -> DistributionallyRobustConditionalDrawdownatRisk
resolve_deferred_quantities(
x::DistributionallyRobustConditionalDrawdownatRisk,
pr::AbstractPriorResult,
slv
) -> DistributionallyRobustConditionalDrawdownatRisk
Resolve the significance level alpha, the tail weight l and the ambiguity radius r of a DistributionallyRobustConditionalDrawdownatRisk against prior result pr.
The steps are those of the method for DistributionallyRobustConditionalValueatRisk, except for the series. This measure prices drawdowns, so calibration_series gives AbsoluteDrawdownSeries, and the marker reaches both the l slot and the r slot. TailTermParity then prices the mean drawdown of each column against the $\mathrm{CDaR}_{\alpha}$ of that column, and a radius rule reads its scale off the drawdown sample. The keys :l and :r are the same on the two measures, so the marker is the only thing that tells a rule which quantity it prices.
A drawdown series holds one entry per row of pr.X, so both contexts carry the count n that scenario_sample_size gives, as on the returns.
Algorithm
- Compute the effective observation weights
ws, the measure's ownwor else the weights ofpr. - Read
s, the series marker, which isAbsoluteDrawdownSeries. - Compute
n, the count of the rows that the programme prices, withscenario_sample_size. - Resolve
alphawithresolve_calibration_slot. - Resolve
l, with a context that carries the resolvedalpha,sandn. - Resolve
r, with a context that carriessandn. - Rebuild the measure with
rebuild_with_slots, which returnsxitself when all three slots already held numbers.
Related
PortfolioOptimisers.conditional_drawdown_at_risk — Function
conditional_drawdown_at_risk(dd::VecNum, alpha::Real, ::Nothing) -> Number
conditional_drawdown_at_risk(dd::VecNum, alpha::Real, w::VecNum) -> NumberReduces a drawdown series to its Conditional Drawdown-at-Risk at level alpha.
ConditionalDrawdownatRisk, DistributionallyRobustConditionalDrawdownatRisk and RelativeConditionalDrawdownatRisk all call it. They differ only in the drawdown series they pass, from absolute_drawdown_vec or relative_drawdown_vec. The unweighted method reorders dd in place, so a caller passes a vector it computed for the call.
Mathematical definition
\[\begin{align} \mathrm{CDaR}_{\alpha}(\boldsymbol{d}) &= -\frac{1}{\alpha W_{T}} \left( \sum_{k=1}^{k^{\star} - 1} w_{(k)} d_{(k)} + \left(\alpha W_{T} - W_{k^{\star} - 1}\right) d_{(k^{\star})} \right)\,. \end{align}\]
Where:
- $\mathrm{CDaR}_{\alpha}(\boldsymbol{d})$: Conditional Drawdown-at-Risk of the drawdown series, a non-negative loss.
- $\boldsymbol{d}$: Drawdown series $T \times 1$, each entry $\leq 0$.
- $\alpha$: Significance level (left tail probability), $\alpha \in (0, 1)$.
- $T$: Number of observations.
- $w_{t}$: Observation weight of observation $t$. Every $w_{t}$ is one when
wisnothing, and then $W_{T} = T$ and $k^{\star} = \lfloor \alpha T \rfloor + 1$. - $x_{(k)}$: $k$-th smallest entry of the series, and $w_{(k)}$ its observation weight. Here the series is $\boldsymbol{d}$, so the entry is $d_{(k)}$.
- $W_{k} = \sum_{j=1}^{k} w_{(j)}$: Cumulative weight of the $k$ smallest entries, with $W_{0} = 0$.
- $k^{\star} = \min\{k : W_{k} > \alpha W_{T}\}$: Boundary index, the position of the one entry that the tail holds in part. It is the position of the upper $\alpha$-quantile of the series, the order statistic of the Value-at-Risk.
It is the closed form of the Rockafellar-Uryasev minimum in the definition of ConditionalValueatRisk, with the drawdowns in place of the returns.
Algorithm
The third argument selects the method.
nothing:
- Compute
aT, which is $\alpha T$, andidx, which is $\lfloor \alpha T \rfloor + 1$, capped at $T$. - Partially sort
ddin place, so that itsidxsmallest entries come first, in ascending order. - Set
varto minus the entry atidx, the Drawdown-at-Risk. - Sum
dd[i] + varover theidx - 1entries before it, givingsum_var. - Return
var - sum_var / aT.
A weights vector w:
- Sort
ddwithsortperm, givingorder, and accumulate the sorted weights, givingcum_w. - Set
alphatosum(w) * alpha, and findidx, the first position at whichcum_wexceeds it. A rounding error incum_wcan putidxone past the end, and thenidxmoves back to the last position. - If
idxis one, return minus the smallest drawdown. - Otherwise, return minus the weighted sum of the
idx - 1smallest drawdowns plus the boundary drawdown timesalpha - cum_w[idx - 1], divided byalpha.
Arguments
dd::VecNum: Drawdown series, each entry $\leq 0$. The unweighted method reorders it.alpha::Real: Significance level,0 < alpha < 1.w: Observation weights fromget_observation_weights, ornothingfor the unweighted method.
Returns
Number: Conditional Drawdown-at-Risk, returned as a positive loss.
Related