QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
gaussianstatistics.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2003 Ferdinando Ametrano
5 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
25#ifndef quantlib_gaussian_statistics_h
26#define quantlib_gaussian_statistics_h
27
28#include <ql/math/distributions/normaldistribution.hpp>
29#include <ql/math/statistics/generalstatistics.hpp>
30
31namespace QuantLib {
32
34
39 template<class Stat>
41 public:
42 typedef typename Stat::value_type value_type;
44 explicit GenericGaussianStatistics(const Stat& s) : Stat(s) {}
46
47
54 return gaussianRegret(0.0);
55 }
56
61 return std::sqrt(gaussianDownsideVariance());
62 }
63
69 Real gaussianRegret(Real target) const;
70
71
76 Real gaussianPercentile(Real percentile) const;
77 Real gaussianTopPercentile(Real percentile) const;
78
80 Real gaussianPotentialUpside(Real percentile) const;
81
83 Real gaussianValueAtRisk(Real percentile) const;
84
86
99 Real gaussianExpectedShortfall(Real percentile) const;
100
102 Real gaussianShortfall(Real target) const;
103
105 Real gaussianAverageShortfall(Real target) const;
107 };
108
111
112
115 public:
120 ~StatsHolder() = default;
121 Real mean() const { return mean_; }
123 private:
125 };
126
127
128 // inline definitions
129
130 template<class Stat>
131 inline
133 Real m = this->mean();
134 Real std = this->standardDeviation();
135 Real variance = std*std;
136 CumulativeNormalDistribution gIntegral(m, std);
138 Real firstTerm = variance + m*m - 2.0*target*m + target*target;
139 Real alfa = gIntegral(target);
140 Real secondTerm = m - target;
141 Real beta = variance*g(target);
142 Real result = alfa*firstTerm - beta*secondTerm;
143 return result/alfa;
144 }
145
147 template<class Stat>
149 Real percentile) const {
150
151 QL_REQUIRE(percentile>0.0,
152 "percentile (" << percentile << ") must be > 0.0");
153 QL_REQUIRE(percentile<1.0,
154 "percentile (" << percentile << ") must be < 1.0");
155
156 InverseCumulativeNormal gInverse(Stat::mean(),
157 Stat::standardDeviation());
158 return gInverse(percentile);
159 }
160
162 template<class Stat>
164 Real percentile) const {
165
166 return gaussianPercentile(1.0-percentile);
167 }
168
170 template<class Stat>
172 Real percentile) const {
173
174 QL_REQUIRE(percentile<1.0 && percentile>=0.9,
175 "percentile (" << percentile << ") out of range [0.9, 1)");
176
177 Real result = gaussianPercentile(percentile);
178 // potential upside must be a gain, i.e., floored at 0.0
179 return std::max<Real>(result, 0.0);
180 }
181
182
184 template<class Stat>
186 Real percentile) const {
187
188 QL_REQUIRE(percentile<1.0 && percentile>=0.9,
189 "percentile (" << percentile << ") out of range [0.9, 1)");
190
191 Real result = gaussianPercentile(1.0-percentile);
192 // VAR must be a loss
193 // this means that it has to be MIN(dist(1.0-percentile), 0.0)
194 // VAR must also be a positive quantity, so -MIN(*)
195 return -std::min<Real>(result, 0.0);
196 }
197
198
200 template<class Stat>
202 Real percentile) const {
203 QL_REQUIRE(percentile<1.0 && percentile>=0.9,
204 "percentile (" << percentile << ") out of range [0.9, 1)");
205
206 Real m = this->mean();
207 Real std = this->standardDeviation();
208 InverseCumulativeNormal gInverse(m, std);
209 Real var = gInverse(1.0-percentile);
211 Real result = m - std*std*g(var)/(1.0-percentile);
212 // expectedShortfall must be a loss
213 // this means that it has to be MIN(result, 0.0)
214 // expectedShortfall must also be a positive quantity, so -MIN(*)
215 return -std::min<Real>(result, 0.0);
216 }
217
218
219 template<class Stat>
221 Real target) const {
222 CumulativeNormalDistribution gIntegral(this->mean(),
223 this->standardDeviation());
224 return gIntegral(target);
225 }
226
227
228 template<class Stat>
230 Real target) const {
231 Real m = this->mean();
232 Real std = this->standardDeviation();
233 CumulativeNormalDistribution gIntegral(m, std);
235 return ( (target-m) + std*std*g(target)/gIntegral(target) );
236 }
237
238}
239
240
241#endif
Cumulative normal distribution function.
Statistics tool for gaussian-assumption risk measures.
Real gaussianAverageShortfall(Real target) const
gaussian-assumption Average Shortfall (averaged shortfallness)
Real gaussianShortfall(Real target) const
gaussian-assumption Shortfall (observations below target)
Real gaussianExpectedShortfall(Real percentile) const
gaussian-assumption Expected Shortfall at a given percentile
Real gaussianValueAtRisk(Real percentile) const
gaussian-assumption Value-At-Risk at a given percentile
Real gaussianPercentile(Real percentile) const
Real gaussianPotentialUpside(Real percentile) const
gaussian-assumption Potential-Upside at a given percentile
Real gaussianTopPercentile(Real percentile) const
Inverse cumulative normal distribution function.
Normal distribution function.
Helper class for precomputed distributions.
StatsHolder(Real mean, Real standardDeviation)
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
GenericGaussianStatistics< GeneralStatistics > GaussianStatistics
default gaussian statistic tool
STL namespace.