Example usage for org.apache.commons.math3.random Well44497b Well44497b

List of usage examples for org.apache.commons.math3.random Well44497b Well44497b

Introduction

In this page you can find the example usage for org.apache.commons.math3.random Well44497b Well44497b.

Prototype

public Well44497b(long seed) 

Source Link

Document

Creates a new random number generator using a single long seed.

Usage

From source file:broadwick.statistics.distributions.MultivariateNormalDistribution.java

/**
 * Create a normal distribution with a given mean and standard deviation.
 * @param means  the [mathematical] vector of means.
 * @param covariances the covariance matrix.
 *//*from w  w w  .  j  a  v a 2s.  co m*/
public MultivariateNormalDistribution(final double[] means, final double[][] covariances) {
    this.mnd = new org.apache.commons.math3.distribution.MultivariateNormalDistribution(
            new Well44497b(System.currentTimeMillis() * Thread.currentThread().getId()), means, covariances);

    //        Find any real matrix A such that AAT = . When  is positive-definite, the Cholesky decomposition is typically used,                                                                                                      
    //        Let z = (z1, , zN)T be a vector whose components are N independent standard normal variates .
    //        Let x be  + Az. This has the desired distribution due to the affine transformation property.
}

From source file:com.opengamma.strata.math.impl.statistics.leastsquare.NonLinearLeastSquareWithPenaltyTest.java

public void linearTest() {
    boolean print = false;
    if (print) {/*w w  w  .  jav  a  2 s  . c o m*/
        System.out.println("NonLinearLeastSquareWithPenaltyTest.linearTest");
    }
    int nWeights = 20;
    int diffOrder = 2;
    double lambda = 100.0;
    DoubleMatrix penalty = (DoubleMatrix) MA.scale(getPenaltyMatrix(nWeights, diffOrder), lambda);
    int[] onIndex = new int[] { 1, 4, 11, 12, 15, 17 };
    double[] obs = new double[] { 0, 1.0, 1.0, 1.0, 0.0, 0.0 };
    int n = onIndex.length;

    Function<DoubleArray, DoubleArray> func = new Function<DoubleArray, DoubleArray>() {

        @Override
        public DoubleArray apply(DoubleArray x) {
            return DoubleArray.of(n, i -> x.get(onIndex[i]));
        }
    };

    Function<DoubleArray, DoubleMatrix> jac = new Function<DoubleArray, DoubleMatrix>() {

        @Override
        public DoubleMatrix apply(DoubleArray x) {
            return DoubleMatrix.of(n, nWeights, (i, j) -> j == onIndex[i] ? 1d : 0d);
        }
    };

    Well44497b random = new Well44497b(0L);
    DoubleArray start = DoubleArray.of(nWeights, i -> random.nextDouble());

    LeastSquareWithPenaltyResults lsRes = NLLSWP.solve(DoubleArray.copyOf(obs), DoubleArray.filled(n, 0.01),
            func, jac, start, penalty);
    if (print) {
        System.out.println("chi2: " + lsRes.getChiSq());
        System.out.println(lsRes.getFitParameters());
    }
    for (int i = 0; i < n; i++) {
        assertEquals(obs[i], lsRes.getFitParameters().get(onIndex[i]), 0.01);
    }
    double expPen = 20.87912357454752;
    assertEquals(expPen, lsRes.getPenalty(), 1e-9);
}

From source file:broadwick.rng.RNG.java

/**
 * Create a random number generator from a given generator using the current time as a seed.
 * @param gen the random number generator to use.
 *///from  w  ww . j a v  a 2  s . com
public RNG(final Generator gen) {
    if (gen.equals(Generator.MERSENNE)) {
        generator = new RandomDataGenerator(
                new MersenneTwister(System.currentTimeMillis() * Thread.currentThread().getId()));
        name = "Mersenne Twister";
    } else if (gen.equals(Generator.Well19937c)) {
        generator = new RandomDataGenerator(
                new Well19937c(System.currentTimeMillis() * Thread.currentThread().getId()));
        name = "Well19937c";
    } else if (gen.equals(Generator.Well44497b)) {
        generator = new RandomDataGenerator(
                new Well44497b(System.currentTimeMillis() * Thread.currentThread().getId()));
        name = "Well44497b";
    } else {
        // By default, the implementation provided in RandomDataImpl uses
        // the JDK-provided PRNG. Like most other PRNGs, the JDK generator
        // generates sequences of random numbers based on an initial
        // "seed value".
        generator = new RandomDataGenerator();
    }
}

From source file:com.github.joulupunikki.math.random.XorShift1024Star.java

public static void speedTest() {
    RandomGenerator rng = null;/*  www  . j av a  2  s  . c  om*/
    double[][] r = new double[2][];
    //        rng = new Well19937c(1);
    rng = new Well44497b(1);
    r[0] = speedTest(rng);
    rng = new XorShift1024Star(1);
    r[1] = speedTest(rng);
    for (int i = 0; i < r[0].length; i++) {
        System.out.println(r[0][i] / r[1][i]);
    }
}

From source file:edu.cmu.tetrad.util.RandomUtil.java

/**
 * Sets the seed to the given value./*  w ww  . j  a  v a 2s.c  o m*/
 *
 * @param seed A long value. Once this seed is set, the behavior of the random number generator is deterministic, so
 *             setting the seed can be used to repeat previous behavior.
 */
public void setSeed(long seed) {

    // Apache offers several random number generators; picking one that works pretty well.
    randomGenerator = new Well44497b(seed);
    normal = new NormalDistribution(randomGenerator, 0, 1);
    this.seed = seed;
}

From source file:gdsc.smlm.ij.plugins.EMGainAnalysis.java

/**
 * Random sample from the cumulative probability distribution function that is used during fitting
 * //from  w  w  w  .  ja  v a 2  s.  c o m
 * @return The histogram
 */
private int[] simulateFromPDF() {
    final double[] g = pdf(0, _photons, _gain, _noise, (int) _bias);

    // Get cumulative probability
    double sum = 0;
    for (int i = 0; i < g.length; i++) {
        final double p = g[i];
        g[i] += sum;
        sum += p;
    }
    for (int i = 0; i < g.length; i++)
        g[i] /= sum;
    g[g.length - 1] = 1; // Ensure value of 1 at the end

    // Randomly sample
    RandomGenerator random = new Well44497b(System.currentTimeMillis() + System.identityHashCode(this));
    int[] h = new int[g.length];
    final int steps = simulationSize;
    for (int n = 0; n < steps; n++) {
        if (n % 64 == 0)
            IJ.showProgress(n, steps);
        final double p = random.nextDouble();
        for (int i = 0; i < g.length; i++)
            if (p <= g[i]) {
                h[i]++;
                break;
            }
    }
    return h;
}

From source file:gdsc.smlm.ij.plugins.EMGainAnalysis.java

/**
 * Randomly generate a histogram from poisson-gamma-gaussian samples
 * /*from  www.j av a 2  s  . com*/
 * @return The histogram
 */
private int[] simulateFromPoissonGammaGaussian() {
    // Randomly sample
    RandomGenerator random = new Well44497b(System.currentTimeMillis() + System.identityHashCode(this));

    PoissonDistribution poisson = new PoissonDistribution(random, _photons, PoissonDistribution.DEFAULT_EPSILON,
            PoissonDistribution.DEFAULT_MAX_ITERATIONS);

    CustomGammaDistribution gamma = new CustomGammaDistribution(random, _photons, _gain,
            GammaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);

    final int steps = simulationSize;
    int[] sample = new int[steps];
    for (int n = 0; n < steps; n++) {
        if (n % 64 == 0)
            IJ.showProgress(n, steps);

        // Poisson
        double d = poisson.sample();

        // Gamma
        if (d > 0) {
            gamma.setShapeUnsafe(d);
            d = gamma.sample();
        }

        // Gaussian
        d += _noise * random.nextGaussian();

        // Convert the sample to a count 
        sample[n] = (int) Math.round(d + _bias);
    }

    int max = Maths.max(sample);
    int[] h = new int[max + 1];
    for (int s : sample)
        h[s]++;
    return h;
}

From source file:edu.cmu.tetrad.sem.LargeSemSimulator.java

public BoxDataSet simulateDataAcyclicConcurrent(int sampleSize) {
    int numVars = variableNodes.size();
    setupModel(numVars);// w  ww .  ja  v a  2s.  co m

    System.out.println("Tier ordering");

    final int[][] _parents = parents;
    final double[][] _coefs = coefs;

    //        final double[][] _data = new double[sampleSize][numVars];
    final double[][] _data = new double[numVars][sampleSize];

    System.out.println("Starting simulation task");

    // This random number generator is not thread safe, so we make a new one each time.
    RandomGenerator apacheGen = new Well19937a(new Date().getTime());
    final RandomDataGenerator generator = new RandomDataGenerator(new SynchronizedRandomGenerator(apacheGen));

    //Do the simulation.
    class SimulationTask extends RecursiveTask<Boolean> {
        private int chunk;
        private int from;
        private int to;

        public SimulationTask(int chunk, int from, int to) {
            this.chunk = chunk;
            this.from = from;
            this.to = to;
        }

        @Override
        protected Boolean compute() {
            RandomGenerator apacheGen = new Well44497b(generator.nextLong(0, Long.MAX_VALUE));
            RandomDataGenerator generatorLocal = new RandomDataGenerator(apacheGen);

            if (to - from <= chunk) {
                for (int row = from; row < to; row++) {
                    if (row % 100 == 0)
                        out.println("Row " + row);

                    for (int i = 0; i < tierIndices.length; i++) {
                        int col = tierIndices[i];

                        double value = generatorLocal.nextGaussian(0, sqrt(errorVars[col]));

                        for (int j = 0; j < _parents[col].length; j++) {
                            int parent = _parents[col][j];
                            final double coef = _coefs[col][j];
                            final double v = _data[parent][row];
                            value += v * coef;

                            if (Double.isNaN(value)) {
                                throw new IllegalArgumentException();
                            }
                        }

                        value += means[col];

                        _data[col][row] = value;
                    }
                }

                return true;
            } else {
                List<SimulationTask> simulationTasks = new ArrayList<SimulationTask>();

                int mid = (to - from) / 2;

                simulationTasks.add(new SimulationTask(chunk, from, from + mid));
                simulationTasks.add(new SimulationTask(chunk, from + mid, to));

                invokeAll(simulationTasks);

                return true;
            }
        }
    }

    int chunk = 25;

    pool.invoke(new SimulationTask(chunk, 0, sampleSize));

    return new BoxDataSet(new VerticalDoubleDataBox(_data), variableNodes);
    //        return ColtDataSet.makeContinuousData(variableNodes, _data);
}

From source file:gdsc.smlm.ij.plugins.CreateData.java

/**
 * Get a random generator. The generators used in the simulation can be adjusted by changing this method.
 * /*from www.j a  va2 s.c  om*/
 * @param seedAddition
 *            Added to the seed generated from the system time
 * @return A random generator
 */
private RandomGenerator createRandomGenerator(int seedAddition) {
    return new Well44497b(System.currentTimeMillis() + System.identityHashCode(this) + seedAddition);
    //return new Well19937c(System.currentTimeMillis() + System.identityHashCode(this));
}

From source file:org.uma.jmetal.util.pseudorandom.impl.Well44497bGenerator.java

/** Constructor */
public Well44497bGenerator() {
    seed = System.currentTimeMillis();
    rnd = new Well44497b(seed);
}