QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
binomialdistribution.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
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file binomialdistribution.hpp
21 \brief Binomial distribution
22*/
23
24#ifndef quantlib_binomial_distribution_h
25#define quantlib_binomial_distribution_h
26
27#include <ql/math/factorial.hpp>
28#include <ql/math/beta.hpp>
29
30namespace QuantLib {
31
33
34 QL_REQUIRE(n>=k, "n<k not allowed");
35
37
38 }
39
41
42 return std::floor(0.5+std::exp(binomialCoefficientLn(n, k)));
43
44 }
45
46 //! Binomial probability distribution function
47 /*! formula here ...
48 Given an integer k it returns its probability in a Binomial
49 distribution with parameters p and n.
50 */
52 public:
54 // function
55 Real operator()(BigNatural k) const;
56 private:
59 };
60
61 //! Cumulative binomial distribution function
62 /*! Given an integer k it provides the cumulative probability
63 of observing kk<=k:
64 formula here ...
65
66 */
68 public:
70 // function
72 if (k >= n_)
73 return 1.0;
74 else
75 return 1.0 - incompleteBetaFunction(k+1, n_-k, p_);
76 }
77 private:
80 };
81
82
85 : n_(n) {
86
87 if (p==0.0) {
89 logOneMinusP_ = 0.0;
90 } else if (p==1.0) {
91 logP_ = 0.0;
93 } else {
94 QL_REQUIRE(p>0, "negative p not allowed");
95 QL_REQUIRE(p<1.0, "p>1.0 not allowed");
96
97 logP_ = std::log(p);
98 logOneMinusP_ = std::log(1.0-p);
99 }
100 }
101
102
103 inline
104 CumulativeBinomialDistribution::CumulativeBinomialDistribution(
105 Real p, BigNatural n)
106 : n_(n), p_(p) {
107
108 QL_REQUIRE(p>=0, "negative p not allowed");
109 QL_REQUIRE(p<=1.0, "p>1.0 not allowed");
110
111 }
112
113 inline Real BinomialDistribution::operator()(BigNatural k) const {
114
115 if (k > n_) return 0.0;
116
117 // p==1.0
118 if (logP_==0.0)
119 return (k==n_ ? 1.0 : 0.0);
120 // p==0.0
121 else if (logOneMinusP_==0.0)
122 return (k==0 ? 1.0 : 0.0);
123 else
124 return std::exp(binomialCoefficientLn(n_, k) +
125 k * logP_ + (n_-k) * logOneMinusP_);
126 }
127
128
129
130 /*! Given an odd integer n and a real number z it returns p such that:
131 1 - CumulativeBinomialDistribution((n-1)/2, n, p) =
132 CumulativeNormalDistribution(z)
133
134 \pre n must be odd
135 */
136 inline Real PeizerPrattMethod2Inversion(Real z, BigNatural n) {
137
138 QL_REQUIRE(n%2==1,
139 "n must be an odd number: " << n << " not allowed");
140
141 Real result = (z/(n+1.0/3.0+0.1/(n+1.0)));
142 result *= result;
143 result = std::exp(-result*(n+1.0/6.0));
144 result = 0.5 + (z>0 ? 1 : -1) * std::sqrt((0.25 * (1.0-result)));
145 return result;
146 }
147
148}
149
150
151#endif
Beta and beta incomplete functions.
Binomial probability distribution function.
BinomialDistribution(Real p, BigNatural n)
Real operator()(BigNatural k) const
Cumulative binomial distribution function.
static Real ln(Natural n)
Definition: factorial.cpp:58
#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
Definition: any.hpp:35
Real incompleteBetaFunction(Real a, Real b, Real x, Real accuracy, Integer maxIteration)
Incomplete Beta function.
Definition: beta.cpp:67
Real binomialCoefficient(BigNatural n, BigNatural k)
Real binomialCoefficientLn(BigNatural n, BigNatural k)
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46