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

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

Introduction

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

Prototype

public double cumulativeProbability(double x) throws MathException 

Source Link

Document

For this distribution, X, this method returns P(X < x).

Usage

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.  j av a2  s . 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:de.tud.kom.p2psim.impl.util.stat.distributions.LimitedNormalDistribution.java

/**
 * Returns a random value that is distributed as a Normal Distribution with
 * an upper and lower limit.//from  w  ww. j av a 2 s  .  c  o  m
 * 
 * @param _mu
 *            average
 * @param _sigma
 *            standard deviation
 * @param _min
 *            lower limit, set to "null", if no limit
 * @param _max
 *            upper limit, set to "null", if no limit
 * @return as double
 */
public static double returnValue(double _mu, double _sigma, Double _min, Double _max) {
    int llimitType;
    double lmax;
    double lmin;
    double lpmax = 1;
    double lpmin = 0;
    double lpfactor;

    NormalDistributionImpl llimitedNormal = new NormalDistributionImpl(_mu, _sigma);
    if (_min == null) {
        if (_max == null) {
            llimitType = LIMIT_NORMAL_DIST_NONE;
        } else {
            // only max is limted
            llimitType = LIMIT_NORMAL_DIST_MAX;
            lmax = _max.doubleValue();
            try {
                lpmax = llimitedNormal.cumulativeProbability(lmax);
            } catch (MathException e) {
                e.printStackTrace();
            }
        }
    } else {
        if (_max == null) {
            // only min is limited.
            llimitType = LIMIT_NORMAL_DIST_MIN;
            lmin = _min.doubleValue();
            try {
                lpmin = llimitedNormal.cumulativeProbability(lmin);
            } catch (MathException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            // both sides limited.
            llimitType = LIMIT_NORMAL_DIST_BOTH;

            // make sure min is really smaller than max.
            if (_max.doubleValue() > _min.doubleValue()) {
                lmin = _min.doubleValue();
                lmax = _max.doubleValue();
            } else {
                lmax = _min.doubleValue();
                lmin = _max.doubleValue();
            }

            // get min and max probabilites that are possible
            try {
                lpmin = llimitedNormal.cumulativeProbability(lmin);
                lpmax = llimitedNormal.cumulativeProbability(lmax);

                lpfactor = lpmax - lpmin;

            } catch (MathException e) {
                e.printStackTrace();
            }
        }
    }
    lpfactor = lpmax - lpmin;

    double lrandom = lpmin + Simulator.getRandom().nextDouble() * lpfactor;
    double lresult;

    try {
        lresult = llimitedNormal.inverseCumulativeProbability(lrandom);
    } catch (MathException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        lresult = 0;
    }

    return lresult;

}

From source file:geogebra.common.kernel.statistics.AlgoZProportionTest.java

@Override
public final void compute() {

    String testType;//  w w w. ja  v  a 2 s .c om
    if (tail.getTextString().equals("<")) {
        testType = "left";
    } else if (tail.getTextString().equals(">")) {
        testType = "right";
    } else if (StringUtil.isNotEqual(tail.getTextString())) {
        testType = "two";
    } else {
        result.setUndefined();
        return;
    }

    double n1 = n.getDouble();
    double hyp = hypPropertion.getDouble();
    double phat = proportion.getDouble();

    se = Math.sqrt(hyp * (1 - hyp) / n1);
    double testStatistic = (phat - hyp) / se;

    NormalDistributionImpl normalDist = new NormalDistributionImpl(0, 1);
    double P = 0;
    try {
        P = normalDist.cumulativeProbability(testStatistic);
    } catch (Exception e) {
        result.setUndefined();
        return;
    }

    if ("right".equals(testType)) {
        P = 1 - P;
    } else if ("two".equals(testType)) {
        if (testStatistic < 0) {
            P = 2 * P;
        } else {
            P = 2 * (1 - P);
        }
    }

    // put these results into the output list
    result.clear();
    result.add(new GeoNumeric(cons, P));
    result.add(new GeoNumeric(cons, testStatistic));

}

From source file:geogebra.common.kernel.statistics.AlgoZProportion2Test.java

@Override
public final void compute() {

    String testType;//www  .ja  v a  2 s  . c  om
    if (tail.getTextString().equals("<")) {
        testType = "left";
    } else if (tail.getTextString().equals(">")) {
        testType = "right";
    } else if (StringUtil.isNotEqual(tail.getTextString())) {
        testType = "two";
    } else {
        result.setUndefined();
        return;
    }

    double n1 = n.getDouble();
    double phat1 = proportion.getDouble();
    double n2 = n_2.getDouble();
    double phat2 = proportion2.getDouble();

    double x1 = phat1 * n1;
    double x2 = phat2 * n2;
    double phatTotal = (x1 + x2) / (n1 + n2);
    se = Math.sqrt(phatTotal * (1 - phatTotal) * (1 / n1 + 1 / n2));
    double testStatistic = (phat1 - phat2) / se;

    NormalDistributionImpl normalDist = new NormalDistributionImpl(0, 1);
    double P = 0;
    try {
        P = normalDist.cumulativeProbability(testStatistic);
    } catch (Exception e) {
        result.setUndefined();
        return;
    }

    if ("right".equals(testType)) {
        P = 1 - P;
    } else if ("two".equals(testType)) {
        if (testStatistic < 0) {
            P = 2 * P;
        } else {
            P = 2 * (1 - P);
        }
    }

    // put these results into the output list
    result.clear();
    result.add(new GeoNumeric(cons, P));
    result.add(new GeoNumeric(cons, testStatistic));

}

From source file:geogebra.common.kernel.statistics.AlgoZMeanTest.java

@Override
public final void compute() {

    String testType;/* w  ww.  j  av  a 2s  .com*/
    if (tail.getTextString().equals("<")) {
        testType = "left";
    } else if (tail.getTextString().equals(">")) {
        testType = "right";
    } else if (StringUtil.isNotEqual(tail.getTextString())) {
        testType = "two";
    } else {
        result.setUndefined();
        return;
    }

    double mean1;
    double n1;

    if (list == null) {
        mean1 = mean.getDouble();
        n1 = n.getDouble();
    } else {
        mean1 = list.mean();
        n1 = list.size();
    }

    double hyp = hypMean.getDouble();
    double sd1 = sd.getDouble();

    double se = sd1 / Math.sqrt(n1);
    double testStatistic = (mean1 - hyp) / se;

    NormalDistributionImpl normalDist = new NormalDistributionImpl(0, 1);
    double P = 0;
    try {
        P = normalDist.cumulativeProbability(testStatistic);
    } catch (Exception e) {
        result.setUndefined();
        return;
    }

    if ("right".equals(testType)) {
        P = 1 - P;
    } else if ("two".equals(testType)) {
        if (testStatistic < 0) {
            P = 2 * P;
        } else {
            P = 2 * (1 - P);
        }
    }

    // put these results into the output list
    result.clear();
    result.add(new GeoNumeric(cons, P));
    result.add(new GeoNumeric(cons, testStatistic));

}

From source file:geogebra.common.kernel.statistics.AlgoZMean2Test.java

@Override
public final void compute() {

    String testType;/*  w  ww.  java 2s  .  c  o  m*/
    if (tail.getTextString().equals("<")) {
        testType = "left";
    } else if (tail.getTextString().equals(">")) {
        testType = "right";
    } else if (StringUtil.isNotEqual(tail.getTextString())) {
        testType = "two";
    } else {
        result.setUndefined();
        return;
    }

    double mean1, mean2;
    double n1, n2;

    double sd1 = sd.getDouble();
    double sd2 = sd_2.getDouble();

    if (list == null) {
        mean1 = mean.getDouble();
        mean2 = mean_2.getDouble();
        n1 = n.getDouble();
        n2 = n_2.getDouble();
    } else {
        mean1 = list.mean();
        n1 = list.size();
        mean2 = list2.mean();
        n2 = list2.size();
    }

    se = Math.sqrt(sd1 * sd1 / n1 + sd2 * sd2 / n2);
    double testStatistic = (mean1 - mean2) / se;

    NormalDistributionImpl normalDist = new NormalDistributionImpl(0, 1);
    double P = 0;
    try {
        P = normalDist.cumulativeProbability(testStatistic);
    } catch (Exception e) {
        result.setUndefined();
        return;
    }

    if ("right".equals(testType)) {
        P = 1 - P;
    } else if ("two".equals(testType)) {
        if (testStatistic < 0) {
            P = 2 * P;
        } else {
            P = 2 * (1 - P);
        }
    }

    // put these results into the output list
    result.clear();
    result.add(new GeoNumeric(cons, P));
    result.add(new GeoNumeric(cons, testStatistic));

}

From source file:cn.edu.tsinghua.gui.HistogramDemo.java

/**
 * Adjust to a normal distribution CDF, mean=128, SD=64.
 * @return the stretched image./*from  ww  w.  j  a v a  2s  . c  om*/
 */
public PlanarImage normalize() {
    int numBands = source.getNumBands();
    int binCount = 256;
    float[][] CDFnorm = new float[numBands][binCount];
    NormalDistributionImpl norm = new NormalDistributionImpl(128.0, 64.0);
    for (int b = 0; b < numBands; b++) {
        for (int i = 0; i < binCount - 1; i++) {
            try {
                CDFnorm[b][i] = (float) norm.cumulativeProbability(i);
            } catch (MathException e) {
                e.printStackTrace();
            }
        }
        // the cumulative probability must equal one
        CDFnorm[b][binCount - 1] = 1;
    }

    int[] bins = { 256 };
    double[] low = { 0.0D };
    double[] high = { 256.0D };

    ParameterBlock pb = new ParameterBlock();
    pb.addSource(source);
    pb.add(null);
    pb.add(1);
    pb.add(1);
    pb.add(bins);
    pb.add(low);
    pb.add(high);

    RenderedOp fmt = JAI.create("histogram", pb, null);
    return JAI.create("matchcdf", fmt, CDFnorm);
}

From source file:org.peerfact.impl.util.stats.distributions.LimitedNormalDistribution.java

/**
 * Returns a random value that is distributed as a Normal Distribution with
 * an upper and lower limit.//from www  .  ja v  a 2 s .  com
 * 
 * @param _mu
 *            average
 * @param _sigma
 *            standard deviation
 * @param _min
 *            lower limit, set to "null", if no limit
 * @param _max
 *            upper limit, set to "null", if no limit
 * @return as double
 */
public static double returnValue(double _mu, double _sigma, Double _min, Double _max) {
    double lmax;
    double lmin;
    double lpmax = 1;
    double lpmin = 0;
    double lpfactor;

    NormalDistributionImpl llimitedNormal = new NormalDistributionImpl(_mu, _sigma);
    if (_min == null) {
        if (_max != null) {
            // only max is limted
            lmax = _max.doubleValue();
            try {
                lpmax = llimitedNormal.cumulativeProbability(lmax);
            } catch (MathException e) {
                e.printStackTrace();
            }
        }
    } else {
        if (_max == null) {
            // only min is limited.
            lmin = _min.doubleValue();
            try {
                lpmin = llimitedNormal.cumulativeProbability(lmin);
            } catch (MathException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            // both sides limited.

            // make sure min is really smaller than max.
            if (_max.doubleValue() > _min.doubleValue()) {
                lmin = _min.doubleValue();
                lmax = _max.doubleValue();
            } else {
                lmax = _min.doubleValue();
                lmin = _max.doubleValue();
            }

            // get min and max probabilites that are possible
            try {
                lpmin = llimitedNormal.cumulativeProbability(lmin);
                lpmax = llimitedNormal.cumulativeProbability(lmax);

                lpfactor = lpmax - lpmin;

            } catch (MathException e) {
                e.printStackTrace();
            }
        }
    }
    lpfactor = lpmax - lpmin;

    double lrandom = lpmin + Simulator.getRandom().nextDouble() * lpfactor;
    double lresult;

    try {
        lresult = llimitedNormal.inverseCumulativeProbability(lrandom);
    } catch (MathException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        lresult = 0;
    }

    return lresult;

}

From source file:org.xenmaster.monitoring.data.Record.java

protected final void applyStatistics(Collection<Double> values) {
    // Let's get statistical
    DescriptiveStatistics ds = new DescriptiveStatistics();

    for (double util : values) {
        ds.addValue(util);/*  w w w  . j  a  v a 2s  .c o m*/
    }

    double a = ds.getMean();
    double stdDev = ds.getStandardDeviation();

    // TODO: actually test this and generate warning
    // Check if all vCPUs have a fair load, e.g. [45, 60, 50] would be fair, [90, 4, 2] indicates you should learn threading
    if (stdDev > 0.8) {
        Logger.getLogger(getClass())
                .info((vm ? "VM" : "Host") + " " + reference + " has an unfair load distribution");
    }

    if (stdDev > 0) {
        try {
            NormalDistributionImpl ndi = new NormalDistributionImpl(ds.getMean(), stdDev);
            double cp = ndi.cumulativeProbability(90);
            if (cp > 0.8) {
                // 80% of the CPUs have a >90% load
                // TODO warning
                Logger.getLogger(getClass()).info((vm ? "VM" : "Host") + " " + reference
                        + " has a load >=90% on 80% of the available CPUs");
            }
        } catch (MathException ex) {
            Logger.getLogger(getClass()).error("Flawed maths", ex);
        }
    }
}