Poisson distribution added
authorAlpar Juttner <alpar@cs.elte.hu>
Fri, 14 Mar 2008 16:57:49 +0000
changeset 925d4decd1b870
parent 78 c46b3453455f
child 93 f857981306ea
Poisson distribution added
lemon/random.h
test/random_test.cc
     1.1 --- a/lemon/random.h	Thu Feb 28 17:06:02 2008 +0100
     1.2 +++ b/lemon/random.h	Fri Mar 14 16:57:49 2008 +0000
     1.3 @@ -803,6 +803,29 @@
     1.4        return exponential(gamma(k,1.0/x_min));
     1.5      }  
     1.6        
     1.7 +    /// Poisson distribution
     1.8 +
     1.9 +    /// This function generates a Poisson distribution random number with
    1.10 +    /// parameter \c lambda.
    1.11 +    /// 
    1.12 +    /// The probability mass function of this distribusion is
    1.13 +    /// \f[ \frac{e^{-\lambda}\lambda^k}{k!} \f]
    1.14 +    /// \note The algorithm is taken from the book of Donald E. Knuth titled
    1.15 +    /// ''Seminumerical Algorithms'' (1969). Its running time is linear in the
    1.16 +    /// return value.
    1.17 +    
    1.18 +    int poisson(double lambda)
    1.19 +    {
    1.20 +      const double l = std::exp(-lambda);
    1.21 +      int k=0;
    1.22 +      double p = 1.0;
    1.23 +      do {
    1.24 +	k++;
    1.25 +	p*=real<double>();
    1.26 +      } while (p>=l);
    1.27 +      return k-1;
    1.28 +    }  
    1.29 +      
    1.30      ///@}
    1.31      
    1.32      ///\name Two dimensional distributions
     2.1 --- a/test/random_test.cc	Thu Feb 28 17:06:02 2008 +0100
     2.2 +++ b/test/random_test.cc	Fri Mar 14 16:57:49 2008 +0000
     2.3 @@ -33,4 +33,5 @@
     2.4    a=lemon::rnd.gamma(4);
     2.5    //Does gamma work with integer k?
     2.6    a=lemon::rnd.gamma(4.0,0);
     2.7 +  a=lemon::rnd.poisson(.5);
     2.8  }