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

cern.jet.random.tdouble.Logarithmic 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;

/**
 * Logarithmic distribution.
 * 

* Valid parameter ranges: 0 < p < 1. *

* 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: Inversion/Transformation. *
This is a port of lsk.c from the C-RAND / * WIN-RAND library. C-RAND's implementation, in turn, is based upon *

* A.W. Kemp (1981): Efficient generation of logarithmically distributed * pseudo-random variables, Appl. Statist. 30, 249-253. * * @author [email protected] * @version 1.0, 09/24/99 */ public class Logarithmic extends AbstractContinousDoubleDistribution { /** * */ private static final long serialVersionUID = 1L; protected double my_p; // cached vars for method nextDouble(a) (for performance only) private double t, h, a_prev = -1.0; // The uniform random number generated shared by all static methods. protected static Logarithmic shared = new Logarithmic(0.5, makeDefaultGenerator()); /** * Constructs a Logarithmic distribution. */ public Logarithmic(double p, DoubleRandomEngine randomGenerator) { setRandomGenerator(randomGenerator); setState(p); } /** * Returns a random number from the distribution. */ public double nextDouble() { return nextDouble(this.my_p); } /** * Returns a random number from the distribution; bypasses the internal * state. */ public double nextDouble(double a) { /*********************************************************************** * * Logarithmic Distribution - Inversion/Transformation * * * ***************************************************************** * * The algorithm combines Inversion and Transformation. * It is based on * the following fact: A random variable X from * the Logarithmic * distribution has the property that X for fixed * Y=y is Geometric * distributed with P(X=x|Y=y)=(1-y)*y^(x-1) (*) * where Y has * distribution function F(y)=ln(1-y)/ln(1-p). * So first random numbers * y are generated by simple Inversion, * then k=(long int) * (1+ln(u)/ln(y)) is a Geometric random number * and because of (*) a * Logarithmic one. * To speed up the algorithm squeezes are used as * well as the * fact, that many of the random numbers are 1 or 2 * (depending on * special circumstances). * On an IBM/PC 486 optimal * performance is achieved, if for p<.97 * simple inversion is used and * otherwise the transformation. * On an IBM/PC 286 inversion should be * restricted to p<.90. * * * ***************************************************************** * * FUNCTION: - lsk samples a random number from the * Logarithmic * distribution with * parameter 0 < p < 1 . * REFERENCE: - A.W. Kemp * (1981): Efficient generation of * logarithmically distributed * pseudo-random * variables, Appl. Statist. 30, 249-253. * SUBPROGRAMS: * - drand(seed) ... (0,1)-Uniform generator with * unsigned long * integer *seed. * * **********************************************************************/ double u, v, p, q; int k; if (a != a_prev) { // Set-up a_prev = a; if (a < 0.97) t = -a / Math.log(1.0 - a); else h = Math.log(1.0 - a); } u = randomGenerator.raw(); if (a < 0.97) { // Inversion/Chop-down k = 1; p = t; while (u > p) { // System.out.println("u="+u+", p="+p); u -= p; k++; p *= a * (k - 1.0) / k; } return k; } if (u > a) return 1; // Transformation u = randomGenerator.raw(); v = u; q = 1.0 - Math.exp(v * h); if (u <= q * q) { k = (int) (1 + Math.log(u) / Math.log(q)); return k; } if (u > q) return 1; return 2; } /** * Sets the distribution parameter. */ public void setState(double p) { this.my_p = p; } /** * Returns a random number from the distribution. */ public static double staticNextDouble(double p) { synchronized (shared) { return shared.nextDouble(p); } } /** * Returns a String representation of the receiver. */ public String toString() { return this.getClass().getName() + "(" + my_p + ")"; } /** * 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