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

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

Introduction

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

Prototype

double sample();

Source Link

Document

Generate a random value sampled from this distribution.

Usage

From source file:io.coala.math3.Math3ProbabilityDistribution.java

@SafeVarargs
public static <S> Math3ProbabilityDistribution<Double> of(final RealDistribution dist, // final PseudoRandom stream,
        final S... args) {
    Objects.requireNonNull(dist);
    final Math3ProbabilityDistribution<Double> result = new Math3ProbabilityDistribution<Double>() {
        @Override/*from ww  w . j  a  v a 2s  .co  m*/
        public Double draw() {
            return Double.valueOf(dist.sample());
        }
    };
    //      result.stream = stream;
    return result;
}

From source file:com.vsthost.rnd.jdeoptim.utils.Utils.java

/**
 * Creates a random population./*from w w  w . j a v  a 2  s.  co  m*/
 *
 * @param size The size of the population to be created.
 * @param dimension The dimension of the candidate (length of each candidate) to be created.
 * @param lower The lower boundary.
 * @param upper The upper boundary.
 * @param distribution The distribution to be sampled from.
 * @return A random population.
 */
public static double[][] randomPopulation(int size, int dimension, double[] lower, double[] upper,
        RealDistribution distribution) {
    // Create the population matrix:
    final double[][] population = new double[size][dimension];

    // Iterate over population and populate each candidate:
    for (int c = 0; c < size; c++) {
        // Iterate over dimensions and populate candidate elements:
        for (int d = 0; d < dimension; d++) {
            // Get the minimum possible value:
            final double min = lower[d];

            // Get the maximum possible value:
            final double max = upper[d];

            // Create a random value within the range:
            // TODO: For better performance, memoize (max - min).
            population[c][d] = min + ((max - min) * distribution.sample());
        }
    }

    // Done, return the population:
    return population;
}

From source file:cz.cuni.mff.d3s.spl.interpretation.DistributionLearningInterpretation.java

private void showDistribution(RealDistribution distr) {
    assert debug != null;
    for (int i = 0; i < 10; i++) {
        debug.printf("  %.3f", distr.sample());
    }/*from w  ww  . j av  a  2s. c  om*/
    debug.println();
}

From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java

/**
 * Adds random jitter to {@code data} using deviates sampled from {@code dist}.
 * <p>//from   w  w w  .  j  a v a  2 s . co m
 * Note that jitter is applied in-place - i.e., the array
 * values are overwritten with the result of applying jitter.</p>
 *
 * @param data input/output data array - entries overwritten by the method
 * @param dist probability distribution to sample for jitter values
 * @throws NullPointerException if either of the parameters is null
 */
private static void jitter(double[] data, RealDistribution dist) {
    for (int i = 0; i < data.length; i++) {
        data[i] += dist.sample();
    }
}

From source file:mtsar.processors.answer.KOSAggregator.java

private Map<Integer, Double> converge(Table<Integer, Integer, Short> graph, int kMax) {
    final RealDistribution distribution = new NormalDistribution(1, 1);

    Table<Integer, Integer, Double> ys = HashBasedTable.create(graph.rowKeySet().size(),
            graph.columnKeySet().size());

    for (final Table.Cell<Integer, Integer, Short> cell : graph.cellSet()) {
        ys.put(cell.getRowKey(), cell.getColumnKey(), distribution.sample());
    }/*from   w ww.j  av  a2  s .c o  m*/

    for (int k = 1; k <= kMax; k++) {
        final Table<Integer, Integer, Double> xs = tasksUpdate(graph, ys);
        if (k < kMax)
            ys = workersUpdate(graph, xs);
    }

    final Map<Integer, Double> estimations = new HashMap<>();

    for (final Integer taskId : graph.rowKeySet()) {
        double sumProduct = 0.0;

        final Map<Integer, Double> workers = ys.row(taskId);
        for (final Map.Entry<Integer, Double> worker : workers.entrySet()) {
            sumProduct += graph.get(taskId, worker.getKey()) * worker.getValue();
        }

        estimations.put(taskId, sumProduct);
    }

    return estimations;
}

From source file:io.coala.random.impl.RandomDistributionFactoryImpl.java

@Override
public RandomNumberDistribution<Double> getExponential(final RandomNumberStream rng, final Number mean) {
    final RealDistribution dist = new ExponentialDistribution(
            RandomNumberStream.Util.asCommonsRandomGenerator(rng), mean.doubleValue());
    return new RandomNumberDistribution<Double>() {
        @Override//from   w  w  w. ja v  a  2s.  c om
        public Double draw() {
            return dist.sample();
        }
    };
}

From source file:io.coala.random.impl.RandomDistributionFactoryImpl.java

@Override
public RandomNumberDistribution<Double> getT(final RandomNumberStream rng, final Number degreesOfFreedom) {
    final RealDistribution dist = new TDistribution(RandomNumberStream.Util.asCommonsRandomGenerator(rng),
            degreesOfFreedom.doubleValue());
    return new RandomNumberDistribution<Double>() {
        @Override/* ww w .  j  av  a2 s.  co m*/
        public Double draw() {
            return dist.sample();
        }
    };
}

From source file:io.coala.random.impl.RandomDistributionFactoryImpl.java

@Override
public RandomNumberDistribution<Double> getLevy(final RandomNumberStream rng, final Number mu, final Number c) {
    final RealDistribution dist = new LevyDistribution(RandomNumberStream.Util.asCommonsRandomGenerator(rng),
            mu.doubleValue(), c.doubleValue());
    return new RandomNumberDistribution<Double>() {
        @Override/*from  w  w w  .jav a 2 s . c o m*/
        public Double draw() {
            return dist.sample();
        }
    };
}

From source file:io.coala.random.impl.RandomDistributionFactoryImpl.java

@Override
public RandomNumberDistribution<Double> getChiSquared(final RandomNumberStream rng,
        final Number degreesOfFreedom) {
    final RealDistribution dist = new ChiSquaredDistribution(
            RandomNumberStream.Util.asCommonsRandomGenerator(rng), degreesOfFreedom.doubleValue());
    return new RandomNumberDistribution<Double>() {
        @Override/*ww w . j a v  a 2  s .  c  om*/
        public Double draw() {
            return dist.sample();
        }
    };
}

From source file:io.coala.random.impl.RandomDistributionFactoryImpl.java

@Override
public RandomNumberDistribution<Double> getNormal(final RandomNumberStream rng, final Number mean,
        final Number sd) {
    final RealDistribution dist = new NormalDistribution(RandomNumberStream.Util.asCommonsRandomGenerator(rng),
            mean.doubleValue(), sd.doubleValue());
    return new RandomNumberDistribution<Double>() {
        @Override/*from  w  ww . j  ava 2 s  . co m*/
        public Double draw() {
            return dist.sample();
        }
    };
}