Search in sources :

Example 66 with Node

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

the class IndTestChiSquare method determines.

/**
 * @param z  The list of variables z1,...,zn with respect to which we want to know whether z determines x oir z.
 * @param x1 The one variable whose determination by z we want to know.
 * @return true if it is estimated that z determines x or z determines y.
 */
public boolean determines(List<Node> z, Node x1) {
    if (z == null) {
        throw new NullPointerException();
    }
    for (Node aZ : z) {
        if (aZ == null) {
            throw new NullPointerException();
        }
    }
    // For testing x, y given z1,...,zn, set up an array of length
    // n + 2 containing the indices of these variables in order.
    int[] testIndices = new int[1 + z.size()];
    testIndices[0] = variables.indexOf(x1);
    for (int i = 0; i < z.size(); i++) {
        testIndices[i + 1] = variables.indexOf(z.get(i));
    }
    // the following is lame code--need a better test
    for (int i = 0; i < testIndices.length; i++) {
        if (testIndices[i] < 0) {
            throw new IllegalArgumentException("Variable " + i + "was not used in the constructor.");
        }
    }
    // System.out.println("Testing " + x + " _||_ " + y + " | " + z);
    boolean countDetermined = chiSquareTest.isDetermined(testIndices, getDeterminationP());
    if (countDetermined) {
        StringBuilder sb = new StringBuilder();
        sb.append("Determination found: ").append(x1).append(" is determined by {");
        for (int i = 0; i < z.size(); i++) {
            sb.append(z.get(i));
            if (i < z.size() - 1) {
                sb.append(", ");
            }
        }
        sb.append("}");
        TetradLogger.getInstance().log("independencies", sb.toString());
    }
    return countDetermined;
}
Also used : Node(edu.cmu.tetrad.graph.Node)

Example 67 with Node

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

the class IndTestChiSquare 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 68 with Node

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

the class IndTestConditionalGaussianLRT 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 69 with Node

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

the class IndTestCramerT method indTestSubset.

// ==========================PUBLIC METHODS=============================//
/**
 * Creates a new IndTestCramerT instance for a subset of the variables.
 */
public IndependenceTest indTestSubset(List<Node> vars) {
    if (vars.isEmpty()) {
        throw new IllegalArgumentException("Subset may not be empty.");
    }
    for (Node var : vars) {
        if (!variables.contains(var)) {
            throw new IllegalArgumentException("All vars must be original vars");
        }
    }
    int[] indices = new int[vars.size()];
    for (int i = 0; i < indices.length; i++) {
        indices[i] = variables.indexOf(vars.get(i));
    }
    ICovarianceMatrix newCorrMatrix = covMatrix.getSubmatrix(indices);
    double alphaNew = getAlpha();
    return new IndTestCramerT(newCorrMatrix, alphaNew);
}
Also used : ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) Node(edu.cmu.tetrad.graph.Node)

Example 70 with Node

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

the class IndTestCramerT method isIndependent.

/**
 * Determines whether variable x is independent of variable y given a list of conditioning variables z.
 *
 * @param x the one variable being compared.
 * @param y the second variable being compared.
 * @param z the list of conditioning variables.
 * @return true iff x _||_ y | z.
 * @throws RuntimeException if a matrix singularity is encountered.
 */
public boolean isIndependent(Node x, Node y, List<Node> z) {
    if (z == null) {
        throw new NullPointerException();
    }
    for (Node node : z) {
        if (node == null) {
            throw new NullPointerException();
        }
    }
    // Precondition: this.variables, this.corrMatrix properly set up.
    // 
    // Postcondition: this.storedR and this.func should be the
    // most recently calculated partial correlation and
    // partial correlation distribution function, respectively.
    // 
    // PROCEDURE:
    // calculate the partial correlation of x and y given z
    // by finding the submatrix of variables x, y, z1...zn,
    // inverting it, and examining the value at position (0, 1)
    // in the inverted matrix.  the partial correlation is
    // -1 * this value / the square root of the outerProduct of the
    // diagonal elements on the same row and column as this
    // value.
    // 
    // Design consideration:
    // Minimize object creation by reusing submatrix and condition
    // arrays and inverting submatrix in place.
    // Create index array for the given variables.
    int size = z.size() + 2;
    int[] indices = new int[size];
    indices[0] = getVariables().indexOf(x);
    indices[1] = getVariables().indexOf(y);
    for (int i = 0; i < z.size(); i++) {
        indices[i + 2] = getVariables().indexOf(z.get(i));
    }
    // Extract submatrix of correlation matrix using this index array.
    TetradMatrix submatrix = covMatrix().getMatrix().getSelection(indices, indices);
    // Check for missing values.
    if (DataUtils.containsMissingValue(submatrix)) {
        throw new IllegalArgumentException("Please remove or impute missing values first.");
    }
    // Invert submatrix.
    if (submatrix.rank() != submatrix.rows()) {
        // if (TetradAlgebra.rank(submatrix) != submatrix.rows()) {
        throw new IllegalArgumentException("Matrix singularity detected while using correlations " + "\nto check for independence; probably due to collinearity " + "\nin the data. The independence fact being checked was " + "\n" + x + " _||_ " + y + " | " + z + ".");
    }
    submatrix = submatrix.inverse();
    double a = -1.0 * submatrix.get(0, 1);
    double b = Math.sqrt(submatrix.get(0, 0) * submatrix.get(1, 1));
    // Store R so P value can be calculated.
    this.storedR = a / b;
    if (Math.abs(storedR) > 1) {
        storedR = Math.signum(storedR);
    }
    if (Double.isNaN(this.storedR)) {
        throw new IllegalArgumentException("Conditional correlation cannot be computed: " + SearchLogUtils.independenceFact(x, y, z));
    }
    // Determine whether this partial correlation is statistically
    // nondifferent from zero.
    boolean independent = isZero(this.storedR, size, getAlpha());
    double pValue = getPValue();
    if (verbose) {
        if (independent) {
            TetradLogger.getInstance().log("independencies", SearchLogUtils.independenceFactMsg(x, y, z, pValue));
        } else {
            TetradLogger.getInstance().log("dependencies", SearchLogUtils.dependenceFactMsg(x, y, z, pValue));
        }
    }
    return independent;
}
Also used : Node(edu.cmu.tetrad.graph.Node)

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