Example usage for org.apache.commons.math3.distribution NormalDistribution NormalDistribution

List of usage examples for org.apache.commons.math3.distribution NormalDistribution NormalDistribution

Introduction

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

Prototype

public NormalDistribution(double mean, double sd, double inverseCumAccuracy)
        throws NotStrictlyPositiveException 

Source Link

Document

Create a normal distribution using the given mean, standard deviation and inverse cumulative distribution accuracy.

Usage

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  ava2s  . com*/
        public Double draw() {
            return dist.sample();
        }
    };
}

From source file:com.winterwell.maths.stats.algorithms.KalmanFilterTest.java

@Test
public void testCannonball() {
    // simulates the flight of a cannonball (only taking gravity and initial thrust into account)

    // number of iterations
    final int iterations = 144;
    // discrete time interval
    final double dt = 0.1d;
    // position measurement noise (meter)
    final double measurementNoise = 30d;
    // the initial velocity of the cannonball
    final double initialVelocity = 100;
    // shooting angle
    final double angle = 45;

    final Cannonball cannonball = new Cannonball(dt, angle, initialVelocity);

    final double speedX = cannonball.getXVelocity();
    final double speedY = cannonball.getYVelocity();

    // A = [ 1, dt, 0,  0 ]  =>  x(n+1) = x(n) + vx(n)
    //     [ 0,  1, 0,  0 ]  => vx(n+1) =        vx(n)
    //     [ 0,  0, 1, dt ]  =>  y(n+1) =              y(n) + vy(n)
    //     [ 0,  0, 0,  1 ]  => vy(n+1) =                     vy(n)
    final Matrix A = new DenseMatrix(
            new double[][] { { 1, dt, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, dt }, { 0, 0, 0, 1 } });

    // The control vector, which adds acceleration to the kinematic equations.
    // 0          =>  x(n+1) =  x(n+1)
    // 0          => vx(n+1) = vx(n+1)
    // -9.81*dt^2 =>  y(n+1) =  y(n+1) - 1/2 * 9.81 * dt^2
    // -9.81*dt   => vy(n+1) = vy(n+1) - 9.81 * dt
    final Vector controlVector = new DenseVector(new double[] { 0, 0, 0.5 * -9.81 * dt * dt, -9.81 * dt });

    //        // The control matrix B only expects y and vy, see control vector
    //        final Matrix B = MatrixUtils.createRealMatrix(new double[][] {
    //                { 0, 0, 0, 0 },
    //                { 0, 0, 0, 0 },
    //                { 0, 0, 1, 0 },
    //                { 0, 0, 0, 1 }
    //        });

    // We only observe the x/y position of the cannonball
    final Matrix H = new DenseMatrix(
            new double[][] { { 1, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 0 } });

    // our guess of the initial state.
    final Vector initialState = new DenseVector(new double[] { 0, speedX, 0, speedY });

    // the initial error covariance matrix, the variance = noise^2
    final double var = measurementNoise * measurementNoise;
    final Matrix initialErrorCovariance = new DenseMatrix(
            new double[][] { { var, 0, 0, 0 }, { 0, 1e-3, 0, 0 }, { 0, 0, var, 0 }, { 0, 0, 0, 1e-3 } });

    // we assume no process noise -> zero matrix
    final Matrix Q = new DenseMatrix(4, 4);

    // the measurement covariance matrix
    final Matrix R = new DenseMatrix(
            new double[][] { { var, 0, 0, 0 }, { 0, 1e-3, 0, 0 }, { 0, 0, var, 0 }, { 0, 0, 0, 1e-3 } });

    //        final ProcessModel pm = new DefaultProcessModel(A, B, Q, initialState, initialErrorCovariance);
    //        final MeasurementModel mm = new DefaultMeasurementModel(H, R);
    final KalmanFilter filter = new KalmanFilter(4, 4);
    filter.setTransitionMatrix(A);// ww  w. j  a v  a 2s.c  om
    filter.setTransitionNoise(Q);
    filter.setEmissionMatrix(H);
    filter.setEmissionNoise(R);

    Gaussian state = new Gaussian(initialState, initialErrorCovariance);

    final RandomGenerator rng = new Well19937c(1000);
    final NormalDistribution dist = new NormalDistribution(rng, 0, measurementNoise);

    for (int i = 0; i < iterations; i++) {
        // get the "real" cannonball position
        double x = cannonball.getX();
        double y = cannonball.getY();

        // apply measurement noise to current cannonball position
        double nx = x + dist.sample();
        double ny = y + dist.sample();

        cannonball.step();

        state = filter.predict(state, controlVector);
        // correct the filter with our measurements
        state = filter.correct(state, new DenseVector(new double[] { nx, 0, ny, 0 }));

        // state estimate shouldn't be larger than the measurement noise
        double diff = FastMath.abs(cannonball.getY() - state.getMean().get(2));
        Assert.assertTrue(Precision.compareTo(diff, measurementNoise, 1e-6) < 0);
    }

    // error covariance of the x/y-position should be already very low (< 3m std dev = 9 variance)

    Assert.assertTrue(Precision.compareTo(state.getCovar().get(0, 0), 9, 1e-6) < 0);

    Assert.assertTrue(Precision.compareTo(state.getCovar().get(2, 2), 9, 1e-6) < 0);
}

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

public double[][] getUncorrelatedGaussianShocks(int sampleSize) {
    NormalDistribution normal = new NormalDistribution(new Well1024a(++seed), 0, 1);

    int numVars = variableNodes.size();
    setupModel(numVars);/*from   ww w.  j ava  2  s  .c  o  m*/

    double[][] shocks = new double[sampleSize][numVars];

    for (int i = 0; i < sampleSize; i++) {
        for (int j = 0; j < numVars; j++) {
            shocks[i][j] = normal.sample() * sqrt(errorVars[j]);
        }
    }

    return shocks;
}

From source file:org.apache.accumulo.core.file.rfile.RolllingStatsTest.java

@Test
public void testNormal() {
    NormalDistribution nd = new NormalDistribution(new Well19937c(42), 200, 20);
    testDistribrution(() -> (int) nd.sample());
}

From source file:org.lightjason.examples.pokemon.simulation.agent.pokemon.CLevel.java

/**
 * generates the random distribution for the values
 *
 * @param p_keys keys//from w  ww  .j  a  v  a2 s. com
 * @param p_value tripel value set (initial value, min, max bounding)
 * @return map
 *
 * @tparam T key type
 */
private static <T> Map<T, ImmutableTriple<AbstractRealDistribution, Number, Number>> initialize(
        final Stream<T> p_keys, final Stream<ImmutableTriple<Number, Number, Number>> p_value) {
    return Collections.unmodifiableMap(StreamUtils
            .zip(p_keys, p_value,
                    (n, v) -> new ImmutablePair<>(n,
                            new ImmutableTriple<AbstractRealDistribution, Number, Number>(
                                    new NormalDistribution(CMath.RANDOMGENERATOR, v.getLeft().doubleValue(),
                                            Math.sqrt(0.5 * (v.getRight().doubleValue()
                                                    - v.getMiddle().doubleValue()))),
                                    v.getMiddle(), v.getRight())))
            .collect(Collectors.toMap(ImmutablePair::getLeft, ImmutablePair::getRight)));
}