package umontreal.iro.lecuyer.probdist;


/**
 * Extends the {@link LognormalDist} class with a constructor accepting the
 * mean m and the variance v of the distribution as arguments.
 * The mean and variance of a lognormal random variable with
 * parameters μ and σ are
 * 
 * eμ+σ2/2 and
 * 
 * e2μ+σ2(eσ2 - 1) respectively, so
 * the parameters are given by 
 * σ2 = ln(v/m2+1) and
 * 
 * μ = ln(m) - σ2/2.
 * 
 */
public class LognormalDistFromMoments extends LognormalDist {



   public LognormalDistFromMoments (double mean, double var) {
      super (getMu (mean, var), Math.sqrt (getSigma2 (mean, var)));
   }

   private static double getMu (double mean, double var) {
      final double sigma2 = getSigma2 (mean, var);
      return Math.log (mean) - sigma2 / 2.0;
   }

   private static double getSigma2 (double mean, double var) {
      if (mean <= 0)
         throw new IllegalArgumentException ("Mean must be positive");
      if (var <= 0)
         throw new IllegalArgumentException ("Variance must be positive");
      return Math.log (var / (mean * mean) + 1);
   }
}

 

Ads help maintain this website.