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(double mean, double sd) 

Source Link

Document

Create a normal distribution using the given mean and standard deviation.

Usage

From source file:markov.java

/**
 * @param args//from  w ww. j  a  v  a 2 s  .  c  o m
 */
public static void main(String[] args) {

    // hack: eclipse don't support IO redirection worth a shit
    // try {
    // System.setIn(new FileInputStream("./json"));
    // } catch (FileNotFoundException e1) {
    // // TODO Auto-generated catch block
    // e1.printStackTrace();
    // }

    boolean graphMode = false;
    boolean jsonMode = false;
    boolean jsonRecoverMode = false;
    boolean endNode = false;

    int count = -1;

    long n = 0;
    long sumOfSqr = 0;
    long sum = 0;

    for (String s : args) {

        if (!s.matches("^-[vegjJh]*(c[0-9]*)?$")) {
            System.out.println("invalid argument");
            return;
        }

        if (s.matches("^-.*h.*")) {
            System.out.println(HELP);
            return;
        }
        if (s.matches("^-.*v.*")) {
            verbose = true;
            log("verbose mode");
        }
        if (s.matches("^-.*g.*")) {
            graphMode = true;
            log("graph mode");
        }
        if (s.matches("^-.*j.*")) {
            jsonMode = true;
            log("json mode");
        }
        if (s.matches("^-.*J.*")) {
            jsonRecoverMode = true;
            log("json recover mode");
        }
        if (s.matches("^-.*e.*")) {
            endNode = true;
            log("include end node");
        }
        if (s.matches("^-.*c[0-9]*$")) {
            log("counted output mode");
            count = Integer.parseInt(s.replaceAll("^-.*c", ""));
        }

        boolean error = (graphMode == true && jsonMode == true);
        if (!error) {
            error = (count > -1) && (graphMode == true || jsonMode == true);
        }

        if (error) {
            System.err.println("[error] switches j, g and, c are mutualy exclusive.");
            return;
        }

    }

    StateTransitionDiagram<Character> std;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    try {
        if (!jsonRecoverMode) {
            Trainer<Character> trainer = new Trainer<Character>();
            String s = br.readLine();
            while (s != null) {
                trainer.train(string2List(s));
                n++;
                sumOfSqr += s.length() * s.length();
                sum += s.length();
                s = br.readLine();
            }
            if (n == 0) {
                System.err
                        .println("Invalid corpus: At least one sample is required, two to make it interesting");
                return;
            }
            std = trainer.getTransitionDiagram();
        } else {
            std = new StateTransitionDiagram<Character>();
            GsonStub gstub = new Gson().fromJson(br, GsonStub.class);
            n = gstub.meta.n;
            sum = gstub.meta.sum;
            sumOfSqr = gstub.meta.sumOfSqr;

            for (Entry<String, StateStub> entry : gstub.states.entrySet()) {
                State<Character> state;
                if (entry.getKey().equals("null")) {
                    state = std.getGuard();
                } else {
                    state = std.getState(Character.valueOf(entry.getKey().charAt(0)));
                }
                for (Entry<String, Integer> transitions : entry.getValue().transitions.entrySet()) {
                    State<Character> tranny;
                    if (transitions.getKey().equals("null")) {
                        tranny = std.getGuard();
                    } else {
                        tranny = std.getState(Character.valueOf(transitions.getKey().charAt(0)));
                    }

                    state.addTransition(tranny.getValue(), transitions.getValue());
                }
            }
        }
        if (graphMode) {
            if (endNode) {
                System.out.println(std.toString());
            } else {
                System.out.println(std.removeEndGuards().toString());
            }
            return;
        }
        if (jsonMode) {
            Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

            String partialJson;
            if (endNode) {
                partialJson = gson.toJson(std);
            } else {
                partialJson = gson.toJson(std.removeEndGuards());
            }
            GsonStub gstub = new Gson().fromJson(partialJson, GsonStub.class);
            gstub.meta = new Meta();
            gstub.meta.n = n;
            gstub.meta.sum = sum;
            gstub.meta.sumOfSqr = sumOfSqr;

            System.out.println(gson.toJson(gstub));
            return;
        }

        Generator<Character> generator;
        if (endNode) {
            generator = new EndTagGenerator<Character>(std);
        } else {
            double sd = ((double) sumOfSqr - (double) (sum * sum) / (double) n) / (double) (n - 1);
            double mean = (double) sum / (double) n;
            log(String.format("mean: %.4f sd: %.4f", mean, sd));
            NormalDistributionImpl dist = new NormalDistributionImpl(mean, sd);
            generator = new NormalizedGenerator<Character>(std.removeEndGuards(), dist);
        }
        if (count >= 0) {
            for (int c = 0; c < count; c++) {
                output(generator);
            }
        } else {
            while (true) {
                output(generator);
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:de.tud.kom.p2psim.impl.util.stat.distributions.NormalDistribution.java

@XMLConfigurableConstructor({ "mu", "sigma" })
public NormalDistribution(double mu, double sigma) {
    this.mu = mu;
    this.sigma = sigma;
    normal = new NormalDistributionImpl(mu, sigma);
}

From source file:de.tud.kom.p2psim.impl.util.stat.distributions.NormalDistribution.java

/**
 * returns a random value normally distributed with mu = _mu and sigma =
 * _sigma./*w  w w .j  a va 2  s .  co m*/
 * 
 * @param _mu
 * @param _sigma
 * @return as double
 */
public static double returnValue(double _mu, double _sigma) {
    try {
        NormalDistributionImpl d = new NormalDistributionImpl(_mu, _sigma);
        return d.inverseCumulativeProbability(Simulator.getRandom().nextDouble());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 0;
    }

}

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

@Override
public final void compute() {

    double n1 = n.getDouble();
    double phat = proportion.getDouble();
    double cLevel = level.getDouble();

    NormalDistributionImpl normalDist = new NormalDistributionImpl(0, 1);

    double critZ = 0;

    try {/*w w  w . ja  v  a 2  s.  c o  m*/
        critZ = normalDist.inverseCumulativeProbability((1 - cLevel) / 2);
    } catch (Exception e) {
        result.setUndefined();
        return;
    }

    se = Math.sqrt(phat * (1 - phat) / n1);
    double z = Math.abs(critZ);
    me = z * se;

    // put these results into the output list
    result.clear();
    result.add(new GeoNumeric(cons, phat - me));
    result.add(new GeoNumeric(cons, phat + me));

}

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

private void calculateZValues(int n) {

    zValues = new double[n];
    NormalDistributionImpl normalDist = new NormalDistributionImpl(0, 1);
    double x;/*  ww w  .  ja v a 2s.c  om*/

    try {
        x = 1 - Math.pow(0.5, 1.0 / n);
        zValues[0] = normalDist.inverseCumulativeProbability(x);

        for (int i = 2; i < n; i++) {
            x = (i - 0.3175) / (n + 0.365);
            zValues[i - 1] = normalDist.inverseCumulativeProbability(x);
        }

        x = Math.pow(0.5, 1.0 / n);
        zValues[n - 1] = normalDist.inverseCumulativeProbability(x);

    } catch (MathException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

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

@Override
public final void compute() {

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

    NormalDistributionImpl normalDist = new NormalDistributionImpl(0, 1);

    double critZ = 0;

    try {//from www . j a va2 s . c om
        critZ = normalDist.inverseCumulativeProbability((1 - cLevel) / 2);
    } catch (Exception e) {
        result.setUndefined();
        return;
    }

    double stat = phat1 - phat2;
    se = Math.sqrt(phat1 * (1 - phat1) / n1 + phat2 * (1 - phat2) / n2);
    double z = Math.abs(critZ);
    me = z * se;

    // put these results into the output list
    result.clear();
    result.add(new GeoNumeric(cons, stat - me));
    result.add(new GeoNumeric(cons, stat + me));

}

From source file:de.tud.kom.p2psim.impl.util.stat.distributions.LimitedNormalDistribution.java

public void config(LimitedNormalConfigurer dc) {
    mu = dc.getMu();//from  ww w  .  jav  a  2  s  .co  m
    sigma = dc.getSigma();
    limitedMin = dc.isLimitedMin();
    limitedMax = dc.isLimitedMax();

    limitedNormal = new NormalDistributionImpl(mu, sigma);

    if (limitedMin == false) {
        if (limitedMax == false) {
            limitType = LIMIT_NORMAL_DIST_NONE;
        } else {
            // only max is limted
            limitType = LIMIT_NORMAL_DIST_MAX;
            max = dc.getMax();
            try {
                pmax = limitedNormal.cumulativeProbability(max);
            } catch (MathException e) {
                e.printStackTrace();
            }
        }
    } else {
        if (limitedMax == false) {
            // only min is limited.
            limitType = LIMIT_NORMAL_DIST_MIN;
            min = dc.getMin();
            try {
                pmin = limitedNormal.cumulativeProbability(min);
            } catch (MathException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            // both sides limited.
            limitType = LIMIT_NORMAL_DIST_BOTH;

            // make sure min is really smaller than max.
            if (max > min) {
                min = dc.getMin();
                max = dc.getMax();
            } else {
                max = dc.getMin();
                min = dc.getMax();
            }

            // get min and max probabilites that are possible
            try {
                pmin = limitedNormal.cumulativeProbability(min);
                pmax = limitedNormal.cumulativeProbability(max);

                pfactor = pmax - pmin;

            } catch (MathException e) {
                e.printStackTrace();
            }
        }
    }
    pfactor = pmax - pmin;

    System.out.println("configured Limited Normal Distribution of type " + limitType);
}

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

@Override
public final void compute() {

    String testType;//from  ww w . j a  v a 2s. co 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 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;//from w ww  . 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.AlgoZMeanEstimate.java

@Override
public final void compute() {

    if (!sd.isDefined() || !level.isDefined()) {
        result.setUndefined();//from w w w . j  a v  a  2 s  .c o m
        return;
    }

    double n1, mean1;
    double sd1 = sd.getDouble();
    double cLevel = level.getDouble();

    if (list == null) {

        if (!n.isDefined() || !mean.isDefined()) {
            result.setUndefined();
            return;
        }

        n1 = n.getDouble();
        mean1 = mean.getDouble();

    } else {

        if (!list.isDefined()) {
            result.setUndefined();
            return;
        }

        n1 = list.size();

        mean1 = list.mean();
    }

    NormalDistributionImpl normalDist = new NormalDistributionImpl(0, 1);

    double critZ = 0;

    try {
        critZ = normalDist.inverseCumulativeProbability((1 - cLevel) / 2);
    } catch (Exception e) {
        result.setUndefined();
        return;
    }

    double se = sd1 / Math.sqrt(n1);
    double z = Math.abs(critZ);
    me = z * se;

    // put these results into the output list
    result.clear();
    result.add(new GeoNumeric(cons, mean1 - me));
    result.add(new GeoNumeric(cons, mean1 + me));

}