Example usage for org.apache.commons.math3.stat.inference TTest TTest

List of usage examples for org.apache.commons.math3.stat.inference TTest TTest

Introduction

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

Prototype

TTest

Source Link

Usage

From source file:GeMSE.GS.Analysis.Stats.OneSampleTTestPanel.java

public OneSampleTTestPanel() {
    initComponents();/*from   w w  w .ja va  2 s.  c om*/
    _sample = new double[0];
    _studentTest = new TTest();

    _decFor = new DecimalFormat("#.#########");
    _decFor.setRoundingMode(RoundingMode.CEILING);
    DecimalFormatSymbols decFors = _decFor.getDecimalFormatSymbols();
    decFors.setNaN("NaN");
    decFors.setInfinity("");
    _decFor.setDecimalFormatSymbols(decFors);
}

From source file:coolmap.utils.statistics.test.CTest.java

public static void ttest(CoolMapObject obj, Dimension direction, VNode leafNode1, VNode leafNode2) {
    try {/*w  w w .j av a  2  s . co  m*/
        ArrayList<VNode> leafNodes = new ArrayList<VNode>();
        if (direction == Dimension.ROW) {
            leafNodes.addAll(obj.getViewNodesRow());
        } else if (direction == Dimension.COLUMN) {
            leafNodes.addAll(obj.getViewNodesColumn());
        } else {
            return;
        }

        if (leafNodes.contains(leafNode1) && leafNodes.contains(leafNode2)) {

            double[] data1 = null;
            double[] data2 = null;

            if (direction == Dimension.ROW) {
                data1 = extractRow(obj, leafNode1);
                data2 = extractRow(obj, leafNode2);
            } else if (direction == Dimension.COLUMN) {
                data1 = extractColumn(obj, leafNode1);
                data2 = extractColumn(obj, leafNode2);
            }

            TTest test = new TTest();
            double pValue = test.pairedTTest(data1, data2);

            TestResult result = new TestResult("Student Paired T-Test",
                    "Two tailed paired t-test with alpha = 0.05.\nData: " + obj.getName() + "\nDirection: "
                            + direction + "\nGroups: " + leafNode1 + ", " + leafNode2,
                    pValue);
            CMConsole.log(result.toString());

        } else {
            CMConsole.logError(
                    "T-test error: group dimension mismatch, must both be row ontology group or column ontology groups.");
        }

    } catch (Exception e) {
        CMConsole.logError("T-test error: " + " Dataset:" + obj + " Direction:" + direction + " Group1:"
                + leafNode1 + " Group2:" + leafNode2);
    }
}

From source file:GeMSE.GS.Analysis.Stats.TwoSampleTTestPanel.java

public TwoSampleTTestPanel() {
    initComponents();/*from   www.  j a  va  2  s. c o m*/
    _sample1 = new double[0];
    _sample2 = new double[0];

    _decFor = new DecimalFormat("#.#########");
    _decFor.setRoundingMode(RoundingMode.CEILING);
    DecimalFormatSymbols decFors = _decFor.getDecimalFormatSymbols();
    decFors.setNaN("NaN");
    decFors.setInfinity("");
    _decFor.setDecimalFormatSymbols(decFors);

    _variance = new Variance();
    _studentTest = new TTest();
}

From source file:carskit.alg.cars.transformation.prefiltering.splitting.ItemSplitting.java

public Table<Integer, Integer, Integer> split(SparseMatrix sm, int min) {
    Table<Integer, Integer, Integer> datatable = HashBasedTable.create();

    for (Integer j : itemRatingList.keySet()) {
        Collection<Integer> uis = itemRatingList.get(j);
        double maxt = Double.MIN_VALUE;
        int splitcond = -1;

        for (Integer cond : condContextsList.keySet()) {
            Collection<Integer> ctx = condContextsList.get(cond);
            // start to extract two rating list
            HashMultiset<Double> rate1 = HashMultiset.create();
            HashMultiset<Double> rate2 = HashMultiset.create();

            for (Integer ui : uis) {
                List<Integer> uctx = sm.getColumns(ui);
                for (Integer c : uctx) {
                    double rate = sm.get(ui, c);
                    if (ctx.contains(c))
                        rate1.add(rate);
                    else
                        rate2.add(rate);
                }// w  ww.j a  v a 2  s.c  om
            }

            double[] drate1 = Doubles.toArray(rate1);
            double[] drate2 = Doubles.toArray(rate2);

            if (drate1.length >= min && drate2.length >= min) {
                TTest tt = new TTest();
                double p = tt.tTest(drate1, drate2);
                if (p < 0.05) {
                    double t = tt.t(drate1, drate2);
                    if (t > maxt) {
                        // update the split
                        splitcond = cond;
                        maxt = t;
                    }
                }
            }
        }
        if (splitcond != -1) {
            // put u, ctx, new uid into datatable
            int newid = startId++;
            Collection<Integer> ctx = condContextsList.get(splitcond);
            for (Integer c : ctx)
                datatable.put(j, c, newid);
        }

    }
    Logs.info(datatable.rowKeySet().size() + " items have been splitted.");
    return datatable;
}

From source file:carskit.alg.cars.transformation.prefiltering.splitting.UserSplitting.java

public Table<Integer, Integer, Integer> split(SparseMatrix sm, int min) {
    Logs.debug("UserSplitting: startId = " + startId);
    Table<Integer, Integer, Integer> datatable = HashBasedTable.create();

    for (Integer u : userRatingList.keySet()) {
        Collection<Integer> uis = userRatingList.get(u);
        double maxt = Double.MIN_VALUE;
        int splitcond = -1;

        for (Integer cond : condContextsList.keySet()) {
            Collection<Integer> ctx = condContextsList.get(cond);
            // start to extract two rating list
            HashMultiset<Double> rate1 = HashMultiset.create();
            HashMultiset<Double> rate2 = HashMultiset.create();

            for (Integer ui : uis) {
                List<Integer> uctx = sm.getColumns(ui);
                for (Integer c : uctx) {
                    double rate = sm.get(ui, c);
                    if (ctx.contains(c))
                        rate1.add(rate);
                    else
                        rate2.add(rate);
                }/*from w w  w . jav  a 2 s .c  o m*/
            }

            double[] drate1 = Doubles.toArray(rate1);
            double[] drate2 = Doubles.toArray(rate2);

            if (drate1.length >= min && drate2.length >= min) {
                TTest tt = new TTest();
                double p = tt.tTest(drate1, drate2);
                if (p < 0.05) {
                    double t = tt.t(drate1, drate2);
                    if (t > maxt) {
                        // update the split
                        splitcond = cond;
                        maxt = t;
                    }
                }
            }
        }

        if (splitcond != -1) {
            // put u, ctx, new uid into datatable
            int newid = startId++;
            Collection<Integer> ctx = condContextsList.get(splitcond);
            for (Integer c : ctx)
                datatable.put(u, c, newid);
        }

    }
    Logs.info(datatable.rowKeySet().size() + " users have been splitted.");
    return datatable;
}

From source file:io.druid.query.aggregation.datasketches.tuple.ArrayOfDoublesSketchTTestPostAggregator.java

@Override
public double[] compute(final Map<String, Object> combinedAggregators) {
    final ArrayOfDoublesSketch sketch1 = (ArrayOfDoublesSketch) getFields().get(0).compute(combinedAggregators);
    final ArrayOfDoublesSketch sketch2 = (ArrayOfDoublesSketch) getFields().get(1).compute(combinedAggregators);
    if (sketch1.getNumValues() != sketch2.getNumValues()) {
        throw new IAE("Sketches have different number of values: %d and %d", sketch1.getNumValues(),
                sketch2.getNumValues());
    }/*from www.  j a  va  2 s .  co m*/

    final SummaryStatistics[] stats1 = getStats(sketch1);
    final SummaryStatistics[] stats2 = getStats(sketch2);

    final int numberOfValues = sketch1.getNumValues();
    final double[] pValues = new double[numberOfValues];
    final TTest test = new TTest();
    for (int i = 0; i < pValues.length; i++) {
        pValues[i] = test.tTest(stats1[i], stats2[i]);
    }
    return pValues;
}

From source file:Query7Servlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  w  w  w .  ja va 2  s.  c om*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Connection connection = null;
    Statement stmt = null;

    String patientsWithAllSqlQuery = null;
    String patientsWithoutAllSqlQuery = null;

    ResultSet expWithAllResultSet = null;
    ResultSet expWithoutAllResultSet = null;

    String geneID = null;
    double expValue = 0;
    List<Double> exprValuesForGeneID = null;

    Map<String, List<Double>> geneToExpValueALL = new HashMap<String, List<Double>>();
    Map<String, List<Double>> geneToExpValueNOTALL = new HashMap<String, List<Double>>();

    Set<String> geneIDSet = null;
    String[] geneIDs = null;
    List<Double> listOneForGeneId = null;
    List<Double> listTwoForGeneId = null;
    Double[] listOneForGeneIdArr = null;
    Double[] listTwoForGeneIdArr = null;

    double p_value = 0;
    JSONArray informativeGenes = new JSONArray();
    JSONObject informativeGene = null;

    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        Class.forName("oracle.jdbc.driver.OracleDriver");

        connection = DriverManager.getConnection("jdbc:oracle:thin:@VIVEK:1521/ORCL", "system", "vivek");

        stmt = connection.createStatement();

        patientsWithAllSqlQuery = "SELECT P.USER_ID, M.EXPRESSION FROM MICROARRAY_FACT M, PROBE P, CLINICAL_FACT C "
                + "WHERE M.PB_ID = P.PB_ID " + "AND M.S_ID = C.S_ID "
                + "AND C.P_ID IN (SELECT P_ID FROM CLINICAL_FACT, DISEASE "
                + "WHERE CLINICAL_FACT.DS_ID = DISEASE.DS_ID AND NAME = 'ALL')";

        patientsWithoutAllSqlQuery = "SELECT P.USER_ID, M.EXPRESSION FROM MICROARRAY_FACT M, PROBE P, CLINICAL_FACT C "
                + "WHERE M.PB_ID = P.PB_ID " + "AND M.S_ID = C.S_ID "
                + "AND C.P_ID IN (SELECT P_ID FROM CLINICAL_FACT, DISEASE "
                + "WHERE CLINICAL_FACT.DS_ID = DISEASE.DS_ID AND NAME <> 'ALL')";

        //Expression Values for patients with "ALL"
        expWithAllResultSet = stmt.executeQuery(patientsWithAllSqlQuery);

        //Converting the result set for "ALL" to the map
        while (expWithAllResultSet.next()) {
            geneID = expWithAllResultSet.getString(1);
            expValue = expWithAllResultSet.getDouble(2);

            if (geneToExpValueALL.get(geneID) != null) {
                exprValuesForGeneID = geneToExpValueALL.get(geneID);
            } else {
                exprValuesForGeneID = new ArrayList<Double>();
            }

            exprValuesForGeneID.add(expValue);
            geneToExpValueALL.put(geneID, exprValuesForGeneID);
        }

        //Expression Values for patients without "ALL"
        expWithoutAllResultSet = stmt.executeQuery(patientsWithoutAllSqlQuery);

        while (expWithoutAllResultSet.next()) {
            geneID = expWithoutAllResultSet.getString(1);
            expValue = expWithoutAllResultSet.getDouble(2);

            if (geneToExpValueNOTALL.get(geneID) != null) {
                exprValuesForGeneID = geneToExpValueNOTALL.get(geneID);
            } else {
                exprValuesForGeneID = new ArrayList<Double>();
            }

            exprValuesForGeneID.add(expValue);
            geneToExpValueNOTALL.put(geneID, exprValuesForGeneID);
        }

        //Calculating the p value for genes between two groups
        geneIDSet = geneToExpValueALL.keySet();
        geneIDs = new String[geneIDSet.size()];
        geneIDSet.toArray(geneIDs);

        for (int i = 0; i < geneIDs.length; i++) {
            listOneForGeneId = geneToExpValueALL.get(geneIDs[i]);
            listTwoForGeneId = geneToExpValueNOTALL.get(geneIDs[i]);

            listOneForGeneIdArr = new Double[listOneForGeneId.size()];
            listTwoForGeneIdArr = new Double[listTwoForGeneId.size()];

            listOneForGeneId.toArray(listOneForGeneIdArr);
            listTwoForGeneId.toArray(listTwoForGeneIdArr);

            p_value = new TTest().tTest(ArrayUtils.toPrimitive(listOneForGeneIdArr),
                    ArrayUtils.toPrimitive(listTwoForGeneIdArr));

            if (p_value < 0.01) {
                informativeGene = new JSONObject();
                informativeGene.put("infogene", geneIDs[i]);
                informativeGenes.put(informativeGene);
            }
        }

        out.print(informativeGenes);
        //Closing the statement and connection
        stmt.close();
        connection.close();
    } catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;
    } catch (ClassNotFoundException e) {
        System.out.println("Where is your Oracle JDBC Driver?");
        e.printStackTrace();
        return;
    } catch (JSONException e) {
        System.out.println("Where is your Oracle JDBC Driver?");
        e.printStackTrace();
        return;
    }
}

From source file:calculators.Calculator.java

/**
 * Calculates the pvalue.//from   w  w  w .j a v a 2 s  .c o m
 *
 * @param control the control group intensities
 * @param target the target group intensities
 * @return
 */
private Double calculatePvalue(final double[] control, final double[] target) {
    TTest t_test = new TTest();
    Log10 logTen = new Log10();
    return -logTen.value(t_test.tTest(target, control));
}

From source file:info.debatty.jinu.CaseResult.java

/**
 *
 * @param test//from  www .j a  v a 2s  .co  m
 * @param other_test
 * @param result
 * @param param_value
 * @return
 */
public final double getSimilarity(final TestInterface test, final TestInterface other_test, final long result,
        final double param_value) {

    double[] test_results = new double[testcase.getIterations()];
    double[] other_results = new double[testcase.getIterations()];

    int i = 0;
    for (TestResult tr : results.get(new TestAndValue(test, param_value))) {
        test_results[i] = tr.getValue(result);
        i++;
    }

    i = 0;
    TestAndValue test_and_value = new TestAndValue(other_test, param_value);
    for (TestResult tr : results.get(test_and_value)) {
        other_results[i] = tr.getValue(result);
        i++;
    }

    TTest t_test = new TTest();
    return t_test.tTest(test_results, other_results);
}

From source file:eu.betaas.taas.securitymanager.taastrustmanager.taastrustcalculator.StatisticsCalculator.java

public boolean isSimilarMean(double[] valuesA, double[] valuesB) {
    TTest studentTest = new TTest();
    boolean testResult = false;
    double error = 0;
    double tValue = 0;

    double meanA = StatUtils.mean(valuesA);
    double meanB = StatUtils.mean(valuesB);

    try {/*from  ww  w .  ja  v a  2  s  .  c o m*/
        testResult = studentTest.tTest(valuesA, valuesB, 0.05);
        error = studentTest.tTest(valuesA, valuesB);
        tValue = studentTest.t(valuesA, valuesB);
        logger.debug("Test result --> MA = " + meanA + " -- MB = " + meanB);
        logger.debug("Test result --> " + testResult + " with p " + error + " and tValue = " + tValue);
    } catch (Exception ex) {
        logger.error("There was an error when trying to calculate Student's t test!");
        ex.printStackTrace();
        return false;
    }

    return testResult;

}