Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
discretedistribution.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/*! \file qle/math/discretedistribution.hpp
20 \brief Discretized probability density and cumulative probability
21 */
22
23#pragma once
24
25#include <fstream>
26#include <vector>
27
28#include <ql/errors.hpp>
29#include <ql/instruments/payoffs.hpp>
30
31namespace QuantExt {
32using namespace std;
33using namespace QuantLib;
34
35//! Distributionpair is a helper class for DiscretDistribution
36/*!
37 The Distributionpair holds a single point of a one-domensional
38 discrete distribution, i.e. coordinate x and ordinate (density) y.
39
40 \ingroup math
41 */
42
44public:
45 Distributionpair(Real x = 0, Real y = 0) : x_(x), y_(y) {}
46 Real x_, y_;
47};
48
49inline bool operator<(const Distributionpair& p1, const Distributionpair& p2) { return p1.x_ < p2.x_; }
50
51inline bool operator>(const Distributionpair& p1, const Distributionpair& p2) { return p1.x_ > p2.x_; }
52
53class MDD;
54//! Discrete Distribution
55/*!
56 This class implements a one-dimensional distribution in terms of a
57 vector of Distributionpairs.
58
59 \ingroup math
60 */
62public:
63 DiscreteDistribution(const vector<Distributionpair>&);
64 //! Default constructor with probability 1.0 at 0.0
66 //! Construct a discrete probability distribution by giving the points and probabilities
67 DiscreteDistribution(const vector<Real>& dataPoints, const vector<Real>& probabilities);
68 // inspectors
69 virtual Size size() const;
70 virtual vector<Distributionpair> get() const;
71 virtual Distributionpair get(Size i) const;
72 //! Return probability for data at index \p i
73 Real probability(Size i) const;
74 //! Return data at index \p i
75 Real data(Size i) const;
76
78
79 friend class MDD;
80
81protected:
82 vector<Distributionpair> data_;
83};
84//! Modify Distrete Distribution
85/*!
86 This class implements a set of operations on discrete disctributions, that
87 involve one or two distributions.
88
89 \todo Complete and check the member function documentation (Donal)
90 \ingroup math
91 */
92
93class MDD {
94public:
95 /*!
96 Convolution of two discrete distribution
97 */
98 static DiscreteDistribution convolve(const DiscreteDistribution& a, const DiscreteDistribution& b, Size buckets);
99 /*!
100 Amend the discretization of the distribution such that the number of
101 buckets is reduced to the given number.
102 */
104 /*!
105 Amend the discretization of the distribution such that the distance
106 of adjacent buckets is reduced to the given number.
107 */
109 /*!
110 Add two discrete distributions while introducing a desired number
111 of buckets.
112 */
113 static DiscreteDistribution sum(const DiscreteDistribution& a, const DiscreteDistribution& b, Size buckets);
114 /*!
115 Add c * distribution b to distribution a, starting from the left.
116 */
118 Real c);
119 /*!
120 Add c * distribution b to distribution a, starting from the right.
121 */
123 /*!
124 TODO
125 */
127 /*!
128 TODO
129 */
131 /*!
132 Scale each density by factor b.
133 */
134 static DiscreteDistribution scalarmultprob(const DiscreteDistribution& a, const Real& b);
135 /*!
136 Scale each coordinate by factor x.
137 */
138 static DiscreteDistribution scalarmultx(const DiscreteDistribution& a, const Real& b);
139 /*!
140 Shift each coordinate by amount b.
141 */
142 static DiscreteDistribution scalarshiftx(const DiscreteDistribution& a, const Real& b);
143 /*!
144 CHECK:
145 Cut off the branch of the distribution to the left of coordinate b and
146 subsitute it with a single oint at coordinate b holding the
147 cumulative probability up to b.
148 */
149 static DiscreteDistribution functionmax(const DiscreteDistribution& a, const Real& b);
150 /*!
151 Apply function F to each coordinate.
152 */
153 template <class F> static DiscreteDistribution function(F&, const DiscreteDistribution& a);
154 /*!
155 TODO
156 */
157 static DiscreteDistribution functionmin(const DiscreteDistribution& a, const Real& b);
158 /*!
159 Return the expected coordinate value.
160 */
161 static Real expectation(const DiscreteDistribution& a);
162 /*!
163 Return the standard deviation of the discrete distribution.
164*/
165 static Real stdev(const DiscreteDistribution& a);
166 /*!
167 TODO
168 */
169 static Real leftstdev(const DiscreteDistribution& a);
170 /*!
171 Print the distribution of the provided stream.
172 */
173 static Real print(const DiscreteDistribution& a, const ostringstream& o);
174 /*!
175 Probability matching:
176
177 Compute the cumulative probability P_b(c) of distribution b up to
178 the provided coordintae c.
179
180 Compute the coordinate c* of distribution a where its cumulative
181 probability equals P_b(c), i.e. P_a(c*) = P_b(c).
182
183 Return coordinate c*.
184 */
185 static Real probabilitymatch(const DiscreteDistribution& a, const DiscreteDistribution& b, Real c, bool forward);
186
187 //! Probability matching with linear interpolation
188 static Real probabilitymatch(const DiscreteDistribution& a, const DiscreteDistribution& b, Real c);
189};
190
191template <class F> inline DiscreteDistribution MDD::function(F& f, const DiscreteDistribution& a) {
192 vector<Distributionpair> x1pm1 = a.get();
193 vector<Distributionpair> func;
194
195 for (Size i = 0; i < x1pm1.size(); i++) {
196 Distributionpair xp(f(x1pm1[i].x_), x1pm1[i].y_);
197 func.push_back(xp);
198 }
199 return DiscreteDistribution(func);
200}
201
202} // namespace QuantExt
203
DiscreteDistribution()
Default constructor with probability 1.0 at 0.0.
virtual vector< Distributionpair > get() const
Real data(Size i) const
Return data at index i.
Real probability(Size i) const
Return probability for data at index i.
vector< Distributionpair > data_
Distributionpair is a helper class for DiscretDistribution.
Distributionpair(Real x=0, Real y=0)
Modify Distrete Distribution.
static DiscreteDistribution scalarshiftx(const DiscreteDistribution &a, const Real &b)
static DiscreteDistribution scalarmultprob(const DiscreteDistribution &a, const Real &b)
static DiscreteDistribution scalarmultx(const DiscreteDistribution &a, const Real &b)
static DiscreteDistribution splicemezz(const DiscreteDistribution &a, const DiscreteDistribution &b, Real c)
static DiscreteDistribution functionmax(const DiscreteDistribution &a, const Real &b)
static DiscreteDistribution sum(const DiscreteDistribution &a, const DiscreteDistribution &b, Size buckets)
static DiscreteDistribution sumspecialunsorted(const DiscreteDistribution &a, const DiscreteDistribution &b, Real c)
static DiscreteDistribution function(F &, const DiscreteDistribution &a)
static DiscreteDistribution rebucketfixedstep(const DiscreteDistribution &a, Real step)
static Real probabilitymatch(const DiscreteDistribution &a, const DiscreteDistribution &b, Real c, bool forward)
static DiscreteDistribution sumspecialright(const DiscreteDistribution &a, const DiscreteDistribution &b, Real c)
static Real expectation(const DiscreteDistribution &a)
static Real stdev(const DiscreteDistribution &a)
static DiscreteDistribution rebucketfixednumber(const DiscreteDistribution &a, Size buckets)
static DiscreteDistribution functionmin(const DiscreteDistribution &a, const Real &b)
static DiscreteDistribution sumspecial(const DiscreteDistribution &a, const DiscreteDistribution &b, Real c)
static Real leftstdev(const DiscreteDistribution &a)
static DiscreteDistribution convolve(const DiscreteDistribution &a, const DiscreteDistribution &b, Size buckets)
static Real print(const DiscreteDistribution &a, const ostringstream &o)
bool operator<(const Dividend &d1, const Dividend &d2)
bool operator>(const Currency &, const Currency &)
Definition: bondbasket.hpp:37