Example usage for org.apache.commons.rng.sampling.distribution ContinuousSampler sample

List of usage examples for org.apache.commons.rng.sampling.distribution ContinuousSampler sample

Introduction

In this page you can find the example usage for org.apache.commons.rng.sampling.distribution ContinuousSampler sample.

Prototype

double sample();

Source Link

Document

Creates a sample.

Usage

From source file:org.apache.commons.rng.examples.jmh.distribution.SamplersPerformance.java

/**
 * Exercises a continuous sampler./*from w  w w  . ja  va 2s.  c o  m*/
 *
 * @param sampler Sampler.
 * @param bh Data sink.
 */
private void runSample(ContinuousSampler sampler, Blackhole bh) {
    for (int i = 0; i < NUM_SAMPLES; i++) {
        bh.consume(sampler.sample());
    }
}

From source file:org.apache.commons.rng.examples.sampling.ProbabilityDensityApproximation.java

/**
 * @param sampler Sampler./*from   www  .  j ava 2 s  .  c  o  m*/
 * @param min Right abscissa of the first bin: every sample smaller
 * than that value will increment an additional bin (of infinite width)
 * placed before the first "equal-width" bin.
 * @param Left abscissa of the last bin: every sample larger than or
 * equal to that value will increment an additional bin (of infinite
 * width) placed after the last "equal-width" bin.
 * @param output Filename.
 */
private void createDensity(ContinuousSampler sampler, double min, double max, String outputFile)
        throws IOException {
    final double binSize = (max - min) / numBins;
    final long[] histogram = new long[numBins];

    long n = 0;
    long belowMin = 0;
    long aboveMax = 0;
    while (++n < numSamples) {
        final double r = sampler.sample();

        if (r < min) {
            ++belowMin;
            continue;
        }

        if (r >= max) {
            ++aboveMax;
            continue;
        }

        final int binIndex = (int) ((r - min) / binSize);
        ++histogram[binIndex];
    }

    final double binHalfSize = 0.5 * binSize;
    final double norm = 1 / (binSize * numSamples);

    final PrintWriter out = new PrintWriter(outputFile);
    out.println("# Sampler: " + sampler);
    out.println("# Number of bins: " + numBins);
    out.println("# Min: " + min + " (fraction of samples below: " + (belowMin / (double) numSamples) + ")");
    out.println("# Max: " + max + " (fraction of samples above: " + (aboveMax / (double) numSamples) + ")");
    out.println("# Bin width: " + binSize);
    out.println("# Histogram normalization factor: " + norm);
    out.println("#");
    out.println("# " + (min - binHalfSize) + " " + (belowMin * norm));
    for (int i = 0; i < numBins; i++) {
        out.println((min + (i + 1) * binSize - binHalfSize) + " " + (histogram[i] * norm));
    }
    out.println("# " + (max + binHalfSize) + " " + (aboveMax * norm));
    out.close();
}

From source file:org.apache.commons.rng.examples.sampling.UniformSamplingVisualCheck.java

/**
 * Program entry point./*w w  w. j  a v  a  2s. c o  m*/
 *
 * @param args Unused.
 */
public static void main(String[] args) {
    final float lo = 0.1f;
    final int bands = 2;
    float hi = lo;
    for (int i = 0; i < bands; i++) {
        hi = Math.nextUp(hi);
    }
    System.out.printf("# lo=%.10e hi=%.10e", lo, hi);
    System.out.println();

    final UniformSamplingVisualCheck app = new UniformSamplingVisualCheck();

    while (true) {
        System.out.printf("%.16e\t", app.rng.nextDouble());

        for (ContinuousSampler s : app.samplers) {
            while (true) {
                final double r = s.sample();
                if (r < lo || r > hi) {
                    // Discard numbers outside the tiny region.
                    continue;
                }

                System.out.printf("%.16e ", r);
                break;
            }
        }

        System.out.println();
    }
}