Lognormal distribution added (#102)
authorAlpar Juttner <alpar@cs.elte.hu>
Thu, 19 Jun 2008 17:33:06 +0100
changeset 3392593e163e407
parent 338 b77fb8c32707
child 340 0badf3bb38c2
child 341 f8832dc16d45
Lognormal distribution added (#102)
lemon/random.h
     1.1 --- a/lemon/random.h	Thu Oct 23 12:39:39 2008 +0200
     1.2 +++ b/lemon/random.h	Thu Jun 19 17:33:06 2008 +0100
     1.3 @@ -840,6 +840,46 @@
     1.4        return gauss()*std_dev+mean;
     1.5      }
     1.6  
     1.7 +    /// Lognormal distribution
     1.8 +
     1.9 +    /// Lognormal distribution. The parameters are the mean and the standard
    1.10 +    /// deviation of <tt>exp(X)</tt>.
    1.11 +    ///
    1.12 +    double lognormal(double n_mean,double n_std_dev)
    1.13 +    {
    1.14 +      return std::exp(gauss(n_mean,n_std_dev));
    1.15 +    }
    1.16 +    /// Lognormal distribution
    1.17 +
    1.18 +    /// Lognormal distribution. The parameter is an <tt>std::pair</tt> of
    1.19 +    /// the mean and the standard deviation of <tt>exp(X)</tt>.
    1.20 +    ///
    1.21 +    double lognormal(const std::pair<double,double> &params)
    1.22 +    {
    1.23 +      return std::exp(gauss(params.first,params.second));
    1.24 +    }
    1.25 +    /// Compute the lognormal parameters from mean and standard deviation
    1.26 +
    1.27 +    /// This function computes the lognormal parameters from mean and
    1.28 +    /// standard deviation. The return value can direcly be passed to
    1.29 +    /// lognormal().
    1.30 +    std::pair<double,double> lognormalParamsFromMD(double mean,
    1.31 +						   double std_dev)
    1.32 +    {
    1.33 +      double fr=std_dev/mean;
    1.34 +      fr*=fr;
    1.35 +      double lg=std::log(1+fr);
    1.36 +      return std::pair<double,double>(std::log(mean)-lg/2.0,std::sqrt(lg));
    1.37 +    }
    1.38 +    /// Lognormal distribution with given mean and standard deviation
    1.39 +    
    1.40 +    /// Lognormal distribution with given mean and standard deviation.
    1.41 +    ///
    1.42 +    double lognormalMD(double mean,double std_dev)
    1.43 +    {
    1.44 +      return lognormal(lognormalParamsFromMD(mean,std_dev));
    1.45 +    }
    1.46 +    
    1.47      /// Exponential distribution with given mean
    1.48  
    1.49      /// This function generates an exponential distribution random number