gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Poisson distribution added
0 2 0
default
2 files changed with 24 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
... ...
@@ -758,96 +758,119 @@
758 758
    ///
759 759
    double gamma(double k,double theta=1.0)
760 760
    {
761 761
      double xi,nu;
762 762
      const double delta = k-std::floor(k);
763 763
      const double v0=E/(E-delta);
764 764
      do {
765 765
	double V0=1.0-real<double>();
766 766
	double V1=1.0-real<double>();
767 767
	double V2=1.0-real<double>();
768 768
	if(V2<=v0) 
769 769
	  {
770 770
	    xi=std::pow(V1,1.0/delta);
771 771
	    nu=V0*std::pow(xi,delta-1.0);
772 772
	  }
773 773
	else 
774 774
	  {
775 775
	    xi=1.0-std::log(V1);
776 776
	    nu=V0*std::exp(-xi);
777 777
	  }
778 778
      } while(nu>std::pow(xi,delta-1.0)*std::exp(-xi));
779 779
      return theta*(xi-gamma(int(std::floor(k))));
780 780
    }
781 781
    
782 782
    /// Weibull distribution
783 783

	
784 784
    /// This function generates a Weibull distribution random number.
785 785
    /// 
786 786
    ///\param k shape parameter (<tt>k>0</tt>)
787 787
    ///\param lambda scale parameter (<tt>lambda>0</tt>)
788 788
    ///
789 789
    double weibull(double k,double lambda)
790 790
    {
791 791
      return lambda*pow(-std::log(1.0-real<double>()),1.0/k);
792 792
    }  
793 793
      
794 794
    /// Pareto distribution
795 795

	
796 796
    /// This function generates a Pareto distribution random number.
797 797
    /// 
798 798
    ///\param k shape parameter (<tt>k>0</tt>)
799 799
    ///\param x_min location parameter (<tt>x_min>0</tt>)
800 800
    ///
801 801
    double pareto(double k,double x_min)
802 802
    {
803 803
      return exponential(gamma(k,1.0/x_min));
804 804
    }  
805 805
      
806
    /// Poisson distribution
807

	
808
    /// This function generates a Poisson distribution random number with
809
    /// parameter \c lambda.
810
    /// 
811
    /// The probability mass function of this distribusion is
812
    /// \f[ \frac{e^{-\lambda}\lambda^k}{k!} \f]
813
    /// \note The algorithm is taken from the book of Donald E. Knuth titled
814
    /// ''Seminumerical Algorithms'' (1969). Its running time is linear in the
815
    /// return value.
816
    
817
    int poisson(double lambda)
818
    {
819
      const double l = std::exp(-lambda);
820
      int k=0;
821
      double p = 1.0;
822
      do {
823
	k++;
824
	p*=real<double>();
825
      } while (p>=l);
826
      return k-1;
827
    }  
828
      
806 829
    ///@}
807 830
    
808 831
    ///\name Two dimensional distributions
809 832
    ///
810 833

	
811 834
    ///@{
812 835
    
813 836
    /// Uniform distribution on the full unit circle
814 837

	
815 838
    /// Uniform distribution on the full unit circle.
816 839
    ///
817 840
    dim2::Point<double> disc() 
818 841
    {
819 842
      double V1,V2;
820 843
      do {
821 844
	V1=2*real<double>()-1;
822 845
	V2=2*real<double>()-1;
823 846
	
824 847
      } while(V1*V1+V2*V2>=1);
825 848
      return dim2::Point<double>(V1,V2);
826 849
    }
827 850
    /// A kind of two dimensional Gauss distribution
828 851

	
829 852
    /// This function provides a turning symmetric two-dimensional distribution.
830 853
    /// Both coordinates are of standard normal distribution, but they are not
831 854
    /// independent.
832 855
    ///
833 856
    /// \note The coordinates are the two random variables provided by
834 857
    /// the Box-Muller method.
835 858
    dim2::Point<double> gauss2()
836 859
    {
837 860
      double V1,V2,S;
838 861
      do {
839 862
	V1=2*real<double>()-1;
840 863
	V2=2*real<double>()-1;
841 864
	S=V1*V1+V2*V2;
842 865
      } while(S>=1);
843 866
      double W=std::sqrt(-2*std::log(S)/S);
844 867
      return dim2::Point<double>(W*V1,W*V2);
845 868
    }
846 869
    /// A kind of two dimensional exponential distribution
847 870

	
848 871
    /// This function provides a turning symmetric two-dimensional distribution.
849 872
    /// The x-coordinate is of conditionally exponential distribution
850 873
    /// with the condition that x is positive and y=0. If x is negative and 
851 874
    /// y=0 then, -x is of exponential distribution. The same is true for the
852 875
    /// y-coordinate.
853 876
    dim2::Point<double> exponential2() 
Ignore white space 96 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/random.h>
20 20
#include "test_tools.h"
21 21

	
22 22
///\file \brief Test cases for random.h
23 23
///
24 24
///\todo To be extended
25 25
///
26 26

	
27 27
int main()
28 28
{
29 29
  double a=lemon::rnd();
30 30
  check(a<1.0&&a>0.0,"This should be in [0,1)");
31 31
  a=lemon::rnd.gauss();
32 32
  a=lemon::rnd.gamma(3.45,0);
33 33
  a=lemon::rnd.gamma(4);
34 34
  //Does gamma work with integer k?
35 35
  a=lemon::rnd.gamma(4.0,0);
36
  a=lemon::rnd.poisson(.5);
36 37
}
0 comments (0 inline)