com.opengamma.strata.math.impl.cern.StudentT Maven / Gradle / Ivy
Show all versions of strata-math Show documentation
/*
* Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
/*
* This code is copied from the original library from the `cern.jet.random` package.
* Changes:
* - package name
* - missing Javadoc param tags
* - reformat
*/
/*
Copyright � 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 com.opengamma.strata.math.impl.cern;
//CSOFF: ALL
/**
* 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 AbstractContinousDistribution {
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.
* @param randomGenerator input
* @throws IllegalArgumentException if freedom <= 0.0.
*/
public StudentT(double freedom, RandomEngine randomGenerator) {
setRandomGenerator(randomGenerator);
setState(freedom);
}
/**
* Returns the cumulative distribution function.
* @param x input
* @return result
*/
public double cdf(double x) {
return Probability.studentT(freedom, x);
}
/**
* Returns a random number from the distribution.
*/
@Override
public double nextDouble() {
return nextDouble(this.freedom);
}
/**
* Returns a random number from the distribution; bypasses the internal state.
* @param degreesOfFreedom degrees of freedom.
* @return result
* @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.
* @param x input
* @return result
*/
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.
* @return result
* @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.
*/
@Override
public String toString() {
return this.getClass().getName() + "(" + freedom + ")";
}
}