QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
generalstatistics.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) 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_general_statistics_hpp
26#define quantlib_general_statistics_hpp
27
28#include <ql/utilities/null.hpp>
29#include <ql/errors.hpp>
30#include <vector>
31#include <algorithm>
32#include <utility>
33
34namespace QuantLib {
35
37
47 public:
51
52
53 Size samples() const;
54
56 const std::vector<std::pair<Real,Real> >& data() const;
57
59 Real weightSum() const;
60
64 Real mean() const;
65
70 Real variance() const;
71
75 Real standardDeviation() const;
76
80 Real errorEstimate() const;
81
87 Real skewness() const;
88
95 Real kurtosis() const;
96
98 Real min() const;
99
101 Real max() const;
102
115 template <class Func, class Predicate>
116 std::pair<Real,Size> expectationValue(const Func& f,
117 const Predicate& inRange) const {
118 Real num = 0.0, den = 0.0;
119 Size N = 0;
120 std::vector<std::pair<Real,Real> >::const_iterator i;
121 for (i=samples_.begin(); i!=samples_.end(); ++i) {
122 Real x = i->first, w = i->second;
123 if (inRange(x)) {
124 num += f(x)*w;
125 den += w;
126 N += 1;
127 }
128 }
129 if (N == 0)
130 return std::make_pair<Real,Size>(Null<Real>(),0);
131 else
132 return std::make_pair(num/den,N);
133 }
134
139 template <class Func>
140 std::pair<Real,Size> expectationValue(const Func& f) const {
141 return expectationValue(f, [](Real x) { return true; });
142 }
143
151 Real percentile(Real y) const;
152
160 Real topPercentile(Real y) const;
162
164
165
166 void add(Real value, Real weight = 1.0);
168 template <class DataIterator>
169 void addSequence(DataIterator begin, DataIterator end) {
170 for (;begin!=end;++begin)
171 add(*begin);
172 }
174 template <class DataIterator, class WeightIterator>
175 void addSequence(DataIterator begin, DataIterator end,
176 WeightIterator wbegin) {
177 for (;begin!=end;++begin,++wbegin)
178 add(*begin, *wbegin);
179 }
180
182 void reset();
183
185 void reserve(Size n) const;
186
188 void sort() const;
190 private:
191 mutable std::vector<std::pair<Real,Real> > samples_;
192 mutable bool sorted_;
193 };
194
195
196 // inline definitions
197
199 reset();
200 }
201
203 return samples_.size();
204 }
205
206 inline const std::vector<std::pair<Real,Real> >&
208 return samples_;
209 }
210
212 return std::sqrt(variance());
213 }
214
216 return std::sqrt(variance()/samples());
217 }
218
220 QL_REQUIRE(samples() > 0, "empty sample set");
221 return std::min_element(samples_.begin(),
222 samples_.end())->first;
223 }
224
226 QL_REQUIRE(samples() > 0, "empty sample set");
227 return std::max_element(samples_.begin(),
228 samples_.end())->first;
229 }
230
232 inline void GeneralStatistics::add(Real value, Real weight) {
233 QL_REQUIRE(weight>=0.0, "negative weight not allowed");
234 samples_.emplace_back(value, weight);
235 sorted_ = false;
236 }
237
239 samples_ = std::vector<std::pair<Real,Real> >();
240 sorted_ = true;
241 }
242
243 inline void GeneralStatistics::reserve(Size n) const {
244 samples_.reserve(n);
245 }
246
247 inline void GeneralStatistics::sort() const {
248 if (!sorted_) {
249 std::sort(samples_.begin(), samples_.end());
250 sorted_ = true;
251 }
252 }
253
254}
255
256
257#endif
Size samples() const
number of samples collected
void reserve(Size n) const
informs the internal storage of a planned increase in size
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
std::vector< std::pair< Real, Real > > samples_
void addSequence(DataIterator begin, DataIterator end)
adds a sequence of data to the set, with default weight
const std::vector< std::pair< Real, Real > > & data() const
collected data
std::pair< Real, Size > expectationValue(const Func &f, const Predicate &inRange) const
void sort() const
sort the data set in increasing order
std::pair< Real, Size > expectationValue(const Func &f) const
void addSequence(DataIterator begin, DataIterator end, WeightIterator wbegin)
adds a sequence of data to the set, each with its weight
void reset()
resets the data to a null set
template class providing a null value for a given type.
Definition: null.hpp:76
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35