cern.jet.random.tdouble.StudentT 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;
import cern.jet.stat.tdouble.Probability;
/**
* StudentT distribution (aka T-distribution); See the math definition and
* animated definition.
*
* p(x) = k * (1+x^2/f) ^ -(f+1)/2 where
* k = g((f+1)/2) / (sqrt(pi*f) * g(f/2)) and g(a) being the
* gamma function and f being the degrees of freedom.
*
* Valid parameter ranges: freedom > 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.
*
* Implementation:
*
Method: Adapted Polar Box-Muller transformation.
* This is a port of RandStudentT used in CLHEP 1.4.0 (C++). CLHEP's
* implementation, in turn, is based on tpol.c from the C-RAND /
* WIN-RAND library. C-RAND's implementation, in turn, is based upon
*
* R.W. Bailey (1994): Polar generation of random variates with the
* t-distribution, Mathematics of Computation 62, 779-781.
*
* @author [email protected]
* @version 1.0, 09/24/99
*/
public class StudentT extends AbstractContinousDoubleDistribution {
/**
*
*/
private static final long serialVersionUID = 1L;
protected double freedom;
protected double TERM; // performance cache for pdf()
// The uniform random number generated shared by all static methods.
protected static StudentT shared = new StudentT(1.0, makeDefaultGenerator());
/**
* Constructs a StudentT distribution. Example: freedom=1.0.
*
* @param freedom
* degrees of freedom.
* @throws IllegalArgumentException
* if freedom <= 0.0.
*/
public StudentT(double freedom, DoubleRandomEngine randomGenerator) {
setRandomGenerator(randomGenerator);
setState(freedom);
}
/**
* Returns the cumulative distribution function.
*/
public double cdf(double x) {
return Probability.studentT(freedom, x);
}
/**
* Returns a random number from the distribution.
*/
public double nextDouble() {
return nextDouble(this.freedom);
}
/**
* Returns a random number from the distribution; bypasses the internal
* state.
*
* @param degreesOfFreedom
* degrees of freedom.
* @throws IllegalArgumentException
* if a <= 0.0.
*/
public double nextDouble(double degreesOfFreedom) {
/*
* The polar method of Box/Muller for generating Normal variates is
* adapted to the Student-t distribution. The two generated variates are
* not independent and the expected no. of uniforms per variate is
* 2.5464.
*
* REFERENCE : - R.W. Bailey (1994): Polar generation of random variates
* with the t-distribution, Mathematics of Computation 62, 779-781.
*/
if (degreesOfFreedom <= 0.0)
throw new IllegalArgumentException();
double u, v, w;
do {
u = 2.0 * randomGenerator.raw() - 1.0;
v = 2.0 * randomGenerator.raw() - 1.0;
} while ((w = u * u + v * v) > 1.0);
return (u * Math.sqrt(degreesOfFreedom * (Math.exp(-2.0 / degreesOfFreedom * Math.log(w)) - 1.0) / w));
}
/**
* Returns the probability distribution function.
*/
public double pdf(double x) {
return this.TERM * Math.pow((1 + x * x / freedom), -(freedom + 1) * 0.5);
}
/**
* Sets the distribution parameter.
*
* @param freedom
* degrees of freedom.
* @throws IllegalArgumentException
* if freedom <= 0.0.
*/
public void setState(double freedom) {
if (freedom <= 0.0)
throw new IllegalArgumentException();
this.freedom = freedom;
double val = Fun.logGamma((freedom + 1) / 2) - Fun.logGamma(freedom / 2);
this.TERM = Math.exp(val) / Math.sqrt(Math.PI * freedom);
}
/**
* Returns a random number from the distribution.
*
* @param freedom
* degrees of freedom.
* @throws IllegalArgumentException
* if freedom <= 0.0.
*/
public static double staticNextDouble(double freedom) {
synchronized (shared) {
return shared.nextDouble(freedom);
}
}
/**
* Returns a String representation of the receiver.
*/
public String toString() {
return this.getClass().getName() + "(" + freedom + ")";
}
/**
* 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);
}
}
}