Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
bucketeddistribution.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19
20/*! \file qle/math/bucketeddistribution.hpp
21 \brief Deals with a bucketed distribution
22*/
23
24#pragma once
25
26#include <vector>
27
28#include <ql/types.hpp>
29
31
32namespace QuantExt {
33
34//! Represents a bucketed probability distibution
36public:
37 //! Default constructor
39 //! Build a default initial distribution from \p min and \p max and \p numberBuckets
40 BucketedDistribution(QuantLib::Real min, QuantLib::Real max, QuantLib::Size numberBuckets);
41 /*! Build a default initial distribution from \p min and \p max and \p numberBuckets and
42 * set all probabilities to \p initialValue
43 */
44 BucketedDistribution(QuantLib::Real min, QuantLib::Real max, QuantLib::Size numberBuckets,
45 QuantLib::Real initialValue);
46 //! Explicitly specify the initial distribution
47 BucketedDistribution(const std::vector<QuantLib::Real>& buckets,
48 const std::vector<QuantLib::Real>& initialProbabilities,
49 const std::vector<QuantLib::Real>& initialPoints);
50 //! Copy constructor
52 //! Update the bucketed distribution by adding a discrete distribution
53 void add(const DiscreteDistribution& distribution);
54 //! Return the buckets of the distribution
55 const std::vector<QuantLib::Real>& buckets() const { return buckets_; }
56 //! Get the probabilities
57 const std::vector<QuantLib::Real>& probabilities() const { return probabilities_; }
58 //! Set the probabilities
59 std::vector<QuantLib::Real>& probabilities() { return probabilities_; }
60 //! Return the points of the distribution
61 const std::vector<QuantLib::Real>& points() const { return points_; }
62 //! Return the number of buckets in the distribution
63 Size numberBuckets() const { return buckets_.size() - 1; }
64 //! Return the cumulative probabilities of the distribution
65 std::vector<QuantLib::Real> cumulativeProbabilities() const;
66 //! Return 1.0 minus the cumulative probabilities of the distribution
67 std::vector<QuantLib::Real> complementaryProbabilities() const;
68 //! Shift all buckets and points by an additive \p shift
69 void applyShift(QuantLib::Real shift);
70 //! Shift all buckets and points by a multiplicative \p factor
71 void applyFactor(QuantLib::Real factor);
72 //! Return cumulative probability at point \p x using linear interpolation
73 Real cumulativeProbability(QuantLib::Real x) const;
74 //! Return inverse cumulative probability given a probability \p p using linear interpolation
75 Real inverseCumulativeProbability(QuantLib::Real p) const;
76 //! Utility functions
78 //! Create a DiscreteDistribution from a BucketedDistribution with discrete points at midpoints of the buckets
80 //! Erase the first \p n buckets from the distribution
81 /*! \warning This may invalidate the distribution if the buckets erased do not have negligible probability.
82 */
83 void erase(QuantLib::Size n);
84 //! Returns the index of the bucket containing value
85 QuantLib::Size bucket(QuantLib::Real value) const;
86
87private:
88 //! Common code used in the constructor
89 void init(QuantLib::Real min, QuantLib::Real max, QuantLib::Size numberBuckets);
90 //! Vector of numbers denoting buckets of distribution
91 std::vector<QuantLib::Real> buckets_;
92 //! Vector of numbers denoting probabilities in each bucket
93 std::vector<QuantLib::Real> probabilities_;
94 std::vector<QuantLib::Real> points_;
95 std::vector<QuantLib::Real> previousProbabilities_;
96 std::vector<QuantLib::Real> previousPoints_;
97 // Probabilities in a bucket less than this value are considered negligible
98 // FIXME: need to find a better way to deal with very small probabilities
99 const static QuantLib::Real minProbability_;
100};
101
102//! Sum probabilities in two bucketed distributions with equal buckets
104
105//! Multiply probabilities in bucketed distribution by \p factor
106BucketedDistribution operator*(QuantLib::Real factor, const BucketedDistribution& rhs);
107BucketedDistribution operator*(const BucketedDistribution& lhs, QuantLib::Real factor);
108} // namespace QuantExt
109
Represents a bucketed probability distibution.
void erase(QuantLib::Size n)
Erase the first n buckets from the distribution.
BucketedDistribution & operator+=(const BucketedDistribution &other)
Utility functions.
void applyShift(QuantLib::Real shift)
Shift all buckets and points by an additive shift.
BucketedDistribution()
Default constructor.
BucketedDistribution(const std::vector< QuantLib::Real > &buckets, const std::vector< QuantLib::Real > &initialProbabilities, const std::vector< QuantLib::Real > &initialPoints)
Explicitly specify the initial distribution.
BucketedDistribution(QuantLib::Real min, QuantLib::Real max, QuantLib::Size numberBuckets)
Build a default initial distribution from min and max and numberBuckets.
void applyFactor(QuantLib::Real factor)
Shift all buckets and points by a multiplicative factor.
void init(QuantLib::Real min, QuantLib::Real max, QuantLib::Size numberBuckets)
Common code used in the constructor.
const std::vector< QuantLib::Real > & probabilities() const
Get the probabilities.
std::vector< QuantLib::Real > points_
Size numberBuckets() const
Return the number of buckets in the distribution.
Real inverseCumulativeProbability(QuantLib::Real p) const
Return inverse cumulative probability given a probability p using linear interpolation.
std::vector< QuantLib::Real > previousPoints_
const std::vector< QuantLib::Real > & buckets() const
Return the buckets of the distribution.
BucketedDistribution(QuantLib::Real min, QuantLib::Real max, QuantLib::Size numberBuckets, QuantLib::Real initialValue)
std::vector< QuantLib::Real > previousProbabilities_
void add(const DiscreteDistribution &distribution)
Update the bucketed distribution by adding a discrete distribution.
std::vector< QuantLib::Real > cumulativeProbabilities() const
Return the cumulative probabilities of the distribution.
std::vector< QuantLib::Real > & probabilities()
Set the probabilities.
Real cumulativeProbability(QuantLib::Real x) const
Return cumulative probability at point x using linear interpolation.
std::vector< QuantLib::Real > buckets_
Vector of numbers denoting buckets of distribution.
QuantLib::Size bucket(QuantLib::Real value) const
Returns the index of the bucket containing value.
const std::vector< QuantLib::Real > & points() const
Return the points of the distribution.
DiscreteDistribution createDiscrete() const
Create a DiscreteDistribution from a BucketedDistribution with discrete points at midpoints of the bu...
static const QuantLib::Real minProbability_
std::vector< QuantLib::Real > probabilities_
Vector of numbers denoting probabilities in each bucket.
std::vector< QuantLib::Real > complementaryProbabilities() const
Return 1.0 minus the cumulative probabilities of the distribution.
Discretized probability density and cumulative probability.
BucketedDistribution operator+(const BucketedDistribution &lhs, const BucketedDistribution &rhs)
Sum probabilities in two bucketed distributions with equal buckets.
BucketedDistribution operator*(Real factor, const BucketedDistribution &rhs)