Search in sources :

Example 21 with ChiSquaredDistribution

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

the class IndTestMixedMultipleTTest method dependencePvalsLinear.

private double[] dependencePvalsLinear(Node x, Node y, List<Node> z) {
    if (!variablesPerNode.containsKey(x)) {
        throw new IllegalArgumentException("Unrecogized node: " + x);
    }
    if (!variablesPerNode.containsKey(y)) {
        throw new IllegalArgumentException("Unrecogized node: " + y);
    }
    for (Node node : z) {
        if (!variablesPerNode.containsKey(node)) {
            throw new IllegalArgumentException("Unrecogized node: " + node);
        }
    }
    List<Node> yzDumList = new ArrayList<>();
    List<Node> yzList = new ArrayList<>();
    yzList.add(y);
    yzList.addAll(z);
    // List<Node> zList = new ArrayList<>();
    yzDumList.addAll(variablesPerNode.get(y));
    for (Node _z : z) {
        yzDumList.addAll(variablesPerNode.get(_z));
    // zList.addAll(variablesPerNode.get(_z));
    }
    int[] _rows = getNonMissingRows(x, y, z);
    regression.setRows(_rows);
    RegressionResult result;
    try {
        result = regression.regress(x, yzDumList);
    } catch (Exception e) {
        return null;
    }
    double[] pVec = new double[yzList.size()];
    double[] pCoef = result.getP();
    // skip intercept at 0
    int coeffInd = 1;
    for (int i = 0; i < pVec.length; i++) {
        List<Node> curDummy = variablesPerNode.get(yzList.get(i));
        if (curDummy.size() == 1) {
            pVec[i] = pCoef[coeffInd];
            coeffInd++;
            continue;
        } else {
            pVec[i] = 0;
        }
        for (Node n : curDummy) {
            pVec[i] += Math.log(pCoef[coeffInd]);
            coeffInd++;
        }
        if (pVec[i] == Double.NEGATIVE_INFINITY)
            pVec[i] = 0.0;
        else
            pVec[i] = 1.0 - new ChiSquaredDistribution(2 * curDummy.size()).cumulativeProbability(-2 * pVec[i]);
    }
    return pVec;
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution) Node(edu.cmu.tetrad.graph.Node) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 22 with ChiSquaredDistribution

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

the class MathFunctions method chiSquaredCdf.

@Description("ChiSquared cdf given the df parameter and value")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
public static double chiSquaredCdf(@SqlType(StandardTypes.DOUBLE) double df, @SqlType(StandardTypes.DOUBLE) double value) {
    checkCondition(value >= 0, INVALID_FUNCTION_ARGUMENT, "value must non-negative");
    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.cumulativeProbability(value);
}
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)

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