Example usage for org.apache.commons.math.stat.inference ChiSquareTestImpl chiSquareTest

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

Introduction

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

Prototype

public boolean chiSquareTest(long[][] counts, double alpha) throws IllegalArgumentException, MathException 

Source Link

Usage

From source file:mlflex.EvaluationMetrics.java

private double CalculateLogRankStatisticMultipleGroups(SurvivalGroups survivalsList) throws Exception {
    double[] allExpected = new double[survivalsList.Size()];
    long[] allObserved = new long[survivalsList.Size()];

    HashMap<Integer, ArrayList<Double>> allTimesExpected = new HashMap<Integer, ArrayList<Double>>();

    for (int i = 0; i < survivalsList.Size(); i++)
        allTimesExpected.put(i, new ArrayList<Double>());

    for (double time : new ArrayList<Double>(
            Lists.Sort(new ArrayList(new HashSet<Double>(survivalsList.GetObservedTimes()))))) {
        double numAtRisk = Lists.GreaterThan(survivalsList.GetAllTimes(), time, true).size();
        double numDied = Lists.GetNumEqualTo(survivalsList.GetObservedTimes(), time);
        double riskOfDeath = numDied / numAtRisk;

        for (int i = 0; i < survivalsList.Size(); i++) {
            Survivals survivals = survivalsList.GetGroup(i);

            double numAllEvents = Lists.GreaterThan(survivals.GetAllTimes(), time, true).size();
            double numExpectedEvents = numAllEvents * riskOfDeath;

            allTimesExpected.get(i).add(numExpectedEvents);
        }/*from w w w  . j av a  2 s .c o  m*/
    }

    for (int i = 0; i < survivalsList.Size(); i++) {
        double expected = MathUtility.Sum(allTimesExpected.get(i));
        long observed = survivalsList.GetGroup(i).GetObservedTimes().size();

        allExpected[i] = expected;
        allObserved[i] = observed;
    }

    //http://commons.apache.org/math/api-2.1/index.html
    org.apache.commons.math.stat.inference.ChiSquareTestImpl chi = new org.apache.commons.math.stat.inference.ChiSquareTestImpl();
    double chiSquareP = chi.chiSquareTest(allExpected, allObserved);

    return chiSquareP;
}