Example usage for org.apache.commons.math.stat.inference TTestImpl TTestImpl

List of usage examples for org.apache.commons.math.stat.inference TTestImpl TTestImpl

Introduction

In this page you can find the example usage for org.apache.commons.math.stat.inference TTestImpl TTestImpl.

Prototype

public TTestImpl() 

Source Link

Document

Default constructor.

Usage

From source file:musite.prediction.feature.disorder.DisorderTest.java

@Test
public void extractMean() throws IOException {
    //         String xml = "testData/musite-test.xml";
    String xml = "data/Uniprot/uniprot_sprot.201009.ptm.musite.with.disorder.xml";

    ProteinsXMLReader reader = DisorderUtil.getDisorderXMLReader();
    Proteins proteins = MusiteIOUtil.read(reader, xml);

    TTest ttest = new TTestImpl();
    StandardDeviation std = new StandardDeviation();

    Map<AminoAcid, double[]> mapAAScores = new EnumMap<AminoAcid, double[]>(AminoAcid.class);
    for (AminoAcid aa : AminoAcid.values()) {
        List<Double> scores = new ArrayList<Double>();
        Iterator<Protein> it = proteins.proteinIterator();
        while (it.hasNext()) {
            Protein protein = it.next();
            List<Double> dis = DisorderUtil.extractDisorder(protein, aa);
            if (dis != null)
                scores.addAll(dis);//  www. ja  v  a  2  s  .co m
        }
        mapAAScores.put(aa, list2array(scores));
        //            System.out.print(aa.toString()+":");
        //            System.out.println(""+average(scores)+"("+scores.size()+")");
    }

    Map<String, AminoAcid> mapKeyAA = getMapKeywordAminoAcid();

    for (PTM ptm : PTM.values()) {
        Set<String> keywords = ptm.getUniprotKeywords();
        for (String keyword : keywords) {
            AnnotationFilter filter = ResidueAnnotationUtil.createAnnotationFilter("keyword",
                    Collections.singleton(keyword), true);

            double[] scores;
            {
                List<Double> list = new ArrayList<Double>();
                Iterator<Protein> it = proteins.proteinIterator();
                while (it.hasNext()) {
                    Protein protein = it.next();
                    List<Double> dis = DisorderUtil.extractDisorder(protein, ptm, filter);
                    if (dis != null)
                        list.addAll(dis);
                }
                scores = list2array(list);
            }

            System.out.print(ptm.toString());
            System.out.print("\t" + keyword);
            if (scores.length == 0)
                System.out.println("\tno_site");
            else {
                System.out.print("\t" + scores.length);
                double mean = StatUtils.mean(scores);
                System.out.print("\t" + mean);
                System.out.print("\t" + std.evaluate(scores, mean));

                AminoAcid aa = mapKeyAA.get(keyword);
                double[] bg = mapAAScores.get(aa);
                System.out.print("\t" + aa.toString());
                System.out.print("\t" + bg.length);
                mean = StatUtils.mean(bg);
                System.out.print("\t" + mean);
                System.out.print("\t" + std.evaluate(bg, mean));

                double pvalue = -1.0;
                try {
                    pvalue = ttest.tTest(scores, bg);
                } catch (Exception e) {
                    //                        e.printStackTrace();
                }

                if (pvalue != -1.0)
                    System.out.print("\t" + pvalue);

                System.out.println();
            }
        }
    }
}

From source file:geogebra.kernel.statistics.AlgoTTestPaired.java

protected final void compute() {

    if (!(tail.getTextString().equals("<") || tail.getTextString().equals(">")
            || tail.getTextString().equals("!=") || tail.getTextString().equals(""))) {
        result.setUndefined();//from  www . ja v a  2s  .c  o  m
        return;
    }

    double p, testStat;

    // sample data input

    int size = geoList0.size();
    if (!geoList1.isDefined() || geoList1.size() != size) {
        result.setUndefined();
        return;
    }

    // create number value arrays
    val0 = new double[size];
    val1 = new double[size];
    GeoElement geo0, geo1;
    NumberValue num0, num1;

    for (int i = 0; i < size; i++) {
        geo0 = geoList0.get(i);
        geo1 = geoList1.get(i);
        if (geo0.isNumberValue() && geo1.isNumberValue()) {
            num0 = (NumberValue) geo0;
            num1 = (NumberValue) geo1;
            val0[i] = num0.getDouble();
            val1[i] = num1.getDouble();

        } else {
            result.setUndefined();
            return;
        }
    }

    try {

        // get the test statistic and p
        if (tTestImpl == null)
            tTestImpl = new TTestImpl();
        testStat = tTestImpl.pairedT(val0, val1);
        p = tTestImpl.pairedTTest(val0, val1);
        testStat = tTestImpl.pairedT(val0, val1);
        p = adjustedPValue(p, testStat);

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

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (MathException e) {
        e.printStackTrace();
    }

}

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

@Override
public final void compute() {

    if (!(StringUtil.isInequality(tail.getTextString()))) {
        result.setUndefined();//www  . j  a v  a 2 s.c o  m
        return;
    }

    double p, testStat;

    // sample data input

    int size = geoList0.size();
    if (!geoList1.isDefined() || geoList1.size() != size) {
        result.setUndefined();
        return;
    }

    // create number value arrays
    val0 = new double[size];
    val1 = new double[size];
    GeoElement geo0, geo1;
    NumberValue num0, num1;

    for (int i = 0; i < size; i++) {
        geo0 = geoList0.get(i);
        geo1 = geoList1.get(i);
        if (geo0 instanceof NumberValue && geo1 instanceof NumberValue) {
            num0 = (NumberValue) geo0;
            num1 = (NumberValue) geo1;
            val0[i] = num0.getDouble();
            val1[i] = num1.getDouble();

        } else {
            result.setUndefined();
            return;
        }
    }

    try {

        // get the test statistic and p
        if (tTestImpl == null)
            tTestImpl = new TTestImpl();
        testStat = tTestImpl.pairedT(val0, val1);
        p = tTestImpl.pairedTTest(val0, val1);
        testStat = tTestImpl.pairedT(val0, val1);
        p = adjustedPValue(p, testStat);

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

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (MathException e) {
        e.printStackTrace();
    }

}

From source file:geogebra.kernel.statistics.AlgoTTest.java

protected final void compute() {

    if (!(tail.getTextString().equals("<") || tail.getTextString().equals(">")
            || tail.getTextString().equals("!=") || tail.getTextString().equals(""))) {
        result.setUndefined();//w  w w . j a  v  a 2  s. co m
        return;
    }

    // sample data input
    if (input.length == 3) {

        int size = geoList.size();
        if (!geoList.isDefined() || size < 2) {
            result.setUndefined();
            return;
        }

        val = new double[size];

        for (int i = 0; i < size; i++) {
            GeoElement geo = geoList.get(i);
            if (geo.isNumberValue()) {
                NumberValue num = (NumberValue) geo;
                val[i] = num.getDouble();

            } else {
                result.setUndefined();
                return;
            }
        }

        try {

            // get the test statistic and p
            if (tTestImpl == null)
                tTestImpl = new TTestImpl();
            testStat = tTestImpl.t(hypMean.getDouble(), val);
            p = tTestImpl.tTest(hypMean.getDouble(), val);
            p = adjustedPValue(p, testStat);

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

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (MathException e) {
            e.printStackTrace();
        }

        // sample statistics input 
    } else {

        // check for valid standard deviation and sample size
        if (sd.getDouble() < 0 || n.getDouble() < 2) {
            result.setUndefined();
            return;
        }
        StatisticalSummaryValues sumStats = new StatisticalSummaryValues(mean.getDouble(),
                sd.getDouble() * sd.getDouble(), (long) n.getDouble(), -1, -1, -1);

        try {

            // get the test statistic and p
            if (tTestImpl == null)
                tTestImpl = new TTestImpl();
            testStat = tTestImpl.t(hypMean.getDouble(), sumStats);
            p = tTestImpl.tTest(hypMean.getDouble(), sumStats);
            p = adjustedPValue(p, testStat);

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

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (MathException e) {
            e.printStackTrace();
        }

    }

}

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

@Override
public final void compute() {

    if (!(StringUtil.isInequality(tail.getTextString()))) {
        result.setUndefined();//from   www . ja  v a2  s  .  c o m
        return;
    }

    // sample data input
    if (input.length == 3) {

        int size = geoList.size();
        if (!geoList.isDefined() || size < 2) {
            result.setUndefined();
            return;
        }

        val = new double[size];

        for (int i = 0; i < size; i++) {
            GeoElement geo = geoList.get(i);
            if (geo instanceof NumberValue) {
                NumberValue num = (NumberValue) geo;
                val[i] = num.getDouble();

            } else {
                result.setUndefined();
                return;
            }
        }

        try {

            // get the test statistic and p
            if (tTestImpl == null)
                tTestImpl = new TTestImpl();
            testStat = tTestImpl.t(hypMean.getDouble(), val);
            p = tTestImpl.tTest(hypMean.getDouble(), val);
            p = adjustedPValue(p, testStat);

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

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (MathException e) {
            e.printStackTrace();
        }

        // sample statistics input
    } else {

        // check for valid standard deviation and sample size
        if (sd.getDouble() < 0 || n.getDouble() < 2) {
            result.setUndefined();
            return;
        }

        try {
            StatisticalSummaryValues sumStats = new StatisticalSummaryValues(mean.getDouble(),
                    sd.getDouble() * sd.getDouble(), (long) n.getDouble(), -1, -1, -1);

            // get the test statistic and p
            if (tTestImpl == null)
                tTestImpl = new TTestImpl();
            testStat = tTestImpl.t(hypMean.getDouble(), sumStats);
            p = tTestImpl.tTest(hypMean.getDouble(), sumStats);
            p = adjustedPValue(p, testStat);

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

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            result.setUndefined();
            return;

        } catch (MathException e) {
            e.printStackTrace();
            result.setUndefined();
            return;
        }

    }

}

From source file:net.sourceforge.jags.model.ModelTest.java

@Test
public void testUnobservedStochasticNode() throws MathException {
    Node mu = model.addConstantNode(new int[] { 1 }, new double[] { 0 });
    Node tau = model.addConstantNode(new int[] { 1 }, new double[] { 1 });
    int N = 1000;
    Node n = model.addStochasticNode("dnorm", new Node[] { mu, tau }, null, null, null);
    model.initialize(true);//from  w  ww.  java 2  s. co m
    model.stopAdapting();
    Monitor m = model.addTraceMonitor(n);
    model.update(N);
    assertEquals(N, model.getCurrentIteration());
    assertEquals(N, m.dim()[1]); // Iterations dimension

    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (double v : m.value(0)) {
        stats.addValue(v);
    }
    TTest test = new TTestImpl();
    assertFalse(test.tTest(0, m.value(0), 0.05));
}

From source file:guineu.modules.dataanalysis.Ttest.TTestTask.java

public double[] Ttest(int mol) throws IllegalArgumentException, MathException {
    DescriptiveStatistics stats1 = new DescriptiveStatistics();
    DescriptiveStatistics stats2 = new DescriptiveStatistics();
    double[] values = new double[3];
    String parameter1 = "";

    try {//from w ww .j  a  va  2 s  . com
        // Determine groups for selected raw data files
        List<String> availableParameterValues = dataset.getParameterAvailableValues(parameter);

        int numberOfGroups = availableParameterValues.size();

        if (numberOfGroups > 1) {
            parameter1 = availableParameterValues.get(0);
            String parameter2 = availableParameterValues.get(1);

            for (String sampleName : dataset.getAllColumnNames()) {
                if (dataset.getParametersValue(sampleName, parameter) != null
                        && dataset.getParametersValue(sampleName, parameter).equals(parameter1)) {
                    try {
                        stats1.addValue((Double) this.dataset.getRow(mol).getPeak(sampleName));
                    } catch (Exception e) {

                    }
                } else if (dataset.getParametersValue(sampleName, parameter) != null
                        && dataset.getParametersValue(sampleName, parameter).equals(parameter2)) {
                    try {
                        stats2.addValue((Double) this.dataset.getRow(mol).getPeak(sampleName));
                    } catch (Exception e) {

                    }
                }
            }
        } else {
            return null;
        }
    } catch (Exception e) {
    }

    TTestImpl ttest = new TTestImpl();
    values[0] = ttest.tTest((StatisticalSummary) stats1, (StatisticalSummary) stats2);
    values[1] = stats1.getMean();
    values[2] = stats2.getMean();
    return values;
}

From source file:geogebra.kernel.statistics.AlgoTTest2.java

protected final void compute() {

    if (!(tail.getTextString().equals("<") || tail.getTextString().equals(">")
            || tail.getTextString().equals("!=") || tail.getTextString().equals(""))) {
        result.setUndefined();/*w  w w  . j a  v a  2 s  .  c  o m*/
        return;
    }

    double p, testStat;

    // sample data input
    if (input.length == 4) {

        int size0 = geoList0.size();
        if (!geoList0.isDefined() || size0 < 2) {
            result.setUndefined();
            return;
        }

        int size1 = geoList1.size();
        if (!geoList1.isDefined() || size1 < 2) {
            result.setUndefined();
            return;
        }

        val0 = new double[size0];
        val1 = new double[size1];
        // load array from first sample
        for (int i = 0; i < size0; i++) {
            GeoElement geo0 = geoList0.get(i);
            if (geo0.isNumberValue()) {
                NumberValue num = (NumberValue) geo0;
                val0[i] = num.getDouble();

            } else {
                result.setUndefined();
                return;
            }
        }
        // load array from second sample
        for (int i = 0; i < size1; i++) {
            GeoElement geo1 = geoList1.get(i);
            if (geo1.isNumberValue()) {
                NumberValue num = (NumberValue) geo1;
                val1[i] = num.getDouble();

            } else {
                result.setUndefined();
                return;
            }
        }

        try {

            // get the test statistic and p
            if (tTestImpl == null)
                tTestImpl = new TTestImpl();

            if (pooled.getBoolean()) {
                testStat = tTestImpl.homoscedasticT(val0, val1);
                p = tTestImpl.homoscedasticTTest(val0, val1);
                p = adjustedPValue(p, testStat);
            } else {
                testStat = tTestImpl.t(val0, val1);
                p = tTestImpl.tTest(val0, val1);
                p = adjustedPValue(p, testStat);
            }

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

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (MathException e) {
            e.printStackTrace();
        }

        // sample statistics input 
    } else {

        // check for valid stand. deviation and sample size
        if (sd0.getDouble() < 0 || sd1.getDouble() < 0 || n0.getDouble() < 2 || n1.getDouble() < 2) {
            result.setUndefined();
            return;
        }

        StatisticalSummaryValues sumStats0 = new StatisticalSummaryValues(mean0.getDouble(),
                sd0.getDouble() * sd0.getDouble(), (long) n0.getDouble(), -1, -1, -1);
        StatisticalSummaryValues sumStats1 = new StatisticalSummaryValues(mean1.getDouble(),
                sd1.getDouble() * sd1.getDouble(), (long) n1.getDouble(), -1, -1, -1);

        try {

            // get the test statistic and p
            if (tTestImpl == null)
                tTestImpl = new TTestImpl();

            if (pooled.getBoolean()) {
                testStat = tTestImpl.homoscedasticT(sumStats0, sumStats1);
                p = tTestImpl.homoscedasticTTest(sumStats0, sumStats1);
                p = adjustedPValue(p, testStat);
            } else {
                testStat = tTestImpl.t(sumStats0, sumStats1);
                p = tTestImpl.tTest(sumStats0, sumStats1);
                p = adjustedPValue(p, testStat);
            }

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

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (MathException e) {
            e.printStackTrace();
        }

    }

}

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

@Override
public final void compute() {

    if (!(StringUtil.isInequality(tail.getTextString()))) {
        result.setUndefined();//w w w  .j a v  a  2 s .  c o  m
        return;
    }

    double p, testStat;

    // sample data input
    if (input.length == 4) {

        int size0 = geoList0.size();
        if (!geoList0.isDefined() || size0 < 2) {
            result.setUndefined();
            return;
        }

        int size1 = geoList1.size();
        if (!geoList1.isDefined() || size1 < 2) {
            result.setUndefined();
            return;
        }

        val0 = new double[size0];
        val1 = new double[size1];
        // load array from first sample
        for (int i = 0; i < size0; i++) {
            GeoElement geo0 = geoList0.get(i);
            if (geo0 instanceof NumberValue) {
                NumberValue num = (NumberValue) geo0;
                val0[i] = num.getDouble();

            } else {
                result.setUndefined();
                return;
            }
        }
        // load array from second sample
        for (int i = 0; i < size1; i++) {
            GeoElement geo1 = geoList1.get(i);
            if (geo1 instanceof NumberValue) {
                NumberValue num = (NumberValue) geo1;
                val1[i] = num.getDouble();

            } else {
                result.setUndefined();
                return;
            }
        }

        try {

            // get the test statistic and p
            if (tTestImpl == null)
                tTestImpl = new TTestImpl();

            if (pooled.getBoolean()) {
                testStat = tTestImpl.homoscedasticT(val0, val1);
                p = tTestImpl.homoscedasticTTest(val0, val1);
                p = adjustedPValue(p, testStat);
            } else {
                testStat = tTestImpl.t(val0, val1);
                p = tTestImpl.tTest(val0, val1);
                p = adjustedPValue(p, testStat);
            }

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

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (MathException e) {
            e.printStackTrace();
        }

        // sample statistics input
    } else {

        // check for valid stand. deviation and sample size
        if (sd0.getDouble() < 0 || sd1.getDouble() < 0 || n0.getDouble() < 2 || n1.getDouble() < 2) {
            result.setUndefined();
            return;
        }

        StatisticalSummaryValues sumStats0 = new StatisticalSummaryValues(mean0.getDouble(),
                sd0.getDouble() * sd0.getDouble(), (long) n0.getDouble(), -1, -1, -1);
        StatisticalSummaryValues sumStats1 = new StatisticalSummaryValues(mean1.getDouble(),
                sd1.getDouble() * sd1.getDouble(), (long) n1.getDouble(), -1, -1, -1);

        try {

            // get the test statistic and p
            if (tTestImpl == null)
                tTestImpl = new TTestImpl();

            if (pooled.getBoolean()) {
                testStat = tTestImpl.homoscedasticT(sumStats0, sumStats1);
                p = tTestImpl.homoscedasticTTest(sumStats0, sumStats1);
                p = adjustedPValue(p, testStat);
            } else {
                testStat = tTestImpl.t(sumStats0, sumStats1);
                p = tTestImpl.tTest(sumStats0, sumStats1);
                p = adjustedPValue(p, testStat);
            }

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

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (MathException e) {
            e.printStackTrace();
        }

    }

}

From source file:net.sourceforge.jags.model.ModelTest.java

@Test
public void testObservedStochasticNode() throws MathException {
    double[] data = normalSample();
    Node mu = model.addConstantNode(new int[] { 1 }, new double[] { 1 });
    Node tau = model.addConstantNode(new int[] { 1 }, new double[] { .001 });
    Node n = model.addStochasticNode("dnorm", new Node[] { mu, tau }, null, null, new double[] { 0 });

    model.initialize(true);/*w  w  w.ja v a2s .c  o  m*/
    model.update(1000);

    int N = 1000;
    model.stopAdapting();
    Monitor m = model.addTraceMonitor(n);
    model.update(N);
    assertEquals(N, m.dim()[1]); // Iterations dimension

    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (double v : m.value(0)) {
        stats.addValue(v);
    }
    TTest test = new TTestImpl();
    assertFalse(test.tTest(0, m.value(0), 0.05));
}