libitofin 0.52.1

A ground-up Rust port of QuantLib: quantitative-finance primitives for pricing, risk, and numerical methods.
Documentation
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Statistics tool based on incremental accumulation.
//!
//! Port of `ql/math/statistics/incrementalstatistics.{hpp,cpp}`, which wraps
//! the boost accumulator library. Instead of boost's raw-moment forms we
//! update the weighted central moments online (Pébay's single-pass update
//! formulas): mathematically equal, but stabler when the mean is large
//! relative to the spread. A second accumulator tracks the negative samples
//! for the downside measures, mirroring QuantLib's `downsideAcc_`.

use crate::errors::QlResult;
use crate::types::{Real, Size};
use crate::{fail, require};

use super::{MeanStdDev, Statistics, check_sample};

/// Single-pass statistics accumulator; it keeps running moments instead of
/// the samples themselves.
#[derive(Clone, Debug, Default)]
pub struct IncrementalStatistics {
    count: Size,
    weight_sum: Real,
    mean: Real,
    m2: Real,
    m3: Real,
    m4: Real,
    min: Real,
    max: Real,
    downside_count: Size,
    downside_weight_sum: Real,
    downside_second_moment_sum: Real,
}

impl IncrementalStatistics {
    /// An empty accumulator.
    pub fn new() -> Self {
        IncrementalStatistics::default()
    }

    /// Number of negative samples collected.
    pub fn downside_samples(&self) -> Size {
        self.downside_count
    }

    /// Sum of the data weights for the negative samples.
    pub fn downside_weight_sum(&self) -> Real {
        self.downside_weight_sum
    }

    /// Whether every stored floating-point accumulator is finite.
    pub fn has_finite_state(&self) -> bool {
        [
            self.weight_sum,
            self.mean,
            self.m2,
            self.m3,
            self.m4,
            self.min,
            self.max,
            self.downside_weight_sum,
            self.downside_second_moment_sum,
        ]
        .iter()
        .all(|value| value.is_finite())
    }

    /// The downside variance, `N/(N-1) Σ wᵢ xᵢ² / Σ wᵢ` over the negative
    /// samples.
    ///
    /// # Errors
    ///
    /// Returns an error on fewer than two negative samples or a zero
    /// downside weight sum.
    pub fn downside_variance(&self) -> QlResult<Real> {
        if self.downside_weight_sum <= 0.0 {
            fail!("sample weight is 0: insufficient");
        }
        require!(self.downside_count > 1, "sample number <= 1: insufficient");
        let n = self.downside_count as Real;
        Ok(n / (n - 1.0) * self.downside_second_moment_sum / self.downside_weight_sum)
    }

    /// The downside deviation, the square root of the downside variance.
    ///
    /// # Errors
    ///
    /// Returns an error on fewer than two negative samples.
    pub fn downside_deviation(&self) -> QlResult<Real> {
        Ok(self.downside_variance()?.sqrt())
    }

    /// The biased (population) central moment of order `k`, `Mₖ / Σ wᵢ`.
    fn central_moment(&self, m: Real) -> Real {
        m / self.weight_sum
    }
}

impl MeanStdDev for IncrementalStatistics {
    fn mean(&self) -> QlResult<Real> {
        if self.weight_sum <= 0.0 {
            fail!("sample weight is 0: insufficient");
        }
        Ok(self.mean)
    }

    fn standard_deviation(&self) -> QlResult<Real> {
        Ok(self.variance()?.sqrt())
    }
}

impl Statistics for IncrementalStatistics {
    fn samples(&self) -> Size {
        self.count
    }

    fn weight_sum(&self) -> Real {
        self.weight_sum
    }

    fn variance(&self) -> QlResult<Real> {
        if self.weight_sum <= 0.0 {
            fail!("sample weight is 0: insufficient");
        }
        require!(self.count > 1, "sample number <= 1: insufficient");
        let n = self.count as Real;
        Ok(n / (n - 1.0) * self.central_moment(self.m2))
    }

    fn error_estimate(&self) -> QlResult<Real> {
        Ok((self.variance()? / self.count as Real).sqrt())
    }

    fn skewness(&self) -> QlResult<Real> {
        require!(self.count > 2, "sample number <= 2: insufficient");
        let n = self.count as Real;
        let g1 = self.central_moment(self.m3) / self.central_moment(self.m2).powf(1.5);
        let r1 = n / (n - 2.0);
        let r2 = (n - 1.0) / (n - 2.0);
        Ok((r1 * r2).sqrt() * g1)
    }

    fn kurtosis(&self) -> QlResult<Real> {
        require!(self.count > 3, "sample number <= 3: insufficient");
        let n = self.count as Real;
        let m2 = self.central_moment(self.m2);
        let g2 = self.central_moment(self.m4) / (m2 * m2) - 3.0;
        let r1 = (n - 1.0) / (n - 2.0);
        let r2 = (n + 1.0) / (n - 3.0);
        let r3 = (n - 1.0) / (n - 3.0);
        Ok(((3.0 + g2) * r2 - 3.0 * r3) * r1)
    }

    fn min(&self) -> QlResult<Real> {
        require!(self.count > 0, "empty sample set");
        Ok(self.min)
    }

    fn max(&self) -> QlResult<Real> {
        require!(self.count > 0, "empty sample set");
        Ok(self.max)
    }

    fn add_weighted(&mut self, value: Real, weight: Real) -> QlResult<()> {
        check_sample(value, weight)?;
        self.min = if self.count == 0 {
            value
        } else {
            self.min.min(value)
        };
        self.max = if self.count == 0 {
            value
        } else {
            self.max.max(value)
        };
        self.count += 1;
        if weight > 0.0 {
            let old_weight_sum = self.weight_sum;
            self.weight_sum += weight;
            let delta = value - self.mean;
            let shift = delta * weight / self.weight_sum;
            self.m4 += weight
                * delta.powi(4)
                * old_weight_sum
                * (old_weight_sum * old_weight_sum - old_weight_sum * weight + weight * weight)
                / self.weight_sum.powi(3)
                + 6.0 * shift * shift * self.m2
                - 4.0 * shift * self.m3;
            self.m3 += weight * delta.powi(3) * old_weight_sum * (old_weight_sum - weight)
                / (self.weight_sum * self.weight_sum)
                - 3.0 * shift * self.m2;
            self.m2 += weight * delta * (delta - shift);
            self.mean += shift;
        }
        if value < 0.0 {
            self.downside_count += 1;
            self.downside_weight_sum += weight;
            self.downside_second_moment_sum += weight * value * value;
        }
        Ok(())
    }

    fn reset(&mut self) {
        *self = IncrementalStatistics::default();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::math::statistics::GeneralStatistics;
    use crate::math::statistics::testutil::{
        AVERAGES, N_SAMPLES, SIGMAS, check, sobol_normal_samples,
    };

    #[test]
    fn matches_quantlib_riskstats_oracle() {
        for average in AVERAGES {
            for sigma in SIGMAS {
                let data = sobol_normal_samples(average, sigma);
                let data_min = data.iter().copied().fold(Real::INFINITY, Real::min);
                let data_max = data.iter().copied().fold(Real::NEG_INFINITY, Real::max);

                let mut igs = IncrementalStatistics::new();
                igs.add_sequence_weighted(data.iter().map(|&x| (x, 1.0)))
                    .unwrap();

                assert_eq!(igs.samples(), N_SAMPLES);
                check(
                    "weight sum",
                    average,
                    sigma,
                    igs.weight_sum(),
                    N_SAMPLES as Real,
                    1e-10,
                );
                check(
                    "minimum",
                    average,
                    sigma,
                    igs.min().unwrap(),
                    data_min,
                    1e-12,
                );
                check(
                    "maximum",
                    average,
                    sigma,
                    igs.max().unwrap(),
                    data_max,
                    1e-12,
                );

                let tolerance = if average == 0.0 {
                    1e-13
                } else {
                    average.abs() * 1e-13
                };
                check(
                    "mean",
                    average,
                    sigma,
                    igs.mean().unwrap(),
                    average,
                    tolerance,
                );

                let expected = sigma * sigma;
                check(
                    "variance",
                    average,
                    sigma,
                    igs.variance().unwrap(),
                    expected,
                    expected * 1e-1,
                );
                check(
                    "standard deviation",
                    average,
                    sigma,
                    igs.standard_deviation().unwrap(),
                    sigma,
                    sigma * 1e-1,
                );
                check(
                    "skewness",
                    average,
                    sigma,
                    igs.skewness().unwrap(),
                    0.0,
                    1e-4,
                );
                check(
                    "kurtosis",
                    average,
                    sigma,
                    igs.kurtosis().unwrap(),
                    0.0,
                    1e-1,
                );

                if average == 0.0 {
                    let expected = sigma * sigma;
                    check(
                        "downside variance",
                        average,
                        sigma,
                        igs.downside_variance().unwrap(),
                        expected,
                        expected * 1e-3,
                    );
                }
            }
        }
    }

    #[test]
    fn agrees_with_general_statistics_on_weighted_data() {
        let values = [2.0, -3.5, 1.25, 8.0, -0.5, 4.75, -6.0, 3.0];
        let weights = [1.0, 2.0, 0.5, 1.5, 3.0, 1.0, 2.5, 0.75];

        let mut incremental = IncrementalStatistics::new();
        let mut general = GeneralStatistics::new();
        for (&value, &weight) in values.iter().zip(&weights) {
            incremental.add_weighted(value, weight).unwrap();
            general.add_weighted(value, weight).unwrap();
        }

        assert_eq!(incremental.samples(), general.samples());
        for (label, a, b) in [
            ("weight sum", incremental.weight_sum(), general.weight_sum()),
            ("mean", incremental.mean().unwrap(), general.mean().unwrap()),
            (
                "variance",
                incremental.variance().unwrap(),
                general.variance().unwrap(),
            ),
            (
                "skewness",
                incremental.skewness().unwrap(),
                general.skewness().unwrap(),
            ),
            (
                "kurtosis",
                incremental.kurtosis().unwrap(),
                general.kurtosis().unwrap(),
            ),
            (
                "minimum",
                incremental.min().unwrap(),
                general.min().unwrap(),
            ),
            (
                "maximum",
                incremental.max().unwrap(),
                general.max().unwrap(),
            ),
        ] {
            assert!(
                (a - b).abs() <= 1e-12,
                "{label}: incremental {a}, general {b}"
            );
        }

        let downside: Vec<(Real, Real)> = values
            .iter()
            .zip(&weights)
            .filter(|&(&value, _)| value < 0.0)
            .map(|(&value, &weight)| (value, weight))
            .collect();
        let n = downside.len() as Real;
        let weight_sum: Real = downside.iter().map(|&(_, w)| w).sum();
        let second: Real = downside.iter().map(|&(x, w)| w * x * x).sum();
        let expected = n / (n - 1.0) * second / weight_sum;
        assert!((incremental.downside_variance().unwrap() - expected).abs() <= 1e-12);
        assert_eq!(incremental.downside_samples(), downside.len());
    }

    #[test]
    fn zero_weight_samples_count_but_carry_no_mass() {
        let mut s = IncrementalStatistics::new();
        s.add_weighted(-5.0, 0.0).unwrap();
        assert_eq!(s.samples(), 1);
        assert_eq!(s.weight_sum(), 0.0);
        assert_eq!(s.min().unwrap(), -5.0);
        assert!(s.mean().is_err());

        s.add(1.0).unwrap();
        s.add(3.0).unwrap();
        assert_eq!(s.mean().unwrap(), 2.0);
        assert_eq!(s.min().unwrap(), -5.0);
    }

    #[test]
    fn finite_state_detects_hidden_moment_overflow() {
        let mut s = IncrementalStatistics::new();
        assert!(s.has_finite_state());
        s.add(1e100).unwrap();
        assert!(!s.has_finite_state());
        assert!(s.mean().unwrap().is_finite());
    }

    #[test]
    fn rejects_insufficient_samples_and_bad_arguments() {
        let mut s = IncrementalStatistics::new();
        assert!(s.mean().is_err());
        assert!(s.min().is_err());
        assert!(s.downside_variance().is_err());
        assert!(s.add_weighted(1.0, -1.0).is_err());
        assert!(s.add_weighted(Real::NAN, 1.0).is_err());
        assert!(s.add_weighted(1.0, Real::NAN).is_err());

        s.add(1.0).unwrap();
        assert!(s.variance().is_err());
        s.add(2.0).unwrap();
        assert!(s.skewness().is_err());
        s.add(3.0).unwrap();
        assert!(s.kurtosis().is_err());

        s.reset();
        assert_eq!(s.samples(), 0);
        assert_eq!(s.weight_sum(), 0.0);
    }
}