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;
}
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;
}
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);
}
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);
}
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);
}
}
}
Aggregations