Worst-Case Distribution: private API
PortfolioOptimisers.expand_positive_weight_rows — Function
expand_positive_weight_rows(qr::VecNum, rows::Base.OneTo, T::Integer) -> VecNum
expand_positive_weight_rows(qr::VecNum, rows::AbstractVector{<:Integer}, T::Integer)
-> VectorExpand a vector with one entry for each observation of positive weight to one entry for each observation.
A programme of ERM, RRM or PRM has one row for each index of positive_weight_rows. The Worst-Case Distribution that ERM_worst_case, RRM_worst_case and PRM_worst_case read off those rows has the same length. An observation of weight zero has no probability, so its entry is zero.
Arguments
qr: Vector with one entry for each index ofrows.rows: Indices frompositive_weight_rows.T: Number of observations.
Returns
qritself whenrowsholds every observation. Otherwise, a new vector of lengthTthat holdsqratrowsand zero elsewhere.
Related
PortfolioOptimisers.worst_loss_distribution — Function
worst_loss_distribution(x::VecNum, ::Nothing) -> Vector
worst_loss_distribution(x::VecNum, w::VecNum) -> VectorReturn the Worst-Case Distribution of a measure whose value is the largest loss among the observations of positive weight.
The distribution puts all its mass on the observations at the loss of worst_positive_weight_loss, in proportion to their weights. ERM_worst_case reads it at alpha = 0, and PRM_worst_case reads it under the condition of PowerNormValueatRisk.
Mathematical definition
\[\begin{align} L^{*} &= \underset{t \in \mathcal{I}}{\max} \; (-x_t)\,, \\ q_t &= \frac{w_t \, \mathbb{1}\{-x_t = L^{*}\}}{\sum_{s \in \mathcal{I}} w_s \, \mathbb{1}\{-x_s = L^{*}\}}\,, \quad t \in \mathcal{I}\,, \\ q_t &= 0\,, \quad t \notin \mathcal{I}\,. \end{align}\]
Where:
- $L^{*}$: Largest loss among the observations of positive weight.
- $\boldsymbol{q}$: Worst-Case Distribution $T \times 1$, one probability for each observation. Its entries are not negative, and they sum to one.
- $\boldsymbol{x}$: Portfolio returns vector $T \times 1$.
- $w_{t}$: Observation weight of observation $t$. Without observation weights, every $w_t$ is one.
- $\mathcal{I}$: Set of the observations of positive weight. Without observation weights, it holds all $T$ observations.
- $\mathbb{1}\{\cdot\}$: Indicator, one when the condition holds and zero otherwise.
The expected loss under $\boldsymbol{q}$ is $L^{*}$, so $\boldsymbol{q}$ reaches the largest loss. When two or more observations tie at $L^{*}$, every distribution on the tied observations reaches it, and the measure has no gradient. By Theorem 17 of [306], each such distribution gives a subgradient of a coherent measure. The function gives the subgradient that shares the mass by weight.
Arguments
x: Return or drawdown series. A loss is a negative entry. The function does not change it.w: Resolved observation weights fromchecked_observation_weights, ornothing.
Returns
q::Vector: The distribution, one entry for each observation. Its entries sum to one.
Related
References
- [306] F. Delbaen. Coherent risk measures. Lecture notes, Cattedra Galileiana, Scuola Normale Superiore di Pisa (2000). Section 8, Theorem 17.
PortfolioOptimisers.element_worst_case_distributions — Function
element_worst_case_distributions(ps::Nothing, r::AbstractBaseRiskMeasure, w::VecNum,
X::MatNum, fees::Option{<:Fees})
element_worst_case_distributions(ps::VecNum, rs::VecBaseRM, w::VecNum, X::MatNum,
fees::Option{<:Fees}) -> VectorCompute the Worst-Case Distribution of one measure, or of each element of a vector of measures, at the unperturbed weights w.
risk_contribution takes it once, before its finite difference, and adjusted_risk reads it at each perturbed weight vector. An element whose chain-rule weight is zero adds nothing to the risk, so the function does not solve its programme, and gives it nothing.
Arguments
ps: Chain-rule weights of a vector of measures, ornothingfor a single measure.r: Risk measure or vector of risk measures.w::VecNum: Portfolio weights vectorassets × 1.X::MatNum: Returns matrixobservations × assets.fees: OptionalFeesstructure.
Returns
- The value of
worst_case_distributionfor a single measure, and a vector of them for a vector of measures.
Related
PortfolioOptimisers.worst_case_distribution — Function
worst_case_distribution(r::AbstractBaseRiskMeasure, x::VecNum)
worst_case_distribution(r::AbstractBaseRiskMeasure, w::VecNum, X::MatNum,
fees::Option{<:Fees})Compute the Worst-Case Distribution of a measure whose value is a solve, or return nothing for every other measure.
The measures of the entropic, the relativistic and the power norm families solve a conic programme for their value. linearised_risk evaluates the expected loss under the distribution that this function returns, and risk_contribution and risk_gradient take the finite difference of that expected loss. A finite difference of the measure itself differences two solves, and the noise of the solver is as large as the change that the step makes.
The method on a return series is the one that a measure implements. Each measure of the three families reads its distribution with ERM_worst_case, RRM_worst_case or PRM_worst_case, on the series that its functor reads. A range measure returns the pair of the distributions of its loss tail and its gain tail, through worst_case_pair.
A composite that wraps other measures returns a tuple of their distributions. These composites are a vector of measures, RiskRatio, NonOptimisationRiskRatio, MeanReturnRiskRatio, RiskTrackingRiskMeasure and GenericValueatRiskRange. The tuple of the dependent tracking measure also holds the risk of the benchmark, which does not move with the weights. A tail of a GenericValueatRiskRange whose value is not a solve has nothing in the tuple. linearised_risk applies the formula of the composite to the linearised values of the measures that it wraps.
Mathematical definition
\[\begin{align} \boldsymbol{x} &= \mathbf{X} \boldsymbol{w} - F(\boldsymbol{w})\,, \\ R(\boldsymbol{w}) &= \underset{\boldsymbol{q} \in \mathcal{Q}}{\max} \; -\boldsymbol{q}^\intercal \boldsymbol{y}(\boldsymbol{x})\,, \\ \nabla_{\boldsymbol{w}} R(\boldsymbol{w}) &= -\mathbf{J}^\intercal \boldsymbol{q}^{*}\,. \end{align}\]
Where:
- $\boldsymbol{x}$: Portfolio returns vector $T \times 1$.
- $\mathbf{X}$: Returns matrix $T \times N$.
- $\boldsymbol{w}$: Portfolio weights vector $N \times 1$.
- $F(\boldsymbol{w})$: Fee series $T \times 1$ of the portfolio, zero when
feesisnothing. Seecalc_net_returns. - $R(\boldsymbol{w})$: Portfolio risk.
- $\mathcal{Q}$: Set of distributions of the measure. It is a Kullback-Leibler ball for the entropic family and a Kaniadakis ball for the relativistic family.
- $\boldsymbol{q}$: Worst-Case Distribution $T \times 1$, one probability for each observation. Its entries are not negative, and they sum to one.
- $\boldsymbol{q}^{*}$: Maximiser of the expected loss over $\mathcal{Q}$, the Worst-Case Distribution that the function returns.
- $\boldsymbol{y}(\boldsymbol{x})$: Measured series $T \times 1$, the series that the risk measure reads. It is the returns, the absolute drawdowns or the relative drawdowns.
- $\mathbf{J}$: Jacobian of $\boldsymbol{y}(\boldsymbol{x})$ with respect to $\boldsymbol{w}$, $T \times N$. For a measure of the returns without fees, it is $\mathbf{X}$.
The measures of the three families are coherent. Proposition 4.1 of [196] states that a coherent measure is the largest expected loss over a set of probability measures. That is the second line. Proposition 5 of [306] applies when the maximiser is unique. It states that the derivative of a coherent measure in a direction is then the expected loss of that direction under the maximiser. The chain rule through $\boldsymbol{y}$ and $\boldsymbol{x}$ then gives the third line, where $\boldsymbol{y}$ is differentiable. When the maximiser is not unique, the measure of $\boldsymbol{y}$ has no gradient. Theorem 17 of [306] states that each maximiser then gives a subgradient.
Algorithm
The method on the weights does these steps:
- Check the slots of
rwithassert_resolved_slotsandassert_calibrated_slots, asexpected_riskdoes. - Read the kind of
rwithrisk_input_kind. For a kind other thanNetReturnsInput, returnnothing. - Compute the net returns
xofwwithcalc_net_returns. - Return the method of
ronx.
A composite replaces these steps with its own method on the weights, which calls this function on each measure that it wraps.
Arguments
r: Risk measure or vector of risk measures.x::VecNum: Net return series of the portfolio.w::VecNum: Portfolio weights vectorassets × 1.X::MatNum: Returns matrixobservations × assets.fees: OptionalFeesstructure.
Validation
- The method on the weights raises through
assert_resolved_slotsandassert_calibrated_slots, asexpected_riskdoes.
Returns
q::VecNum: The Worst-Case Distribution, one entry for each observation. When the maximiser is not unique, it is the maximiser that the solver finds.(q_loss, q_gain): The distributions of the two tails of a range measure.Tuple: The distributions of the measures that a composite wraps.Vector: The distribution of each element of a vector of measures.nothing: For a measure whose value is not a solve, and when no solver solves the programme or gives its dual.
Related
worst_case_risklinearised_riskelement_worst_case_distributionsERM_worst_caseRRM_worst_casePRM_worst_case
References
- [196] P. Artzner, F. Delbaen, J.-M. Eber and D. Heath. Coherent measures of risk. Mathematical Finance 9, 203–228 (1999). Section 4.1, Proposition 4.1.
- [306] F. Delbaen. Coherent risk measures. Lecture notes, Cattedra Galileiana, Scuola Normale Superiore di Pisa (2000). Section 8, Theorem 17 and Proposition 5.
PortfolioOptimisers.worst_case_pair — Function
worst_case_pair(q_loss::VecNum, q_gain::VecNum) -> Tuple
worst_case_pair(q_loss::Option{<:VecNum}, q_gain::Option{<:VecNum}) -> NothingPair the Worst-Case Distributions of the loss tail and the gain tail of a range measure.
A range measure is the sum of a measure of the losses and a measure of the gains. When the programme of either tail gives no distribution, the pair is nothing, so that linearised_risk evaluates the measure itself. A tail gives a vector or nothing, so the second method takes Option{<:VecNum} on each slot, and any other type is a MethodError.
Arguments
q_loss: Worst-Case Distribution of the loss tail, ornothing.q_gain: Worst-Case Distribution of the gain tail, read on the negated series, ornothing.
Returns
(q_loss, q_gain)when both are vectors. Otherwise,nothing.
Related
PortfolioOptimisers.worst_case_risk — Function
worst_case_risk(r::AbstractBaseRiskMeasure, x::VecNum, q::VecNum) -> Number
worst_case_risk(r::AbstractBaseRiskMeasure, x::VecNum, q::Tuple) -> NumberEvaluate the expected loss of the series that r reads from the net returns x, under the fixed Worst-Case Distribution q.
A measure on the returns reads x itself. A drawdown measure implements a method that reads its drawdown series instead. A range measure carries a pair, and the gain tail is the loss of the negated series. GenericValueatRiskRange implements a method that adds its two tails through tail_worst_case_risk.
Mathematical definition
\[\begin{align} \tilde{R}(\boldsymbol{x}) &= -\boldsymbol{q}^\intercal \boldsymbol{y}(\boldsymbol{x})\,, \\ \tilde{R}_{\mathrm{range}}(\boldsymbol{x}) &= -\boldsymbol{q}_{L}^\intercal \boldsymbol{x} + \boldsymbol{q}_{G}^\intercal \boldsymbol{x}\,. \end{align}\]
Where:
- $\tilde{R}$: Linearised risk, the expected loss of the measured series under a fixed Worst-Case Distribution.
- $\boldsymbol{q}$: Worst-Case Distribution $T \times 1$, one probability for each observation. Its entries are not negative, and they sum to one.
- $\boldsymbol{y}(\boldsymbol{x})$: Measured series $T \times 1$, the series that the risk measure reads. It is the returns, the absolute drawdowns or the relative drawdowns.
- $\boldsymbol{q}_{L}$, $\boldsymbol{q}_{G}$: Worst-Case Distributions of the loss tail and of the gain tail of a range measure.
- $\boldsymbol{x}$: Portfolio returns vector $T \times 1$.
At the series that gave $\boldsymbol{q}$, $\tilde{R}$ equals the value of the measure, to the tolerance of the solver. Its gradient with respect to $\boldsymbol{x}$ is the gradient of the measure, as worst_case_distribution states.
Arguments
r: Risk measure or vector of risk measures.x::VecNum: Net return series of the portfolio.q: Worst-Case Distribution, or the pair of a range measure.
Returns
rk::Number: The expected loss underq.
Related
PortfolioOptimisers.tail_worst_case_risk — Function
tail_worst_case_risk(q::Nothing, r::AbstractBaseRiskMeasure, x::VecNum) -> Number
tail_worst_case_risk(q::VecNum, r::AbstractBaseRiskMeasure, x::VecNum) -> NumberEvaluate one tail of a GenericValueatRiskRange on the series x, with or without the Worst-Case Distribution of the tail.
Each tail of the range is a measure of its own, so a tail whose value is not a solve keeps its own value. worst_case_risk of the range adds the loss tail on the net returns and the gain tail on the negated net returns, as the functor of the range does.
Arguments
q: Worst-Case Distribution of the tail fromworst_case_distribution, ornothing.r: Tail measure, a member ofValueatRiskRMs.x::VecNum: Series of the tail. It is the net returns for the loss tail and the negated net returns for the gain tail.
Returns
rk::Number: The value ofronxwithoutq, andworst_case_riskofronxunderqwith it.
Related
PortfolioOptimisers.linearised_risk — Function
linearised_risk(q::Nothing, r::AbstractBaseRiskMeasure, w::VecNum, X::MatNum,
fees::Option{<:Fees}; kwargs...)
linearised_risk(q, r::AbstractBaseRiskMeasure, w::VecNum, X::MatNum,
fees::Option{<:Fees}; kwargs...)
linearised_risk(sca::Option{<:Scalariser}, qs::AbstractVector, rs::VecBaseRM, w::VecNum, X::MatNum,
fees::Option{<:Fees}; kwargs...)
linearised_risk(sca::Option{<:Scalariser}, q, r::AbstractBaseRiskMeasure, w::VecNum, X::MatNum,
fees::Option{<:Fees}; kwargs...)Evaluate the risk of r at w, or its expected loss under the fixed Worst-Case Distribution q, for a finite difference.
Without a distribution, the function returns expected_risk. With one, it returns worst_case_risk of the net returns at w. Both have the same gradient at the weights that gave q, but the second differences no solve.
A composite, whose q is a tuple from worst_case_distribution, applies its own formula to the linearised values of the measures that it wraps, as its expected_risk method does to their values. A side of a composite that can hold a vector takes the scalariser sca of the composite as its first argument. A vector of measures scalarises them with sca, and a single measure ignores it. The tracking measure with independent variables reads the series of difference_risk through difference_linearised_risk.
Mathematical definition
\[\begin{align} \tilde{R}(\boldsymbol{w}) &= -\boldsymbol{q}^\intercal \boldsymbol{y}(\boldsymbol{x}(\boldsymbol{w}))\,, \\ \tilde{R}(\boldsymbol{w}_0) &= R(\boldsymbol{w}_0)\,, \\ \nabla_{\boldsymbol{w}} \tilde{R}(\boldsymbol{w}_0) &= \nabla_{\boldsymbol{w}} R(\boldsymbol{w}_0)\,. \end{align}\]
The composites apply their formulas to the linearised values of the measures that they wrap:
\[\begin{align} \tilde{R}_{\mathrm{vec}}(\boldsymbol{w}) &= S\left(s_1 \tilde{R}_1(\boldsymbol{w}), \ldots, s_K \tilde{R}_K(\boldsymbol{w})\right)\,, \\ \tilde{R}_{\mathrm{ratio}}(\boldsymbol{w}) &= \frac{\tilde{R}_1(\boldsymbol{w})}{\tilde{R}_2(\boldsymbol{w})}\,, \\ \tilde{R}_{\mathrm{mean}}(\boldsymbol{w}) &= \frac{m(\boldsymbol{w}) - r_f}{\tilde{R}_1(\boldsymbol{w})}\,, \\ \tilde{R}_{\mathrm{dep}}(\boldsymbol{w}) &= \left| \tilde{R}_1(\boldsymbol{w}) - R_1(\boldsymbol{w}_b) \right|\,. \end{align}\]
Where:
- $\tilde{R}$: Linearised risk, the expected loss of the measured series under a fixed Worst-Case Distribution.
- $\boldsymbol{q}$: Worst-Case Distribution $T \times 1$, one probability for each observation. Its entries are not negative, and they sum to one.
- $\boldsymbol{y}(\boldsymbol{x})$: Measured series $T \times 1$, the series that the risk measure reads. It is the returns, the absolute drawdowns or the relative drawdowns.
- $\boldsymbol{x}(\boldsymbol{w})$: Net return series of the portfolio at $\boldsymbol{w}$.
- $\boldsymbol{w}$: Portfolio weights vector $N \times 1$.
- $\boldsymbol{w}_0$: Portfolio weights at which the solve gave $\boldsymbol{q}$.
- $R(\boldsymbol{w})$: Portfolio risk.
- $\tilde{R}_k$, $R_k$: Linearised risk and risk of the $k$-th measure that the composite wraps. Without a distribution, $\tilde{R}_k = R_k$.
- $S$: Scalariser that combines the scaled risks of a vector of risk measures into one number.
- $s_k$:
scaleof the $k$-th risk measure of a vector, its weight in the combination. - $K$: Number of measures in the vector.
- $m(\boldsymbol{w})$: Mean net return of the portfolio, the value of the return measure of
MeanReturnRiskRatio. - $r_f$: Risk-free rate, the
rffield ofMeanReturnRiskRatio. - $\boldsymbol{w}_b$: Benchmark portfolio weights vector $N \times 1$, the
wof the tracking specificationtr.
The first line is linear in $\boldsymbol{y}$, so its finite difference holds no solver noise. The second and the third lines hold to the tolerance of the solver when $\boldsymbol{q}$ is the unique maximiser, as worst_case_distribution states. By the chain rule, each composite formula then has the gradient of the composite at $\boldsymbol{w}_0$. The ratios cover RiskRatio and NonOptimisationRiskRatio, and $\tilde{R}_{\mathrm{dep}}$ is RiskTrackingRiskMeasure with DependentVariableTracking. With IndependentVariableTracking, the tracking measure is $\tilde{R}_1$ of the active weights.
Arguments
sca: Scalariser for combining multiple risk measures.nothinggives the sum ofSumScalariser.q: Worst-Case Distribution fromworst_case_distribution, ornothing.qs: Worst-Case Distribution of each measure ofrs, ornothingfor a measure without one.r: Risk measure or vector of risk measures.rs: Vector of risk measures.w::VecNum: Portfolio weights vectorassets × 1.X::MatNum: Returns matrixobservations × assets.fees: OptionalFeesstructure.
Returns
rk::Number: The risk, or the expected loss underq.
Related
PortfolioOptimisers.difference_worst_case — Function
difference_worst_case(r::AbstractBaseRiskMeasure, wd::VecNum, w::VecNum, X::MatNum,
fees::Option{<:Fees})Compute the Worst-Case Distribution of r on the active weights wd, on the series that difference_risk reads.
RiskTrackingRiskMeasure with IndependentVariableTracking measures the risk of the active weights, and charges the fees on the portfolio weights w. Without fees, the series is the net return series of wd, so the function reads the distribution on the weights. With fees, it reads the distribution of a measure of the kind NetReturnsInput on the series that difference_risk builds, and it returns nothing for every other kind.
With fees, difference_risk decomposes each ratio and a nested RiskTrackingRiskMeasure into the measures that they wrap. So the function returns a tuple of the distributions of those measures, as worst_case_distribution does without fees. The tuple of the dependent tracking measure also holds the risk of its benchmark. The return side of MeanReturnRiskRatio and of ExpectedReturnRiskRatio is linear, so their tuple holds the distribution of the risk side alone. A vector side gives a vector of the distributions of its measures.
Mathematical definition
\[\begin{align} \boldsymbol{w}_d &= \boldsymbol{w} - \boldsymbol{w}_b\,, \\ \boldsymbol{x}_d &= \mathbf{X} \boldsymbol{w}_d - F(\boldsymbol{w})\,. \end{align}\]
Where:
- $\boldsymbol{w}_d$: Active weights vector $N \times 1$.
- $\boldsymbol{w}$: Portfolio weights vector $N \times 1$.
- $\boldsymbol{w}_b$: Benchmark portfolio weights vector $N \times 1$, the
wof the tracking specificationtr. - $\boldsymbol{x}_d$: Series of the active weights $T \times 1$, the series that
difference_riskmeasures. - $\mathbf{X}$: Returns matrix $T \times N$.
- $F(\boldsymbol{w})$: Fee series $T \times 1$ of the portfolio, zero when
feesisnothing. Seecalc_net_returns.
The function returns the Worst-Case Distribution of r on $\boldsymbol{x}_d$.
Arguments
r: Risk measure or vector of risk measures.wd::VecNum: Active weights, the portfolio weights minus the benchmark weights.w::VecNum: Portfolio weights vectorassets × 1.X::MatNum: Returns matrixobservations × assets.fees: OptionalFeesstructure.
Validation
- With fees, the function raises through
assert_resolved_slotsandassert_calibrated_slots, as the functor of the tracking measure does.
Returns
- The value of
worst_case_distributionon the series of the active weights. Tuple: With fees, the distributions of the measures that a ratio or a nested tracking measure wraps.
Related
PortfolioOptimisers.difference_linearised_risk — Function
difference_linearised_risk(q, r::AbstractBaseRiskMeasure, wd::VecNum, w::VecNum,
X::MatNum, fees::Option{<:Fees})Evaluate the risk of r on the active weights wd, or its expected loss under the fixed Worst-Case Distribution q, for a finite difference.
This is linearised_risk on the series that difference_risk reads. Without a distribution, the function returns difference_risk itself. With one, it returns worst_case_risk of r on the series of the active weights that difference_worst_case states. With fees, a ratio and a nested tracking measure apply their formulas to the linearised values of the measures that they wrap, as difference_risk does to their values. A vector side scalarises the linearised values of its measures with the scalariser of the ratio.
Arguments
q: Worst-Case Distribution fromdifference_worst_case, ornothing.r: Risk measure or vector of risk measures.wd::VecNum: Active weights, the portfolio weights minus the benchmark weights.w::VecNum: Portfolio weights vectorassets × 1.X::MatNum: Returns matrixobservations × assets.fees: OptionalFeesstructure.
Returns
rk::Number: The risk of the active weights, or its expected loss underq.
Related
References
- [196]
- P. Artzner, F. Delbaen, J.-M. Eber and D. Heath. Coherent measures of risk. Mathematical Finance 9, 203–228 (1999).
- [306]
- F. Delbaen. Coherent risk measures. Lecture notes, Cattedra Galileiana, Scuola Normale Superiore di Pisa (2000).