QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
poissondistribution.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) 2004 Walter Penschke
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_poisson_distribution_hpp
26#define quantlib_poisson_distribution_hpp
27
28#include <ql/math/factorial.hpp>
29#include <ql/math/incompletegamma.hpp>
30
31namespace QuantLib {
32
34
41 public:
45 QL_DEPRECATED
47
51 QL_DEPRECATED
53
55 // function
56 Real operator()(BigNatural k) const;
57 private:
59 };
60
61
63
74 public:
78 QL_DEPRECATED
80
84 QL_DEPRECATED
86
89 return 1.0 - incompleteGammaFunction(k+1, mu_);
90 }
91 private:
93 };
94
95
97
101 public:
105 QL_DEPRECATED
107
111 QL_DEPRECATED
113
114 InverseCumulativePoisson(Real lambda = 1.0);
115 Real operator()(Real x) const;
116 private:
118 Real calcSummand(BigNatural index) const;
119 };
120
121
122
123 // inline definitions
124
126 : mu_(mu) {
127
128 QL_REQUIRE(mu_>=0.0,
129 "mu must be non negative (" << mu_ << " not allowed)");
130
131 if (mu_!=0.0) logMu_ = std::log(mu_);
132 }
133
135 if (mu_==0.0) {
136 if (k==0) return 1.0;
137 else return 0.0;
138 }
139 Real logFactorial = Factorial::ln(k);
140 return std::exp(k*std::log(mu_) - logFactorial - mu_);
141 }
142
143
145 : lambda_(lambda) {
146 QL_REQUIRE(lambda_ > 0.0, "lambda must be positive");
147 }
148
150 QL_REQUIRE(x >= 0.0 && x <= 1.0,
151 "Inverse cumulative Poisson distribution is "
152 "only defined on the interval [0,1]");
153
154 if (x == 1.0)
155 return QL_MAX_REAL;
156
157 Real sum = 0.0;
158 BigNatural index = 0;
159 while (x > sum) {
160 sum += calcSummand(index);
161 index++;
162 }
163
164 return Real(index-1);
165 }
166
168 return std::exp(-lambda_) * std::pow(lambda_, Integer(index)) /
169 Factorial::get(index);
170 }
171
172}
173
174
175#endif
Cumulative Poisson distribution function.
static Real get(Natural n)
Definition: factorial.cpp:50
static Real ln(Natural n)
Definition: factorial.cpp:58
Inverse cumulative Poisson distribution function.
Real calcSummand(BigNatural index) const
QL_DEPRECATED typedef Real argument_type
QL_DEPRECATED typedef Real result_type
Poisson distribution function.
Real operator()(BigNatural k) const
QL_DEPRECATED typedef Real argument_type
QL_DEPRECATED typedef Real result_type
#define QL_MAX_REAL
Definition: qldefines.hpp:176
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:35
Real incompleteGammaFunction(Real a, Real x, Real accuracy, Integer maxIteration)
Incomplete Gamma function.
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46