QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
poissondistribution.hpp
Go to the documentation of this file.
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
21/*! \file poissondistribution.hpp
22 \brief Poisson distribution
23*/
24
25#ifndef quantlib_poisson_distribution_hpp
26#define quantlib_poisson_distribution_hpp
27
28#include <ql/math/factorial.hpp>
30
31namespace QuantLib {
32
33 //! Poisson distribution function
34 /*! Given an integer \f$ k \f$, it returns its probability
35 in a Poisson distribution.
36
37 \test the correctness of the returned value is tested by
38 checking it against known good results.
39 */
41 public:
43 // function
44 Real operator()(BigNatural k) const;
45 private:
47 };
48
49
50 //! Cumulative Poisson distribution function
51 /*! This function provides an approximation of the
52 integral of the Poisson distribution.
53
54 For this implementation see
55 "Numerical Recipes in C", 2nd edition,
56 Press, Teukolsky, Vetterling, Flannery, chapter 6
57
58 \test the correctness of the returned value is tested by
59 checking it against known good results.
60 */
62 public:
65 return 1.0 - incompleteGammaFunction(k+1, mu_);
66 }
67 private:
69 };
70
71
72 //! Inverse cumulative Poisson distribution function
73 /*! \test the correctness of the returned value is tested by
74 checking it against known good results.
75 */
77 public:
78 InverseCumulativePoisson(Real lambda = 1.0);
79 Real operator()(Real x) const;
80 private:
82 Real calcSummand(BigNatural index) const;
83 };
84
85
86
87 // inline definitions
88
90 : mu_(mu) {
91
92 QL_REQUIRE(mu_>=0.0,
93 "mu must be non negative (" << mu_ << " not allowed)");
94
95 if (mu_!=0.0) logMu_ = std::log(mu_);
96 }
97
99 if (mu_==0.0) {
100 if (k==0) return 1.0;
101 else return 0.0;
102 }
103 Real logFactorial = Factorial::ln(k);
104 return std::exp(k*std::log(mu_) - logFactorial - mu_);
105 }
106
107
109 : lambda_(lambda) {
110 QL_REQUIRE(lambda_ > 0.0, "lambda must be positive");
111 }
112
114 QL_REQUIRE(x >= 0.0 && x <= 1.0,
115 "Inverse cumulative Poisson distribution is "
116 "only defined on the interval [0,1]");
117
118 if (x == 1.0)
119 return QL_MAX_REAL;
120
121 Real sum = 0.0;
122 BigNatural index = 0;
123 while (x > sum) {
124 sum += calcSummand(index);
125 index++;
126 }
127
128 return Real(index-1);
129 }
130
132 return std::exp(-lambda_) * std::pow(lambda_, Integer(index)) /
133 Factorial::get(index);
134 }
135
136}
137
138
139#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
Poisson distribution function.
Real operator()(BigNatural k) const
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Factorial numbers calculator.
#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
Incomplete Gamma function.
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