QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
riskstatistics.hpp
Go to the documentation of this file.
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
28
29namespace 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 */
124
125
126
127 // inline definitions
128
129 template <class S>
131 return regret(this->mean());
132 }
133
134 template <class S>
136 return std::sqrt(semiVariance());
137 }
138
139 template <class S>
141 return regret(0.0);
142 }
143
144 template <class S>
146 return std::sqrt(downsideVariance());
147 }
148
149 // template definitions
150
151 template <class S>
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>
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>
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>
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>(x, 0.0);
205 }
206
207 template <class S>
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>
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
empirical-distribution risk measures
Real valueAtRisk(Real percentile) const
value-at-risk at a given percentile
Real potentialUpside(Real percentile) const
potential upside (the reciprocal of VAR) at a given percentile
Real averageShortfall(Real target) const
Real expectedShortfall(Real percentile) const
expected shortfall at a given percentile
Real shortfall(Real target) const
Real regret(Real target) const
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
Definition: errors.hpp:130
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
statistics tool for gaussian-assumption risk measures
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
GenericRiskStatistics< GaussianStatistics > RiskStatistics
default risk measures tool