Example usage for org.apache.commons.math3.distribution BetaDistribution sample

List of usage examples for org.apache.commons.math3.distribution BetaDistribution sample

Introduction

In this page you can find the example usage for org.apache.commons.math3.distribution BetaDistribution sample.

Prototype

public double sample() 

Source Link

Document

The default implementation uses the <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> inversion method.

Usage

From source file:math.test.TestDimpleRandom.java

/**
 * Measures speed of Apache vs Colt generators
 *///  w  w w.j a v  a  2s . co m
public static void main(String[] args) {
    RandomGenerator apacheGenerator = new MersenneTwister(42);
    cern.jet.random.engine.RandomEngine cernGenerator = new cern.jet.random.engine.MersenneTwister();

    final int N = 100000;
    long start, end;

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        apacheGenerator.nextDouble();
    }
    end = System.nanoTime();
    long apacheTime = end - start;

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        cernGenerator.nextDouble();
    }
    end = System.nanoTime();

    long cernTime = end - start;

    System.out.format("MersenneTwister.nextDouble() x %d apache/cern %f\n", N, (double) apacheTime / cernTime);

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        BetaDistribution apacheBeta = new BetaDistribution(apacheGenerator, 1.0, 1.0);
        apacheBeta.sample();
    }
    end = System.nanoTime();
    apacheTime = end - start;

    cern.jet.random.Beta cernBeta = new cern.jet.random.Beta(1, 1, cernGenerator);

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        cernBeta.nextDouble();
    }
    end = System.nanoTime();
    apacheTime = end - start;

    System.out.format("Beta x %d apache/cern %f\n", N, (double) apacheTime / cernTime);

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        GammaDistribution apacheGamma = new GammaDistribution(apacheGenerator, 1.0, 1.0);
        apacheGamma.sample();
    }
    end = System.nanoTime();
    apacheTime = end - start;

    cern.jet.random.Gamma cernGamma = new cern.jet.random.Gamma(1, 1, cernGenerator);

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        cernGamma.nextDouble();
    }
    end = System.nanoTime();
    apacheTime = end - start;

    System.out.format("Gamma x %d apache/cern %f\n", N, (double) apacheTime / cernTime);

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        BinomialDistribution apacheBinomial = new BinomialDistribution(apacheGenerator, 1, .5);
        apacheBinomial.sample();
    }
    end = System.nanoTime();
    apacheTime = end - start;

    cern.jet.random.Binomial cernBinomial = new cern.jet.random.Binomial(1, .5, cernGenerator);

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        cernBinomial.nextInt();
    }
    end = System.nanoTime();
    apacheTime = end - start;

    System.out.format("Binomial x %d apache/cern %f\n", N, (double) apacheTime / cernTime);
}

From source file:com.analog.lyric.math.DimpleRandom.java

/**
 * Returns sample from beta distribution with specified alpha and beta parameters.
 * @since 0.08/*  www.  j a v  a 2  s. c om*/
 */
public double nextBeta(double alpha, double beta) {
    BetaDistribution randBeta = _randBeta;

    if (randBeta.getAlpha() != alpha || randBeta.getBeta() != beta) {
        randBeta = new BetaDistribution(_randGenerator, alpha, beta);
        _randBeta = randBeta;
    }

    return randBeta.sample();
}