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 Copyright (C) 2015 Peter Caspers
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22#include <ql/math/statistics/incrementalstatistics.hpp>
23#include <iomanip>
24
25namespace QuantLib {
26
27 IncrementalStatistics::IncrementalStatistics() {
28 reset();
29 }
30
31 Size IncrementalStatistics::samples() const {
32 return boost::accumulators::extract_result<
33 boost::accumulators::tag::count>(acc: acc_);
34 }
35
36 Real IncrementalStatistics::weightSum() const {
37 return boost::accumulators::extract_result<
38 boost::accumulators::tag::sum_of_weights>(acc: acc_);
39 }
40
41 Real IncrementalStatistics::mean() const {
42 QL_REQUIRE(weightSum() > 0.0, "sampleWeight_= 0, unsufficient");
43 return boost::accumulators::extract_result<
44 boost::accumulators::tag::weighted_mean>(acc: acc_);
45 }
46
47 Real IncrementalStatistics::variance() const {
48 QL_REQUIRE(weightSum() > 0.0, "sampleWeight_= 0, unsufficient");
49 QL_REQUIRE(samples() > 1, "sample number <= 1, unsufficient");
50 Real n = static_cast<Real>(samples());
51 return n / (n - 1.0) *
52 boost::accumulators::extract_result<
53 boost::accumulators::tag::weighted_variance>(acc: acc_);
54 }
55
56 Real IncrementalStatistics::standardDeviation() const {
57 return std::sqrt(x: variance());
58 }
59
60 Real IncrementalStatistics::errorEstimate() const {
61 return std::sqrt(x: variance() / (samples()));
62 }
63
64 Real IncrementalStatistics::skewness() const {
65 QL_REQUIRE(samples() > 2, "sample number <= 2, unsufficient");
66 Real n = static_cast<Real>(samples());
67 Real r1 = n / (n - 2.0);
68 Real r2 = (n - 1.0) / (n - 2.0);
69 return std::sqrt(x: r1 * r2) *
70 boost::accumulators::extract_result<
71 boost::accumulators::tag::weighted_skewness>(acc: acc_);
72 }
73
74 Real IncrementalStatistics::kurtosis() const {
75 QL_REQUIRE(samples() > 3,
76 "sample number <= 3, unsufficient");
77 boost::accumulators::extract_result<
78 boost::accumulators::tag::weighted_kurtosis>(acc: acc_);
79 Real n = static_cast<Real>(samples());
80 Real r1 = (n - 1.0) / (n - 2.0);
81 Real r2 = (n + 1.0) / (n - 3.0);
82 Real r3 = (n - 1.0) / (n - 3.0);
83 return ((3.0 + boost::accumulators::extract_result<
84 boost::accumulators::tag::weighted_kurtosis>(acc: acc_)) *
85 r2 -
86 3.0 * r3) *
87 r1;
88 }
89
90 Real IncrementalStatistics::min() const {
91 QL_REQUIRE(samples() > 0, "empty sample set");
92 return boost::accumulators::extract_result<
93 boost::accumulators::tag::min>(acc: acc_);
94 }
95
96 Real IncrementalStatistics::max() const {
97 QL_REQUIRE(samples() > 0, "empty sample set");
98 return boost::accumulators::extract_result<
99 boost::accumulators::tag::max>(acc: acc_);
100 }
101
102 Size IncrementalStatistics::downsideSamples() const {
103 return boost::accumulators::extract_result<
104 boost::accumulators::tag::count>(acc: downsideAcc_);
105 }
106
107 Real IncrementalStatistics::downsideWeightSum() const {
108 return boost::accumulators::extract_result<
109 boost::accumulators::tag::sum_of_weights>(acc: downsideAcc_);
110 }
111
112 Real IncrementalStatistics::downsideVariance() const {
113 QL_REQUIRE(downsideWeightSum() > 0.0, "sampleWeight_= 0, unsufficient");
114 QL_REQUIRE(downsideSamples() > 1, "sample number <= 1, unsufficient");
115 Real n = static_cast<Real>(downsideSamples());
116 Real r1 = n / (n - 1.0);
117 return r1 *
118 boost::accumulators::extract_result<
119 boost::accumulators::tag::moment<2> >(acc: downsideAcc_);
120 }
121
122 Real IncrementalStatistics::downsideDeviation() const {
123 return std::sqrt(x: downsideVariance());
124 }
125
126 void IncrementalStatistics::add(Real value, Real valueWeight) {
127 QL_REQUIRE(valueWeight >= 0.0, "negative weight (" << valueWeight
128 << ") not allowed");
129 acc_(value, boost::accumulators::weight = valueWeight);
130 if(value < 0.0)
131 downsideAcc_(value, boost::accumulators::weight = valueWeight);
132 }
133
134 void IncrementalStatistics::reset() {
135 acc_ = accumulator_set();
136 downsideAcc_ = downside_accumulator_set();
137 }
138
139}
140

source code of quantlib/ql/math/statistics/incrementalstatistics.cpp