QuantLib: a free/open-source library for quantitative finance
Reference manual - version 1.40
Loading...
Searching...
No Matches
GeneralStatistics Class Reference

Statistics tool. More...

#include <ql/math/statistics/generalstatistics.hpp>

Inheritance diagram for GeneralStatistics:

Public Types

typedef Real value_type

Public Member Functions

Inspectors
Size samples () const
 number of samples collected
const std::vector< std::pair< Real, Real > > & data () const
 collected data
Real weightSum () const
 sum of data weights
Real mean () const
Real variance () const
Real standardDeviation () const
Real errorEstimate () const
Real skewness () const
Real kurtosis () const
Real min () const
Real max () const
template<class Func, class Predicate>
std::pair< Real, SizeexpectationValue (const Func &f, const Predicate &inRange) const
template<class Func>
std::pair< Real, SizeexpectationValue (const Func &f) const
Real percentile (Real y) const
Real topPercentile (Real y) const

Modifiers

void add (Real value, Real weight=1.0)
 adds a datum to the set, possibly with a weight
template<class DataIterator>
void addSequence (DataIterator begin, DataIterator end)
 adds a sequence of data to the set, with default weight
template<class DataIterator, class WeightIterator>
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
void reserve (Size n) const
 informs the internal storage of a planned increase in size
void sort () const
 sort the data set in increasing order

Detailed Description

Statistics tool.

This class accumulates a set of data and returns their statistics (e.g: mean, variance, skewness, kurtosis, error estimation, percentile, etc.) based on the empirical distribution (no gaussian assumption)

It doesn't suffer the numerical instability problem of IncrementalStatistics. The downside is that it stores all samples, thus increasing the memory requirements.

Member Function Documentation

◆ mean()

Real mean ( ) const

returns the mean, defined as

\[ \langle x \rangle = \frac{\sum w_i x_i}{\sum w_i}. \]

Examples
MarketModels.cpp.

◆ variance()

Real variance ( ) const

returns the variance, defined as

\[ \sigma^2 = \frac{N}{N-1} \left\langle \left( x-\langle x \rangle \right)^2 \right\rangle. \]

◆ standardDeviation()

Real standardDeviation ( ) const

returns the standard deviation \( \sigma \), defined as the square root of the variance.

◆ errorEstimate()

Real errorEstimate ( ) const

returns the error estimate on the mean value, defined as \( \epsilon = \sigma/\sqrt{N}. \)

Examples
MarketModels.cpp.

◆ skewness()

Real skewness ( ) const

returns the skewness, defined as

\[ \frac{N^2}{(N-1)(N-2)} \frac{\left\langle \left( x-\langle x \rangle \right)^3 \right\rangle}{\sigma^3}. \]

The above evaluates to 0 for a Gaussian distribution.

◆ kurtosis()

Real kurtosis ( ) const

returns the excess kurtosis, defined as

\[ \frac{N^2(N+1)}{(N-1)(N-2)(N-3)} \frac{\left\langle \left(x-\langle x \rangle \right)^4 \right\rangle}{\sigma^4} - \frac{3(N-1)^2}{(N-2)(N-3)}. \]

The above evaluates to 0 for a Gaussian distribution.

◆ min()

Real min ( ) const

returns the minimum sample value

◆ max()

Real max ( ) const

returns the maximum sample value

◆ expectationValue() [1/2]

template<class Func, class Predicate>
std::pair< Real, Size > expectationValue ( const Func & f,
const Predicate & inRange ) const

Expectation value of a function \( f \) on a given range \( \mathcal{R} \), i.e.,

\[ \mathrm{E}\left[f \;|\; \mathcal{R}\right] = \frac{\sum_{x_i \in \mathcal{R}} f(x_i) w_i}{ \sum_{x_i \in \mathcal{R}} w_i}. \]

The range is passed as a boolean function returning true if the argument belongs to the range or false otherwise.

The function returns a pair made of the result and the number of observations in the given range.

◆ expectationValue() [2/2]

template<class Func>
std::pair< Real, Size > expectationValue ( const Func & f) const

Expectation value of a function \( f \) over the whole set of samples; equivalent to passing the other overload a range function always returning true.

◆ percentile()

Real percentile ( Real y) const

\( y \)-th percentile, defined as the value \( \bar{x} \) such that

\[ y = \frac{\sum_{x_i < \bar{x}} w_i}{ \sum_i w_i} \]

Precondition
\( y \) must be in the range \( (0-1]. \)

◆ topPercentile()

Real topPercentile ( Real y) const

\( y \)-th top percentile, defined as the value \( \bar{x} \) such that

\[ y = \frac{\sum_{x_i > \bar{x}} w_i}{ \sum_i w_i} \]

Precondition
\( y \) must be in the range \( (0-1]. \)

◆ add()

void add ( Real value,
Real weight = 1.0 )

adds a datum to the set, possibly with a weight

Precondition
weights must be positive or null