1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//! Gaussian-assumption risk measures.
//!
//! Port of `ql/math/statistics/gaussianstatistics.hpp`. QuantLib's
//! `GenericGaussianStatistics<Stat>` wraps any statistics tool and derives
//! risk measures from its mean and variance under a gaussian assumption; here
//! [`GaussianStatistics`] is an extension trait with default methods, blanket
//! implemented for every [`MeanStdDev`], so both accumulators and the
//! precomputed [`StatsHolder`] get the measures for free.
use crate::errors::QlResult;
use crate::fail;
use crate::math::distributions::normal::{
CumulativeNormalDistribution, InverseCumulativeNormal, NormalDistribution,
};
use crate::types::Real;
use super::MeanStdDev;
/// Precomputed mean/standard-deviation pair, for taking gaussian risk
/// measures of an already-summarized distribution.
#[derive(Clone, Copy, Debug)]
pub struct StatsHolder {
mean: Real,
standard_deviation: Real,
}
impl StatsHolder {
/// Wraps a precomputed mean and standard deviation.
pub fn new(mean: Real, standard_deviation: Real) -> Self {
StatsHolder {
mean,
standard_deviation,
}
}
}
impl MeanStdDev for StatsHolder {
fn mean(&self) -> QlResult<Real> {
Ok(self.mean)
}
fn standard_deviation(&self) -> QlResult<Real> {
Ok(self.standard_deviation)
}
}
/// Gaussian-assumption risk measures derived from a mean and a standard
/// deviation.
pub trait GaussianStatistics: MeanStdDev {
/// Gaussian-assumption `percentile`-th percentile, for `percentile` in
/// `(0, 1)`.
///
/// # Errors
///
/// Returns an error if `percentile` lies outside `(0, 1)` or the
/// underlying moments are unavailable.
fn gaussian_percentile(&self, percentile: Real) -> QlResult<Real> {
if percentile <= 0.0 {
fail!("percentile ({percentile}) must be > 0.0");
}
if percentile >= 1.0 {
fail!("percentile ({percentile}) must be < 1.0");
}
let inverse = InverseCumulativeNormal::new(self.mean()?, self.standard_deviation()?)?;
inverse.value(percentile)
}
/// Gaussian-assumption top percentile, `gaussian_percentile(1 - p)`.
///
/// # Errors
///
/// Returns an error if `percentile` lies outside `(0, 1)` or the
/// underlying moments are unavailable.
fn gaussian_top_percentile(&self, percentile: Real) -> QlResult<Real> {
self.gaussian_percentile(1.0 - percentile)
}
/// Gaussian-assumption potential upside (the gain-side counterpart of
/// VAR) at a percentile in `[0.9, 1)`; floored at 0.
///
/// # Errors
///
/// Returns an error if `percentile` lies outside `[0.9, 1)` or the
/// underlying moments are unavailable.
fn gaussian_potential_upside(&self, percentile: Real) -> QlResult<Real> {
require_var_percentile(percentile)?;
Ok(self.gaussian_percentile(percentile)?.max(0.0))
}
/// Gaussian-assumption value-at-risk at a percentile in `[0.9, 1)`,
/// reported as a positive loss (capped at 0 and negated).
///
/// # Errors
///
/// Returns an error if `percentile` lies outside `[0.9, 1)` or the
/// underlying moments are unavailable.
fn gaussian_value_at_risk(&self, percentile: Real) -> QlResult<Real> {
require_var_percentile(percentile)?;
Ok(-self.gaussian_percentile(1.0 - percentile)?.min(0.0))
}
/// Gaussian-assumption expected shortfall (conditional VAR) at a
/// percentile in `[0.9, 1)`: the expected loss given that the loss
/// exceeds the VAR threshold, reported as a positive loss.
///
/// # Errors
///
/// Returns an error if `percentile` lies outside `[0.9, 1)` or the
/// underlying moments are unavailable.
fn gaussian_expected_shortfall(&self, percentile: Real) -> QlResult<Real> {
require_var_percentile(percentile)?;
let mean = self.mean()?;
let std_dev = self.standard_deviation()?;
let var = InverseCumulativeNormal::new(mean, std_dev)?.value(1.0 - percentile)?;
let gaussian = NormalDistribution::new(mean, std_dev)?;
let result = mean - std_dev * std_dev * gaussian.value(var) / (1.0 - percentile);
Ok(-result.min(0.0))
}
/// Gaussian-assumption shortfall: the probability that a sample falls
/// below `target`.
///
/// # Errors
///
/// Returns an error if the underlying moments are unavailable.
fn gaussian_shortfall(&self, target: Real) -> QlResult<Real> {
let cumulative =
CumulativeNormalDistribution::new(self.mean()?, self.standard_deviation()?)?;
Ok(cumulative.value(target))
}
/// Gaussian-assumption average shortfall, `E[t - x | x < t]`.
///
/// # Errors
///
/// Returns an error if the underlying moments are unavailable.
fn gaussian_average_shortfall(&self, target: Real) -> QlResult<Real> {
let mean = self.mean()?;
let std_dev = self.standard_deviation()?;
let cumulative = CumulativeNormalDistribution::new(mean, std_dev)?;
let gaussian = NormalDistribution::new(mean, std_dev)?;
Ok((target - mean) + std_dev * std_dev * gaussian.value(target) / cumulative.value(target))
}
/// Gaussian-assumption regret: the variance of observations below
/// `target`, `E[(x - t)² | x < t]`.
///
/// # Errors
///
/// Returns an error if the underlying moments are unavailable.
fn gaussian_regret(&self, target: Real) -> QlResult<Real> {
let mean = self.mean()?;
let std_dev = self.standard_deviation()?;
let variance = std_dev * std_dev;
let cumulative = CumulativeNormalDistribution::new(mean, std_dev)?;
let gaussian = NormalDistribution::new(mean, std_dev)?;
let first_term = variance + mean * mean - 2.0 * target * mean + target * target;
let alfa = cumulative.value(target);
let second_term = mean - target;
let beta = variance * gaussian.value(target);
Ok((alfa * first_term - beta * second_term) / alfa)
}
/// Gaussian-assumption downside variance, `gaussian_regret(0)`.
///
/// # Errors
///
/// Returns an error if the underlying moments are unavailable.
fn gaussian_downside_variance(&self) -> QlResult<Real> {
self.gaussian_regret(0.0)
}
/// Gaussian-assumption downside deviation, the square root of the
/// downside variance.
///
/// # Errors
///
/// Returns an error if the underlying moments are unavailable.
fn gaussian_downside_deviation(&self) -> QlResult<Real> {
Ok(self.gaussian_downside_variance()?.sqrt())
}
}
impl<T: MeanStdDev + ?Sized> GaussianStatistics for T {}
/// Validates the percentile range `[0.9, 1)` shared by the VAR-style
/// measures.
fn require_var_percentile(percentile: Real) -> QlResult<()> {
if !(0.9..1.0).contains(&percentile) {
fail!("percentile ({percentile}) out of range [0.9, 1.0)");
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::math::comparison::close;
use crate::math::statistics::testutil::{AVERAGES, SIGMAS, check, sobol_normal_samples};
use crate::math::statistics::{GeneralStatistics, IncrementalStatistics, Statistics};
#[test]
fn matches_quantlib_riskstats_oracle() {
for average in AVERAGES {
for sigma in SIGMAS {
let normal = NormalDistribution::new(average, sigma).unwrap();
let cumulative = CumulativeNormalDistribution::new(average, sigma).unwrap();
let data = sobol_normal_samples(average, sigma);
let mut igs = IncrementalStatistics::new();
let mut s = GeneralStatistics::new();
igs.add_sequence(data.iter().copied()).unwrap();
s.add_sequence(data.iter().copied()).unwrap();
for stats in [&igs as &dyn GaussianStatistics, &s] {
let expected = average;
let tolerance = if expected == 0.0 {
1e-3
} else {
(expected * 1e-3).abs()
};
check(
"gaussian percentile",
average,
sigma,
stats.gaussian_percentile(0.5).unwrap(),
expected,
tolerance,
);
let upper_tail = average + 2.0 * sigma;
let lower_tail = average - 2.0 * sigma;
let two_sigma = cumulative.value(upper_tail);
let expected = upper_tail.max(0.0);
let tolerance = if expected == 0.0 {
1e-3
} else {
(expected * 1e-3).abs()
};
check(
"gaussian potential upside",
average,
sigma,
stats.gaussian_potential_upside(two_sigma).unwrap(),
expected,
tolerance,
);
let expected = -lower_tail.min(0.0);
let tolerance = if expected == 0.0 {
1e-3
} else {
(expected * 1e-3).abs()
};
check(
"gaussian value-at-risk",
average,
sigma,
stats.gaussian_value_at_risk(two_sigma).unwrap(),
expected,
tolerance,
);
if average > 0.0 && sigma < average {
continue;
}
let expected = -(average
- sigma * sigma * normal.value(lower_tail) / (1.0 - two_sigma))
.min(0.0);
let tolerance = if expected == 0.0 {
1e-4
} else {
(expected * 1e-2).abs()
};
check(
"gaussian expected shortfall",
average,
sigma,
stats.gaussian_expected_shortfall(two_sigma).unwrap(),
expected,
tolerance,
);
check(
"gaussian shortfall",
average,
sigma,
stats.gaussian_shortfall(average).unwrap(),
0.5,
0.5e-3,
);
let expected = sigma / (2.0 * std::f64::consts::PI).sqrt() * 2.0;
check(
"gaussian average shortfall",
average,
sigma,
stats.gaussian_average_shortfall(average).unwrap(),
expected,
expected * 1e-3,
);
let expected = sigma * sigma;
check(
"gaussian regret",
average,
sigma,
stats.gaussian_regret(average).unwrap(),
expected,
expected * 1e-1,
);
}
}
}
}
#[test]
fn stats_holder_reproduces_accumulator_measures() {
let data = sobol_normal_samples(-1.0, 2.0);
let mut s = GeneralStatistics::new();
s.add_sequence(data).unwrap();
let holder = StatsHolder::new(s.mean().unwrap(), s.standard_deviation().unwrap());
let two_sigma = CumulativeNormalDistribution::new(-1.0, 2.0)
.unwrap()
.value(-1.0 + 2.0 * 2.0);
assert!(close(
holder.gaussian_potential_upside(two_sigma).unwrap(),
s.gaussian_potential_upside(two_sigma).unwrap(),
));
assert!(close(
holder.gaussian_downside_variance().unwrap(),
s.gaussian_downside_variance().unwrap(),
));
}
#[test]
fn rejects_out_of_range_percentiles() {
let holder = StatsHolder::new(0.0, 1.0);
assert!(holder.gaussian_percentile(0.0).is_err());
assert!(holder.gaussian_percentile(1.0).is_err());
assert!(holder.gaussian_potential_upside(0.5).is_err());
assert!(holder.gaussian_value_at_risk(1.0).is_err());
assert!(holder.gaussian_expected_shortfall(0.89).is_err());
}
}