Search in sources :

Example 6 with ChiSquaredDistribution

use of org.apache.commons.math3.distribution.ChiSquaredDistribution in project tetrad by cmu-phil.

the class BayesProperties method getLikelihoodRatioP.

/**
 * Calculates the p-value of the graph with respect to the given data.
 */
public final double getLikelihoodRatioP(Graph graph) {
    // Null hypothesis = complete graph.
    List<Node> nodes = graph.getNodes();
    Graph graph0 = new EdgeListGraph(nodes);
    for (int i = 0; i < nodes.size(); i++) {
        for (int j = i + 1; j < nodes.size(); j++) graph0.addDirectedEdge(nodes.get(i), nodes.get(j));
    }
    Ret r0 = getLikelihood2(graph0);
    Ret r1 = getLikelihood2(graph);
    this.likelihood = r1.getLik();
    double lDiff = r0.getLik() - r1.getLik();
    System.out.println("lDiff = " + lDiff);
    int nDiff = r0.getDof() - r1.getDof();
    System.out.println("nDiff = " + nDiff);
    double chisq = 2.0 * lDiff;
    double dof = nDiff;
    this.chisq = chisq;
    this.dof = dof;
    int N = dataSet.getNumRows();
    this.bic = 2 * r1.getLik() - r1.getDof() * Math.log(N);
    System.out.println("bic = " + bic);
    System.out.println("chisq = " + chisq);
    System.out.println("dof = " + dof);
    double p = 1.0 - new ChiSquaredDistribution(dof).cumulativeProbability(chisq);
    System.out.println("p = " + p);
    return p;
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph)

Example 7 with ChiSquaredDistribution

use of org.apache.commons.math3.distribution.ChiSquaredDistribution in project tetrad by cmu-phil.

the class DeltaSextadTest method getPValue.

/**
 * Takes a list of tetrads for the given data set and returns the chi square value for the test. We assume that the
 * tetrads are non-redundant; if not, a matrix exception will be thrown.
 * <p>
 * Calculates the T statistic (Bollen and Ting, p. 161). This is significant if tests as significant using the Chi
 * Square distribution with degrees of freedom equal to the number of nonredundant tetrads tested.
 */
public double getPValue(IntSextad... sextads) {
    // int df = sextads.length;
    int df = dofHarman(sextads.length);
    double chisq = calcChiSquare(sextads);
    // double cdf = ProbUtils.chisqCdf(chisq, df);
    double cdf = new ChiSquaredDistribution(df).cumulativeProbability(chisq);
    return 1.0 - cdf;
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution)

Example 8 with ChiSquaredDistribution

use of org.apache.commons.math3.distribution.ChiSquaredDistribution in project SeqMonk by s-andrews.

the class ChiSquareTest method chiSquarePvalue.

public static double chiSquarePvalue(int[][] table) {
    // We assume that all elements of table have the same number of elements in them
    int[] rowTotals = new int[table.length];
    int[] colTotals = new int[table[0].length];
    int grandTotal = 0;
    for (int i = 0; i < table.length; i++) {
        for (int j = 0; j < table[i].length; j++) {
            rowTotals[i] += table[i][j];
            colTotals[j] += table[i][j];
            grandTotal += table[i][j];
        }
    }
    double[] colProportions = new double[colTotals.length];
    for (int c = 0; c < colTotals.length; c++) {
        colProportions[c] = ((double) colTotals[c]) / grandTotal;
    }
    double chiSquare = 0;
    for (int r = 0; r < table.length; r++) {
        for (int c = 0; c < table[r].length; c++) {
            double expected = rowTotals[r] * colProportions[c];
            chiSquare += Math.pow(expected - table[r][c], 2) / expected;
        }
    }
    ChiSquaredDistribution dist = new ChiSquaredDistribution((colTotals.length - 1) * (rowTotals.length - 1));
    return 1 - dist.cumulativeProbability(chiSquare);
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution)

Example 9 with ChiSquaredDistribution

use of org.apache.commons.math3.distribution.ChiSquaredDistribution in project presto by prestodb.

the class MathFunctions method inverseChiSquaredCdf.

@Description("inverse of ChiSquared cdf given df parameter and probability")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
public static double inverseChiSquaredCdf(@SqlType(StandardTypes.DOUBLE) double df, @SqlType(StandardTypes.DOUBLE) double p) {
    checkCondition(p >= 0 && p <= 1, INVALID_FUNCTION_ARGUMENT, "p must be in the interval [0, 1]");
    checkCondition(df > 0, INVALID_FUNCTION_ARGUMENT, "df must be greater than 0");
    ChiSquaredDistribution distribution = new ChiSquaredDistribution(null, df, ChiSquaredDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    return distribution.inverseCumulativeProbability(p);
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution) DecimalOperators.modulusScalarFunction(com.facebook.presto.type.DecimalOperators.modulusScalarFunction) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 10 with ChiSquaredDistribution

use of org.apache.commons.math3.distribution.ChiSquaredDistribution in project GDSC-SMLM by aherbert.

the class ChiSquaredDistributionTableTest method canComputeProbability.

// @formatter:on
@SeededTest
void canComputeProbability() {
    for (final int df : new int[] { 5, 10 }) {
        double obs;
        double exp;
        double chi = 0;
        final ChiSquaredDistribution d = new ChiSquaredDistribution(null, df);
        obs = ChiSquaredDistributionTable.computePValue(chi, df);
        exp = d.cumulativeProbability(chi);
        Assertions.assertEquals(exp, obs, 1e-10);
        chi = 1;
        for (int i = 0; i < 10; i++, chi *= 2) {
            obs = ChiSquaredDistributionTable.computePValue(chi, df);
            exp = d.cumulativeProbability(chi);
            Assertions.assertEquals(exp, obs, 1e-10);
            obs = ChiSquaredDistributionTable.computeQValue(chi, df);
            exp = 1 - exp;
            Assertions.assertEquals(exp, obs, 1e-10);
        }
    }
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

ChiSquaredDistribution (org.apache.commons.math3.distribution.ChiSquaredDistribution)22 Node (edu.cmu.tetrad.graph.Node)4 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)2 Description (com.facebook.presto.spi.function.Description)2 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)2 SqlType (com.facebook.presto.spi.function.SqlType)2 DecimalOperators.modulusScalarFunction (com.facebook.presto.type.DecimalOperators.modulusScalarFunction)2 LogisticRegression (edu.cmu.tetrad.regression.LogisticRegression)2 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)2 AbstractRealDistribution (org.apache.commons.math3.distribution.AbstractRealDistribution)2 ExponentialDistribution (org.apache.commons.math3.distribution.ExponentialDistribution)2 FDistribution (org.apache.commons.math3.distribution.FDistribution)2 NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)2 TDistribution (org.apache.commons.math3.distribution.TDistribution)2 ChiSquareTest (org.apache.commons.math3.stat.inference.ChiSquareTest)2 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)2 Test (org.junit.Test)2 DoubleArrayList (cern.colt.list.DoubleArrayList)1 IntArrayList (cern.colt.list.IntArrayList)1 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)1