Conditional X at Risk: private API

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

source
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

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(
    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

  1. Compute the effective observation weights ws, the measure's own w or else the weights of pr.
  2. Read s, the series marker, with calibration_series.
  3. Compute n, the count of the rows that the programme prices, with scenario_sample_size.
  4. Resolve alpha with resolve_calibration_slot.
  5. Resolve l, with a context that carries the resolved alpha, s and n.
  6. Resolve r, with a context that carries s and n.
  7. Rebuild the measure with rebuild_with_slots. It returns x itself when all three slots already held numbers, so the common case allocates nothing.

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(
    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

  1. Compute the effective observation weights ws, the measure's own w or else the weights of pr.
  2. Read se, the series marker, with calibration_series.
  3. Resolve alpha and beta with resolve_calibration_slot.
  4. Compute n, the count of the rows that the programme prices, with scenario_sample_size.
  5. Build three contexts, each with se and n: cs carries them alone, ca carries the resolved alpha too, and cb carries the resolved beta too.
  6. Resolve l_a in ca, r_a in cs, l_b in cb and r_b in cs.
  7. Rebuild the measure with rebuild_with_slots, which returns x itself when every slot already held a number.

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(
    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

  1. Compute the effective observation weights ws, the measure's own w or else the weights of pr.
  2. Read s, the series marker, which is AbsoluteDrawdownSeries.
  3. Compute n, the count of the rows that the programme prices, with scenario_sample_size.
  4. Resolve alpha with resolve_calibration_slot.
  5. Resolve l, with a context that carries the resolved alpha, s and n.
  6. Resolve r, with a context that carries s and n.
  7. Rebuild the measure with rebuild_with_slots, which returns x itself when all three slots already held numbers.

Related

source
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) -> Number

Reduces 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 w is nothing, 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:

  1. Compute aT, which is $\alpha T$, and idx, which is $\lfloor \alpha T \rfloor + 1$, capped at $T$.
  2. Partially sort dd in place, so that its idx smallest entries come first, in ascending order.
  3. Set var to minus the entry at idx, the Drawdown-at-Risk.
  4. Sum dd[i] + var over the idx - 1 entries before it, giving sum_var.
  5. Return var - sum_var / aT.

A weights vector w:

  1. Sort dd with sortperm, giving order, and accumulate the sorted weights, giving cum_w.
  2. Set alpha to sum(w) * alpha, and find idx, the first position at which cum_w exceeds it. A rounding error in cum_w can put idx one past the end, and then idx moves back to the last position.
  3. If idx is one, return minus the smallest drawdown.
  4. Otherwise, return minus the weighted sum of the idx - 1 smallest drawdowns plus the boundary drawdown times alpha - cum_w[idx - 1], divided by alpha.

Arguments

  • dd::VecNum: Drawdown series, each entry $\leq 0$. The unweighted method reorders it.
  • alpha::Real: Significance level, 0 < alpha < 1.
  • w: Observation weights from get_observation_weights, or nothing for the unweighted method.

Returns

  • Number: Conditional Drawdown-at-Risk, returned as a positive loss.

Related

source