All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cern.jet.random.ChiSquare Maven / Gradle / Ivy

/*
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;

import cern.jet.random.engine.RandomEngine;
import cern.jet.stat.Probability;
/**
 * ChiSquare distribution; See the  math definition
 * and  animated definition.
 * 
A special case of the Gamma distribution. *

* p(x) = (1/g(f/2)) * (x/2)^(f/2-1) * exp(-x/2) with 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: Ratio of Uniforms with shift. *
* High performance implementation. This is a port of RandChiSquare used in CLHEP 1.4.0 (C++). * CLHEP's implementation, in turn, is based on chru.c from the C-RAND / WIN-RAND library. * C-RAND's implementation, in turn, is based upon *

J.F. Monahan (1987): An algorithm for generating chi random variables, ACM Trans. Math. Software 13, 168-172. * * @author [email protected] * @version 1.0, 09/24/99 */ public class ChiSquare extends AbstractContinousDistribution { protected double freedom; // cached vars for method nextDouble(a) (for performance only) private double freedom_in = -1.0,b,vm,vp,vd; // The uniform random number generated shared by all static methods. protected static ChiSquare shared = new ChiSquare(1.0,makeDefaultGenerator()); /** * Constructs a ChiSquare distribution. * Example: freedom=1.0. * @param freedom degrees of freedom. * @throws IllegalArgumentException if freedom < 1.0. */ public ChiSquare(double freedom, RandomEngine randomGenerator) { setRandomGenerator(randomGenerator); setState(freedom); } /** * Returns the cumulative distribution function. */ public double cdf(double x) { return Probability.chiSquare(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 freedom degrees of freedom. * It should hold freedom < 1.0. */ public double nextDouble(double freedom) { /****************************************************************** * * * Chi Distribution - Ratio of Uniforms with shift * * * ****************************************************************** * * * FUNCTION : - chru samples a random number from the Chi * * distribution with parameter a > 1. * * REFERENCE : - J.F. Monahan (1987): An algorithm for * * generating chi random variables, ACM Trans. * * Math. Software 13, 168-172. * * SUBPROGRAM : - anEngine ... pointer to a (0,1)-Uniform * * engine * * * * Implemented by R. Kremer, 1990 * ******************************************************************/ double u,v,z,zz,r; //if( a < 1 ) return (-1.0); // Check for invalid input value if (freedom == 1.0) { for(;;) { u = randomGenerator.raw(); v = randomGenerator.raw() * 0.857763884960707; z = v / u; if (z < 0) continue; zz = z * z; r = 2.5 - zz; if (z < 0.0) r = r + zz * z / (3.0 * z); if (u < r * 0.3894003915) return(z*z); if (zz > (1.036961043 / u + 1.4)) continue; if (2.0 * Math.log(u) < (- zz * 0.5 )) return(z*z); } } else { if (freedom != freedom_in) { b = Math.sqrt(freedom - 1.0); vm = - 0.6065306597 * (1.0 - 0.25 / (b * b + 1.0)); vm = (-b > vm) ? -b : vm; vp = 0.6065306597 * (0.7071067812 + b) / (0.5 + b); vd = vp - vm; freedom_in = freedom; } for(;;) { u = randomGenerator.raw(); v = randomGenerator.raw() * vd + vm; z = v / u; if (z < -b) continue; zz = z * z; r = 2.5 - zz; if (z < 0.0) r = r + zz * z / (3.0 * (z + b)); if (u < r * 0.3894003915) return((z + b)*(z + b)); if (zz > (1.036961043 / u + 1.4)) continue; if (2.0 * Math.log(u) < (Math.log(1.0 + z / b) * b * b - zz * 0.5 - z * b)) return((z + b)*(z + b)); } } } /** * Returns the probability distribution function. */ public double pdf(double x) { if (x <= 0.0) throw new IllegalArgumentException(); double logGamma = Fun.logGamma(freedom/2.0); return Math.exp((freedom/2.0 - 1.0) * Math.log(x/2.0) - x/2.0 - logGamma) / 2.0; } /** * Sets the distribution parameter. * @param freedom degrees of freedom. * @throws IllegalArgumentException if freedom < 1.0. */ public void setState(double freedom) { if (freedom<1.0) throw new IllegalArgumentException(); this.freedom = freedom; } /** * Returns a random number from the distribution. * @param freedom degrees of freedom. * @throws IllegalArgumentException if freedom < 1.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(RandomEngine randomGenerator) { synchronized (shared) { shared.setRandomGenerator(randomGenerator); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy