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

com.opengamma.strata.math.impl.cern.Normal Maven / Gradle / Ivy

There is a newer version: 2.12.46
Show newest version
/*
 * 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
 * - remove unused method
 */
/*
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
/**
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 AbstractContinousDistribution { 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. * @param mean mean * @param standardDeviation standard deviation * @param randomGenerator generator */ public Normal(double mean, double standardDeviation, RandomEngine randomGenerator) { setRandomGenerator(randomGenerator); setState(mean, standardDeviation); } /** * Returns the cumulative distribution function. * @param x x * @return result */ public double cdf(double x) { return Probability.normal(mean, variance, x); } /** * Returns a random number from the distribution. */ @Override public double nextDouble() { return nextDouble(this.mean, this.standardDeviation); } /** * Returns a random number from the distribution; bypasses the internal state. * @param mean mean * @param standardDeviation standard deviation * @return result */ 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); cache = mean + standardDeviation * x * z; cacheFilled = true; return mean + standardDeviation * y * z; } /** * Returns the probability distribution function. * @param x x * @return result */ 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. */ @Override protected void setRandomGenerator(RandomEngine randomGenerator) { super.setRandomGenerator(randomGenerator); this.cacheFilled = false; } /** * Sets the mean and variance. * @param mean mean * @param standardDeviation standard deviation */ 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. * @param mean mean * @param standardDeviation standard deviation * @return result */ public static double staticNextDouble(double mean, double standardDeviation) { synchronized (shared) { return shared.nextDouble(mean, standardDeviation); } } /** * Returns a String representation of the receiver. */ @Override public String toString() { return this.getClass().getName() + "(" + mean + "," + standardDeviation + ")"; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy