QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
incrementalstatistics.cpp
Go to the documentation of this file.
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
23#include <iomanip>
24
25namespace QuantLib {
26
28 reset();
29 }
30
32 return boost::accumulators::extract_result<
33 boost::accumulators::tag::count>(acc_);
34 }
35
37 return boost::accumulators::extract_result<
38 boost::accumulators::tag::sum_of_weights>(acc_);
39 }
40
42 QL_REQUIRE(weightSum() > 0.0, "sampleWeight_= 0, unsufficient");
43 return boost::accumulators::extract_result<
44 boost::accumulators::tag::weighted_mean>(acc_);
45 }
46
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_);
54 }
55
57 return std::sqrt(variance());
58 }
59
61 return std::sqrt(variance() / (samples()));
62 }
63
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(r1 * r2) *
70 boost::accumulators::extract_result<
71 boost::accumulators::tag::weighted_skewness>(acc_);
72 }
73
75 QL_REQUIRE(samples() > 3,
76 "sample number <= 3, unsufficient");
77 boost::accumulators::extract_result<
78 boost::accumulators::tag::weighted_kurtosis>(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_)) *
85 r2 -
86 3.0 * r3) *
87 r1;
88 }
89
91 QL_REQUIRE(samples() > 0, "empty sample set");
92 return boost::accumulators::extract_result<
93 boost::accumulators::tag::min>(acc_);
94 }
95
97 QL_REQUIRE(samples() > 0, "empty sample set");
98 return boost::accumulators::extract_result<
99 boost::accumulators::tag::max>(acc_);
100 }
101
103 return boost::accumulators::extract_result<
104 boost::accumulators::tag::count>(downsideAcc_);
105 }
106
108 return boost::accumulators::extract_result<
109 boost::accumulators::tag::sum_of_weights>(downsideAcc_);
110 }
111
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> >(downsideAcc_);
120 }
121
123 return std::sqrt(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
137 }
138
139}
Size samples() const
number of samples collected
Size downsideSamples() const
number of negative samples collected
Real weightSum() const
sum of data weights
void add(Real value, Real weight=1.0)
adds a datum to the set, possibly with a weight
boost::accumulators::accumulator_set< Real, boost::accumulators::stats< boost::accumulators::tag::count, boost::accumulators::tag::weighted_moment< 2 >, boost::accumulators::tag::sum_of_weights >, Real > downside_accumulator_set
boost::accumulators::accumulator_set< Real, boost::accumulators::stats< boost::accumulators::tag::count, boost::accumulators::tag::min, boost::accumulators::tag::max, boost::accumulators::tag::weighted_mean, boost::accumulators::tag::weighted_variance, boost::accumulators::tag::weighted_skewness, boost::accumulators::tag::weighted_kurtosis, boost::accumulators::tag::sum_of_weights >, Real > accumulator_set
Real downsideWeightSum() const
sum of data weights for negative samples
void reset()
resets the data to a null set
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
statistics tool based on incremental accumulation in the meantime, this is just a wrapper to the boos...
Definition: any.hpp:35