Search in sources :

Example 1 with ChiSquaredDistribution

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

the class IndTestMultinomialLogisticRegression method isIndependentMultinomialLogisticRegression.

private boolean isIndependentMultinomialLogisticRegression(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(x)) {
            throw new IllegalArgumentException("Unrecogized node: " + node);
        }
    }
    List<Double> pValues = new ArrayList<>();
    int[] _rows = getNonMissingRows(x, y, z);
    logisticRegression.setRows(_rows);
    for (Node _x : variablesPerNode.get(x)) {
        // Without y
        List<Node> regressors0 = new ArrayList<>();
        for (Node _z : z) {
            regressors0.addAll(variablesPerNode.get(_z));
        }
        LogisticRegression.Result result0 = logisticRegression.regress((DiscreteVariable) _x, regressors0);
        // With y.
        List<Node> regressors1 = new ArrayList<>();
        regressors1.addAll(variablesPerNode.get(y));
        for (Node _z : z) {
            regressors1.addAll(variablesPerNode.get(_z));
        }
        LogisticRegression.Result result1 = logisticRegression.regress((DiscreteVariable) _x, regressors1);
        // Returns -2 LL
        double ll0 = result0.getLogLikelihood();
        double ll1 = result1.getLogLikelihood();
        double chisq = (ll0 - ll1);
        int df = variablesPerNode.get(y).size();
        double p = 1.0 - new ChiSquaredDistribution(df).cumulativeProbability(chisq);
        pValues.add(p);
    }
    double p = 1.0;
    // This is only one method that can be used, this requires every coefficient to be significant
    for (double val : pValues) {
        if (val < p)
            p = val;
    }
    boolean indep = p > alpha;
    this.lastP = p;
    if (verbose) {
        if (indep) {
            TetradLogger.getInstance().log("independencies", SearchLogUtils.independenceFactMsg(x, y, z, p));
        } else {
            TetradLogger.getInstance().log("dependencies", SearchLogUtils.dependenceFactMsg(x, y, z, p));
        }
    }
    return indep;
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution) Node(edu.cmu.tetrad.graph.Node) LogisticRegression(edu.cmu.tetrad.regression.LogisticRegression)

Example 2 with ChiSquaredDistribution

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

the class IndTestConditionalGaussianLRT method isIndependent.

/**
 * @return true if the given independence question is judged true, false if not. The independence question is of the
 * form x _||_ y | z, z = <z1,...,zn>, where x, y, z1,...,zn are searchVariables in the list returned by
 * getVariableNames().
 */
public boolean isIndependent(Node x, Node y, List<Node> z) {
    likelihood.setNumCategoriesToDiscretize(numCategoriesToDiscretize);
    int _x = nodesHash.get(x);
    int _y = nodesHash.get(y);
    int[] list0 = new int[z.size() + 1];
    int[] list1 = new int[z.size() + 1];
    int[] list2 = new int[z.size()];
    list0[0] = _x;
    list1[0] = _y;
    for (int i = 0; i < z.size(); i++) {
        int _z = nodesHash.get(z.get(i));
        list0[i + 1] = _z;
        list1[i + 1] = _z;
        list2[i] = _z;
    }
    ConditionalGaussianLikelihood.Ret ret1 = likelihood.getLikelihood(_y, list0);
    ConditionalGaussianLikelihood.Ret ret2 = likelihood.getLikelihood(_y, list2);
    ConditionalGaussianLikelihood.Ret ret3 = likelihood.getLikelihood(_x, list1);
    ConditionalGaussianLikelihood.Ret ret4 = likelihood.getLikelihood(_x, list2);
    double lik0 = ret1.getLik() - ret2.getLik();
    double dof0 = ret1.getDof() - ret2.getDof();
    double lik1 = ret3.getLik() - ret4.getLik();
    double dof1 = ret3.getDof() - ret4.getDof();
    if (dof0 <= 0) {
        dof0 = 1;
    // throw new IllegalArgumentException("DOF must be >= 1");
    }
    if (dof1 <= 0) {
        dof1 = 1;
    // throw new IllegalArgumentException("DOF must be >= 1");
    }
    double p0 = 0;
    double p1 = 0;
    try {
        p0 = 1.0 - new ChiSquaredDistribution(dof0).cumulativeProbability(2.0 * lik0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        p1 = 1.0 - new ChiSquaredDistribution(dof1).cumulativeProbability(2.0 * lik1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    this.pValue = Math.min(p0, p1);
    return this.pValue > alpha;
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution)

Example 3 with ChiSquaredDistribution

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

the class IndTestMVPLRT method isIndependent.

/**
 * @return true if the given independence question is judged true, false if not. The independence question is of the
 * form x _||_ y | z, z = <z1,...,zn>, where x, y, z1,...,zn are searchVariables in the list returned by
 * getVariableNames().
 */
public boolean isIndependent(Node x, Node y, List<Node> z) {
    int _x = nodesHash.get(x);
    int _y = nodesHash.get(y);
    int[] list0 = new int[z.size() + 1];
    int[] list1 = new int[z.size() + 1];
    int[] list2 = new int[z.size()];
    list0[0] = _x;
    list1[0] = _y;
    for (int i = 0; i < z.size(); i++) {
        int _z = nodesHash.get(z.get(i));
        list0[i + 1] = _z;
        list1[i + 1] = _z;
        list2[i] = _z;
    }
    double lik_0;
    double dof_0;
    double lik_1;
    double dof_1;
    lik_0 = likelihood.getLik(_y, list0) - likelihood.getLik(_y, list2);
    dof_0 = likelihood.getLik(_y, list0) - likelihood.getLik(_y, list2);
    lik_1 = likelihood.getLik(_x, list1) - likelihood.getLik(_x, list2);
    dof_1 = likelihood.getLik(_x, list1) - likelihood.getLik(_x, list2);
    if (dof_0 <= 0) {
        dof_0 = 1;
    }
    if (dof_1 <= 0) {
        dof_1 = 1;
    }
    double p_0 = 0;
    double p_1 = 0;
    try {
        p_0 = 1.0 - new ChiSquaredDistribution(dof_0).cumulativeProbability(2.0 * lik_0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        p_1 = 1.0 - new ChiSquaredDistribution(dof_1).cumulativeProbability(2.0 * lik_1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    this.pValue = Math.min(p_0, p_1);
    return this.pValue > alpha;
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution)

Example 4 with ChiSquaredDistribution

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

the class StatUtils method getChiSquareCutoff.

public static double getChiSquareCutoff(double alpha, int df) {
    double low = 0.0;
    double high = 50.0;
    double mid = 25.0;
    ChiSquaredDistribution dist = new ChiSquaredDistribution(df);
    while (high - low > 1e-4) {
        mid = (high + low) / 2.0;
        double _alpha = 2.0 * (1.0 - dist.cumulativeProbability(Math.abs(mid)));
        if (_alpha > alpha) {
            low = mid;
        } else {
            high = mid;
        }
    }
    return mid;
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution)

Example 5 with ChiSquaredDistribution

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

the class LogisticRegression method norm.

private double norm(double z) {
    double q = z * z;
    double piOver2 = Math.PI / 2.0;
    if (Math.abs(q) > 7.0) {
        return (1.0 - 1.0 / q + 3.0 / (q * q)) * Math.exp(-q / 2.0) / (Math.abs(z) * Math.sqrt(piOver2));
    } else {
        return new ChiSquaredDistribution(1).cumulativeProbability(q);
    }
}
Also used : ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution)

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