Search in sources :

Example 41 with Node

use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.

the class GraphScore method aBetterScore.

private double aBetterScore(int x, int y, int[] z) {
    Node _y = variables.get(y);
    Node _x = variables.get(x);
    List<Node> _z = getVariableList(z);
    boolean dsep = dag.isDSeparatedFrom(_x, _y, _z);
    int count = 0;
    if (!dsep)
        count++;
    for (Node z0 : _z) {
        if (dag.isDSeparatedFrom(_x, z0, _z)) {
            count += 1;
        }
    }
    double score = dsep ? -1 - count : 1 + count;
    // if (score == 1) score -= Math.tanh(z.length);
    return score;
}
Also used : Node(edu.cmu.tetrad.graph.Node)

Example 42 with Node

use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.

the class IndTestMNLRLRT method getVariableNames.

/**
 * @return the list of variable varNames.
 */
public List<String> getVariableNames() {
    List<Node> variables = getVariables();
    List<String> variableNames = new ArrayList<>();
    for (Node variable1 : variables) {
        variableNames.add(variable1.getName());
    }
    return variableNames;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList)

Example 43 with Node

use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.

the class IndTestMixedMultipleTTest method multiLL.

private double multiLL(DoubleMatrix2D coeffs, Node dep, List<Node> indep) {
    DoubleMatrix2D indepData = factory2D.make(internalData.subsetColumns(indep).getDoubleData().toArray());
    List<Node> depList = new ArrayList<>();
    depList.add(dep);
    DoubleMatrix2D depData = factory2D.make(internalData.subsetColumns(depList).getDoubleData().toArray());
    int N = indepData.rows();
    DoubleMatrix2D probs = Algebra.DEFAULT.mult(factory2D.appendColumns(factory2D.make(N, 1, 1.0), indepData), coeffs);
    probs = factory2D.appendColumns(factory2D.make(indepData.rows(), 1, 1.0), probs).assign(Functions.exp);
    double ll = 0;
    for (int i = 0; i < N; i++) {
        DoubleMatrix1D curRow = probs.viewRow(i);
        curRow.assign(Functions.div(curRow.zSum()));
        ll += Math.log(curRow.get((int) depData.get(i, 0)));
    }
    return ll;
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) Node(edu.cmu.tetrad.graph.Node) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D)

Example 44 with Node

use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.

the class IndTestMultinomialLogisticRegression method getVariableNames.

/**
 * @return the list of variable varNames.
 */
public List<String> getVariableNames() {
    List<Node> variables = getVariables();
    List<String> variableNames = new ArrayList<>();
    for (Node variable1 : variables) {
        variableNames.add(variable1.getName());
    }
    return variableNames;
}
Also used : Node(edu.cmu.tetrad.graph.Node)

Example 45 with Node

use of edu.cmu.tetrad.graph.Node 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)

Aggregations

Node (edu.cmu.tetrad.graph.Node)674 ArrayList (java.util.ArrayList)129 Graph (edu.cmu.tetrad.graph.Graph)106 GraphNode (edu.cmu.tetrad.graph.GraphNode)64 DataSet (edu.cmu.tetrad.data.DataSet)59 LinkedList (java.util.LinkedList)55 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)48 Test (org.junit.Test)48 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)46 List (java.util.List)45 Dag (edu.cmu.tetrad.graph.Dag)41 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)41 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)40 ChoiceGenerator (edu.cmu.tetrad.util.ChoiceGenerator)37 Endpoint (edu.cmu.tetrad.graph.Endpoint)29 DisplayNode (edu.cmu.tetradapp.workbench.DisplayNode)26 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)25 Edge (edu.cmu.tetrad.graph.Edge)23 SemIm (edu.cmu.tetrad.sem.SemIm)19 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)19