cern.jet.random.tdouble.Exponential Maven / Gradle / Ivy
Show all versions of parallelcolt Show documentation
/*
Copyright (C) 1999 CERN - European Organization for Nuclear Research.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose
is hereby granted without fee, provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear in supporting documentation.
CERN makes no representations about the suitability of this software for any purpose.
It is provided "as is" without expressed or implied warranty.
*/
package cern.jet.random.tdouble;
import cern.jet.random.tdouble.engine.DoubleRandomEngine;
/**
* Exponential Distribution (aka Negative Exponential Distribution); See the math definition
* animated definition.
*
* p(x) = lambda*exp(-x*lambda) for x >= 0,
* lambda > 0.
*
* Instance methods operate on a user supplied uniform random number generator;
* they are unsynchronized.
*
Static methods operate on a default uniform random number generator; they
* are synchronized.
*
*
* @author [email protected]
* @version 1.0, 09/24/99
*/
public class Exponential extends AbstractContinousDoubleDistribution {
/**
*
*/
private static final long serialVersionUID = 1L;
protected double lambda;
// The uniform random number generated shared by all static methods.
protected static Exponential shared = new Exponential(1.0, makeDefaultGenerator());
/**
* Constructs a Negative Exponential distribution.
*/
public Exponential(double lambda, DoubleRandomEngine randomGenerator) {
setRandomGenerator(randomGenerator);
setState(lambda);
}
/**
* Returns the cumulative distribution function.
*/
public double cdf(double x) {
if (x <= 0.0)
return 0.0;
return 1.0 - Math.exp(-x * lambda);
}
/**
* Returns a random number from the distribution.
*/
public double nextDouble() {
return nextDouble(lambda);
}
/**
* Returns a random number from the distribution; bypasses the internal
* state.
*/
public double nextDouble(double lambda) {
return -Math.log(randomGenerator.raw()) / lambda;
}
/**
* Returns the probability distribution function.
*/
public double pdf(double x) {
if (x < 0.0)
return 0.0;
return lambda * Math.exp(-x * lambda);
}
/**
* Sets the mean.
*/
public void setState(double lambda) {
this.lambda = lambda;
}
/**
* Returns a random number from the distribution with the given lambda.
*/
public static double staticNextDouble(double lambda) {
synchronized (shared) {
return shared.nextDouble(lambda);
}
}
/**
* Returns a String representation of the receiver.
*/
public String toString() {
return this.getClass().getName() + "(" + lambda + ")";
}
/**
* Sets the uniform random number generated shared by all static
* methods.
*
* @param randomGenerator
* the new uniform random number generator to be shared.
*/
private static void xstaticSetRandomGenerator(DoubleRandomEngine randomGenerator) {
synchronized (shared) {
shared.setRandomGenerator(randomGenerator);
}
}
}