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

cern.jet.random.tdouble.Normal Maven / Gradle / Ivy

Go to download

Parallel Colt is a multithreaded version of Colt - a library for high performance scientific computing in Java. It contains efficient algorithms for data analysis, linear algebra, multi-dimensional arrays, Fourier transforms, statistics and histogramming.

The newest version!
/*
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;

/**
 * Normal (aka Gaussian) distribution; See the  math definition and 
 * animated definition.
 * 
 * 
 * 
 *  1                       2
 *  pdf(x) = ---------    exp( - (x-mean) / 2v ) 
 *  sqrt(2pi*v)
 * 
 *  x
 *  -
 *  1        | |                 2
 *  cdf(x) = ---------    |    exp( - (t-mean) / 2v ) dt
 *  sqrt(2pi*v)| |
 *  -
 *  -inf.
 * 
* * where v = variance = standardDeviation^2. *

* 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: Polar Box-Muller transformation. See G.E.P. Box, M.E. * Muller (1958): A note on the generation of random normal deviates, Annals * Math. Statist. 29, 610-611. *

* * @author [email protected] * @version 1.0, 09/24/99 */ public class Normal extends AbstractContinousDoubleDistribution { /** * */ private static final long serialVersionUID = 1L; protected double mean; protected double variance; protected double standardDeviation; protected double cache; // cache for Box-Mueller algorithm protected boolean cacheFilled; // Box-Mueller protected double SQRT_INV; // performance cache // The uniform random number generated shared by all static methods. protected static Normal shared = new Normal(0.0, 1.0, makeDefaultGenerator()); /** * Constructs a normal (gauss) distribution. Example: mean=0.0, * standardDeviation=1.0. */ public Normal(double mean, double standardDeviation, DoubleRandomEngine randomGenerator) { setRandomGenerator(randomGenerator); setState(mean, standardDeviation); } /** * Returns the cumulative distribution function. */ public double cdf(double x) { return Probability.normal(mean, variance, x); } /** * Returns a random number from the distribution. */ public double nextDouble() { return nextDouble(this.mean, this.standardDeviation); } /** * Returns a random number from the distribution; bypasses the internal * state. */ public double nextDouble(double mean, double standardDeviation) { // Uses polar Box-Muller transformation. if (cacheFilled && this.mean == mean && this.standardDeviation == standardDeviation) { cacheFilled = false; return cache; } ; double x, y, r, z; do { x = 2.0 * randomGenerator.raw() - 1.0; y = 2.0 * randomGenerator.raw() - 1.0; r = x * x + y * y; } while (r >= 1.0); z = Math.sqrt(-2.0 * Math.log(r) / r); if(this.mean == mean && this.standardDeviation == standardDeviation){ cache = mean + standardDeviation * x * z; cacheFilled = true; } return mean + standardDeviation * y * z; } /** * Returns the probability distribution function. */ public double pdf(double x) { double diff = x - mean; return SQRT_INV * Math.exp(-(diff * diff) / (2.0 * variance)); } /** * Sets the uniform random generator internally used. */ protected void setRandomGenerator(DoubleRandomEngine randomGenerator) { super.setRandomGenerator(randomGenerator); this.cacheFilled = false; } /** * Sets the mean and variance. */ public void setState(double mean, double standardDeviation) { if (mean != this.mean || standardDeviation != this.standardDeviation) { this.mean = mean; this.standardDeviation = standardDeviation; this.variance = standardDeviation * standardDeviation; this.cacheFilled = false; this.SQRT_INV = 1.0 / Math.sqrt(2.0 * Math.PI * variance); } } /** * Returns a random number from the distribution with the given mean and * standard deviation. */ public static double staticNextDouble(double mean, double standardDeviation) { synchronized (shared) { return shared.nextDouble(mean, standardDeviation); } } /** * Returns a String representation of the receiver. */ public String toString() { return this.getClass().getName() + "(" + mean + "," + standardDeviation + ")"; } /** * 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); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy