QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
riskstatistics.hpp
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
24#ifndef quantlib_risk_statistics_h
25#define quantlib_risk_statistics_h
26
27#include <ql/math/statistics/gaussianstatistics.hpp>
28
29namespace QuantLib {
30
32
40 template <class S>
41 class GenericRiskStatistics : public S {
42 public:
43 typedef typename S::value_type value_type;
44
52 Real semiVariance() const;
53
57 Real semiDeviation() const;
58
63 Real downsideVariance() const;
64
68 Real downsideDeviation() const;
69
77 Real regret(Real target) const;
78
80 Real potentialUpside(Real percentile) const;
81
83 Real valueAtRisk(Real percentile) const;
84
86
98 Real expectedShortfall(Real percentile) const;
99
110 Real shortfall(Real target) const;
111
115 Real averageShortfall(Real target) const;
116 };
117
118
120
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
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
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
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
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