| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2003 RiskMap srl |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | /*! \file riskstatistics.hpp |
| 21 | \brief empirical-distribution risk measures |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_risk_statistics_h |
| 25 | #define quantlib_risk_statistics_h |
| 26 | |
| 27 | #include <ql/math/statistics/gaussianstatistics.hpp> |
| 28 | |
| 29 | namespace QuantLib { |
| 30 | |
| 31 | //! empirical-distribution risk measures |
| 32 | /*! This class wraps a somewhat generic statistic tool and adds |
| 33 | a number of risk measures (e.g.: value-at-risk, expected |
| 34 | shortfall, etc.) based on the data distribution as reported by |
| 35 | the underlying statistic tool. |
| 36 | |
| 37 | \todo add historical annualized volatility |
| 38 | |
| 39 | */ |
| 40 | template <class S> |
| 41 | class GenericRiskStatistics : public S { |
| 42 | public: |
| 43 | typedef typename S::value_type value_type; |
| 44 | |
| 45 | /*! returns the variance of observations below the mean, |
| 46 | \f[ \frac{N}{N-1} |
| 47 | \mathrm{E}\left[ (x-\langle x \rangle)^2 \;|\; |
| 48 | x < \langle x \rangle \right]. \f] |
| 49 | |
| 50 | See Markowitz (1959). |
| 51 | */ |
| 52 | Real semiVariance() const; |
| 53 | |
| 54 | /*! returns the semi deviation, defined as the |
| 55 | square root of the semi variance. |
| 56 | */ |
| 57 | Real semiDeviation() const; |
| 58 | |
| 59 | /*! returns the variance of observations below 0.0, |
| 60 | \f[ \frac{N}{N-1} |
| 61 | \mathrm{E}\left[ x^2 \;|\; x < 0\right]. \f] |
| 62 | */ |
| 63 | Real downsideVariance() const; |
| 64 | |
| 65 | /*! returns the downside deviation, defined as the |
| 66 | square root of the downside variance. |
| 67 | */ |
| 68 | Real downsideDeviation() const; |
| 69 | |
| 70 | /*! returns the variance of observations below target, |
| 71 | \f[ \frac{N}{N-1} |
| 72 | \mathrm{E}\left[ (x-t)^2 \;|\; |
| 73 | x < t \right]. \f] |
| 74 | |
| 75 | See Dembo and Freeman, "The Rules Of Risk", Wiley (2001). |
| 76 | */ |
| 77 | Real regret(Real target) const; |
| 78 | |
| 79 | //! potential upside (the reciprocal of VAR) at a given percentile |
| 80 | Real potentialUpside(Real percentile) const; |
| 81 | |
| 82 | //! value-at-risk at a given percentile |
| 83 | Real valueAtRisk(Real percentile) const; |
| 84 | |
| 85 | //! expected shortfall at a given percentile |
| 86 | /*! returns the expected loss in case that the loss exceeded |
| 87 | a VaR threshold, |
| 88 | |
| 89 | \f[ \mathrm{E}\left[ x \;|\; x < \mathrm{VaR}(p) \right], \f] |
| 90 | |
| 91 | that is the average of observations below the |
| 92 | given percentile \f$ p \f$. |
| 93 | Also know as conditional value-at-risk. |
| 94 | |
| 95 | See Artzner, Delbaen, Eber and Heath, |
| 96 | "Coherent measures of risk", Mathematical Finance 9 (1999) |
| 97 | */ |
| 98 | Real expectedShortfall(Real percentile) const; |
| 99 | |
| 100 | /*! probability of missing the given target, defined as |
| 101 | \f[ \mathrm{E}\left[ \Theta \;|\; (-\infty,\infty) \right] \f] |
| 102 | where |
| 103 | \f[ \Theta(x) = \left\{ |
| 104 | \begin{array}{ll} |
| 105 | 1 & x < t \\ |
| 106 | 0 & x \geq t |
| 107 | \end{array} |
| 108 | \right. \f] |
| 109 | */ |
| 110 | Real shortfall(Real target) const; |
| 111 | |
| 112 | /*! averaged shortfallness, defined as |
| 113 | \f[ \mathrm{E}\left[ t-x \;|\; x<t \right] \f] |
| 114 | */ |
| 115 | Real averageShortfall(Real target) const; |
| 116 | }; |
| 117 | |
| 118 | |
| 119 | //! default risk measures tool |
| 120 | /*! \test the correctness of the returned values is tested by |
| 121 | checking them against numerical calculations. |
| 122 | */ |
| 123 | typedef GenericRiskStatistics<GaussianStatistics> RiskStatistics; |
| 124 | |
| 125 | |
| 126 | |
| 127 | // inline definitions |
| 128 | |
| 129 | template <class S> |
| 130 | inline Real GenericRiskStatistics<S>::semiVariance() const { |
| 131 | return regret(target: this->mean()); |
| 132 | } |
| 133 | |
| 134 | template <class S> |
| 135 | inline Real GenericRiskStatistics<S>::semiDeviation() const { |
| 136 | return std::sqrt(semiVariance()); |
| 137 | } |
| 138 | |
| 139 | template <class S> |
| 140 | inline Real GenericRiskStatistics<S>::downsideVariance() const { |
| 141 | return regret(target: 0.0); |
| 142 | } |
| 143 | |
| 144 | template <class S> |
| 145 | inline Real GenericRiskStatistics<S>::downsideDeviation() const { |
| 146 | return std::sqrt(downsideVariance()); |
| 147 | } |
| 148 | |
| 149 | // template definitions |
| 150 | |
| 151 | template <class S> |
| 152 | Real GenericRiskStatistics<S>::regret(Real target) const { |
| 153 | // average over the range below the target |
| 154 | std::pair<Real, Size> result = this->expectationValue( |
| 155 | [=](Real xi) -> Real { |
| 156 | Real d = (xi - target); |
| 157 | return d * d; |
| 158 | }, |
| 159 | [=](Real xi) -> bool { return xi < target; }); |
| 160 | Real x = result.first; |
| 161 | Size N = result.second; |
| 162 | QL_REQUIRE(N > 1, |
| 163 | "samples under target <= 1, unsufficient" ); |
| 164 | return (N/(N-1.0))*x; |
| 165 | } |
| 166 | |
| 167 | /*! \pre percentile must be in range [90%-100%) */ |
| 168 | template <class S> |
| 169 | Real GenericRiskStatistics<S>::potentialUpside(Real centile) |
| 170 | const { |
| 171 | QL_REQUIRE(centile>=0.9 && centile<1.0, |
| 172 | "percentile (" << centile << ") out of range [0.9, 1.0)" ); |
| 173 | |
| 174 | // potential upside must be a gain, i.e., floored at 0.0 |
| 175 | return std::max<Real>(this->percentile(centile), 0.0); |
| 176 | } |
| 177 | |
| 178 | /*! \pre percentile must be in range [90%-100%) */ |
| 179 | template <class S> |
| 180 | Real GenericRiskStatistics<S>::valueAtRisk(Real centile) const { |
| 181 | |
| 182 | QL_REQUIRE(centile>=0.9 && centile<1.0, |
| 183 | "percentile (" << centile << ") out of range [0.9, 1.0)" ); |
| 184 | |
| 185 | // must be a loss, i.e., capped at 0.0 and negated |
| 186 | return -std::min<Real>(this->percentile(1.0-centile), 0.0); |
| 187 | } |
| 188 | |
| 189 | /*! \pre percentile must be in range [90%-100%) */ |
| 190 | template <class S> |
| 191 | Real GenericRiskStatistics<S>::expectedShortfall(Real centile) const { |
| 192 | QL_REQUIRE(centile>=0.9 && centile<1.0, |
| 193 | "percentile (" << centile << ") out of range [0.9, 1.0)" ); |
| 194 | |
| 195 | QL_ENSURE(this->samples() != 0, "empty sample set" ); |
| 196 | Real target = -valueAtRisk(centile); |
| 197 | std::pair<Real,Size> result = |
| 198 | this->expectationValue([ ](Real xi) { return xi; }, |
| 199 | [=](Real xi) { return xi < target; }); |
| 200 | Real x = result.first; |
| 201 | Size N = result.second; |
| 202 | QL_ENSURE(N != 0, "no data below the target" ); |
| 203 | // must be a loss, i.e., capped at 0.0 and negated |
| 204 | return -std::min<Real>(a: x, b: 0.0); |
| 205 | } |
| 206 | |
| 207 | template <class S> |
| 208 | Real GenericRiskStatistics<S>::shortfall(Real target) const { |
| 209 | QL_ENSURE(this->samples() != 0, "empty sample set" ); |
| 210 | return this->expectationValue([=](Real x) -> Real { return x < target ? 1.0 : 0.0; }).first; |
| 211 | } |
| 212 | |
| 213 | template <class S> |
| 214 | Real GenericRiskStatistics<S>::averageShortfall(Real target) |
| 215 | const { |
| 216 | std::pair<Real,Size> result = this->expectationValue( |
| 217 | [=](Real xi) -> Real { return target - xi; }, |
| 218 | [=](Real xi) { return xi < target; }); |
| 219 | Real x = result.first; |
| 220 | Size N = result.second; |
| 221 | QL_ENSURE(N != 0, "no data below the target" ); |
| 222 | return x; |
| 223 | } |
| 224 | |
| 225 | } |
| 226 | |
| 227 | |
| 228 | #endif |
| 229 | |
| 230 | |