COIN-OR::LEMON - Graph Library

Changeset 2483:bf6d7b624d5c in lemon-0.x for lemon/random.h


Ignore:
Timestamp:
10/01/07 20:57:21 (17 years ago)
Author:
Alpar Juttner
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3322
Message:
  • Gamma distributon random variable.
  • Test file for random.h
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/random.h

    r2391 r2483  
    749749    }
    750750
     751    double gamma(int k)
     752    {
     753      double s = 0;
     754      for(int i=0;i<k;i++) s-=std::log(1.0-real<double>());
     755      return s;
     756    }
     757   
     758    /// Gamma distribution with given shape and scale parameter
     759
     760    /// This function generates a gamma distribution random number.
     761    ///
     762    ///\param k shape parameter (<tt>k>0</tt>)
     763    ///\param theta scale parameter
     764    ///
     765    double gamma(double k,double theta=1.0)
     766    {
     767      double xi,nu;
     768      const double delta = k-std::floor(k);
     769      const double v0=M_E/(M_E-delta);
     770      do {
     771        double V0=1.0-real<double>();
     772        double V1=1.0-real<double>();
     773        double V2=1.0-real<double>();
     774        if(V2<=v0)
     775          {
     776            xi=std::pow(V1,1.0/delta);
     777            nu=V0*std::pow(xi,delta-1.0);
     778          }
     779        else
     780          {
     781            xi=1.0-std::log(V1);
     782            nu=V0*std::exp(-xi);
     783          }
     784      } while(nu>std::pow(xi,delta-1.0)*std::exp(-xi));
     785      return theta*(xi-gamma(int(std::floor(k))));
     786    }
     787   
     788     
    751789    ///@}
    752790   
Note: See TracChangeset for help on using the changeset viewer.