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

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

Introduction

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

Prototype

@Override
public double sample() 

Source Link

Document

This implementation uses the following algorithms:

For 0 < shape < 1:
Ahrens, J.

Usage

From source file:math.test.TestDimpleRandom.java

/**
 * Measures speed of Apache vs Colt generators
 *//*  w  ww  .  j a  va 2 s  . c  om*/
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:hivemall.utils.lang.ArrayUtils.java

@Nonnull
public static float[] newRandomFloatArray(@Nonnegative final int size, @Nonnull final GammaDistribution gd) {
    final float[] ret = new float[size];
    for (int i = 0; i < size; i++) {
        ret[i] = (float) gd.sample();
    }//from   w w  w.j  a  va 2s  .  c o  m
    return ret;
}

From source file:msi.gaml.operators.Stats.java

@operator(value = "gamma_rnd", can_be_const = false, type = IType.LIST, category = {
        IOperatorCategory.STATISTICAL }, concept = { IConcept.STATISTIC, IConcept.CLUSTERING })
@doc(value = "returns a random value from a gamma distribution with specified values of the shape and scale parameters", examples = {
        @example(value = "gamma_rnd(10.0,5.0)", isExecutable = false) })
public static Double OpGammaDist(final IScope scope, final Double shape, final Double scale)
        throws GamaRuntimeException {
    final GammaDistribution dist = new GammaDistribution(scope.getRandom().getGenerator(), shape, scale,
            GammaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    return dist.sample();
}

From source file:org.rhwlab.variationalbayesian.GaussianMixture.java

public double[] dirichlet(double alpha, int K) {
    GammaDistribution dist = new GammaDistribution(alpha, 1.0);
    double[] ret = new double[K];
    double total = 0.0;
    for (int i = 0; i < K; ++i) {
        ret[i] = dist.sample();
        total = total + ret[i];//from  w  ww.  j  ava 2  s  .  com
    }
    for (int i = 0; i < K; ++i) {
        ret[i] = ret[i] / total;
    }
    return ret;
}