Example usage for org.apache.commons.math.distribution NormalDistributionImpl NormalDistributionImpl

List of usage examples for org.apache.commons.math.distribution NormalDistributionImpl NormalDistributionImpl

Introduction

In this page you can find the example usage for org.apache.commons.math.distribution NormalDistributionImpl NormalDistributionImpl.

Prototype

public NormalDistributionImpl() 

Source Link

Document

Creates normal distribution with the mean equal to zero and standard deviation equal to one.

Usage

From source file:datafu.pig.stats.WilsonBinConf.java

/**
 * @param x The number of positive (success) outcomes
 * @param n The number of observations/*from w  w w. j  a v  a  2 s  .co m*/
 * @return The (lower,upper) confidence interval
 */
public Tuple binconf(Long x, Long n) throws IOException {
    NormalDistribution normalDist = new NormalDistributionImpl();

    if (x == null || n == null)
        return null;
    if (x < 0 || n < 0)
        throw new IllegalArgumentException("non-negative values expected");
    if (x > n)
        throw new IllegalArgumentException("invariant violation: number of successes > number of obs");
    if (n == 0)
        return tupleFactory.newTuple(ImmutableList.of(Double.valueOf(0), Double.valueOf(0)));

    try {
        double zcrit = -1.0 * normalDist.inverseCumulativeProbability(alpha / 2);
        double z2 = zcrit * zcrit;
        double p = x / (double) n;

        double a = p + z2 / 2 / n;
        double b = zcrit * Math.sqrt((p * (1 - p) + z2 / 4 / n) / n);
        double c = (1 + z2 / n);

        double lower = (a - b) / c;
        double upper = (a + b) / c;

        // Add corrections for when x is very close to n.  This improves the estimates.
        // For more info on wilson binomial confidence interval, see paper:
        // L.D. Brown, T.T. Cai and A. DasGupta, Interval estimation for a binomial proportion (with discussion), 
        //   _Statistical Science,_*16*:101-133, 2001. 
        // http://www-stat.wharton.upenn.edu/~tcai/paper/Binomial-StatSci.pdf

        if (x == 1)
            lower = -Math.log(1 - alpha) / n;
        if (x == (n - 1))
            upper = 1 + Math.log(1 - alpha) / n;

        return tupleFactory.newTuple(ImmutableList.of(lower, upper));
    } catch (MathException e) {
        throw new IOException("math error", e);
    }
}

From source file:com.dagobert_engine.statistics.service.MtGoxStatisticsService.java

private static double calcPropability(double avg, double dev, PropabilityType type, double value) {
    NormalDistributionImpl normDistr = new NormalDistributionImpl();

    if (dev == 0.0)
        return 0.0;

    double stdValue = (value - avg) / dev;

    try {/*w  ww  .java 2s.  c o m*/
        switch (type) {
        case GREATER_THAN:
            return 1 - normDistr.cumulativeProbability(stdValue);
        case LESS_THAN:
            return normDistr.cumulativeProbability(stdValue);
        }
        return 0.0;
    } catch (MathException exc) {
        throw new RuntimeException(exc);

    }
}

From source file:org.apache.mahout.knn.generate.NormalTest.java

@Test
public void testSample() throws MathException {
    double[] data = new double[10001];
    Sampler<Double> sampler = new Normal();
    for (int i = 0; i < 10001; i++) {
        data[i] = sampler.sample();/*from  w  w w . ja v  a  2  s  .  c o  m*/
    }
    Arrays.sort(data);

    NormalDistribution reference = new NormalDistributionImpl();

    assertEquals("Median", reference.inverseCumulativeProbability(0.5), data[5000], 0.04);
}

From source file:org.bdval.MaqciiHelper.java

private double stouffer(final double[] pval) {
    double rval = 1.0;
    final NormalDistribution normd = new NormalDistributionImpl();
    normd.setMean(0);//w w  w  .  jav  a2 s .c o m
    normd.setStandardDeviation(1);
    final int size = pval.length;
    final double[] z = new double[size];
    double zCombine = 0;

    for (int i = 0; i < size; i++) {
        try {
            z[i] = normd.inverseCumulativeProbability(pval[i]);
        } catch (MathException e) {
            LOG.warn("Error calculating probability", e);
        }
        zCombine += z[i];
    }
    zCombine /= Math.sqrt(size);

    try {
        rval = normd.cumulativeProbability(zCombine);
    } catch (MathException e) {
    }

    return rval;
}