[Lemon-commits] Alpar Juttner: Poisson distribution added
Lemon HG
hg at lemon.cs.elte.hu
Mon Mar 17 20:22:52 CET 2008
details: http://lemon.cs.elte.hu/hg/lemon/rev/5d4decd1b870
changeset: 92:5d4decd1b870
user: Alpar Juttner <alpar [at] cs.elte.hu>
date: Fri Mar 14 16:57:49 2008 +0000
description:
Poisson distribution added
diffstat:
2 files changed, 24 insertions(+)
lemon/random.h | 23 +++++++++++++++++++++++
test/random_test.cc | 1 +
diffs (42 lines):
diff -r c46b3453455f -r 5d4decd1b870 lemon/random.h
--- a/lemon/random.h Thu Feb 28 17:06:02 2008 +0100
+++ b/lemon/random.h Fri Mar 14 16:57:49 2008 +0000
@@ -803,6 +803,29 @@ namespace lemon {
return exponential(gamma(k,1.0/x_min));
}
+ /// Poisson distribution
+
+ /// This function generates a Poisson distribution random number with
+ /// parameter \c lambda.
+ ///
+ /// The probability mass function of this distribusion is
+ /// \f[ \frac{e^{-\lambda}\lambda^k}{k!} \f]
+ /// \note The algorithm is taken from the book of Donald E. Knuth titled
+ /// ''Seminumerical Algorithms'' (1969). Its running time is linear in the
+ /// return value.
+
+ int poisson(double lambda)
+ {
+ const double l = std::exp(-lambda);
+ int k=0;
+ double p = 1.0;
+ do {
+ k++;
+ p*=real<double>();
+ } while (p>=l);
+ return k-1;
+ }
+
///@}
///\name Two dimensional distributions
diff -r c46b3453455f -r 5d4decd1b870 test/random_test.cc
--- a/test/random_test.cc Thu Feb 28 17:06:02 2008 +0100
+++ b/test/random_test.cc Fri Mar 14 16:57:49 2008 +0000
@@ -33,4 +33,5 @@ int main()
a=lemon::rnd.gamma(4);
//Does gamma work with integer k?
a=lemon::rnd.gamma(4.0,0);
+ a=lemon::rnd.poisson(.5);
}
More information about the Lemon-commits
mailing list