Search in sources :

Example 76 with Node

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

the class IndTestFisherZD method indTestSubset.

// ==========================PUBLIC METHODS=============================//
/**
 * Creates a new independence test 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] = indexMap.get(vars.get(i));
    }
    ICovarianceMatrix newCovMatrix = covMatrix.getSubmatrix(indices);
    double alphaNew = getAlpha();
    return new IndTestFisherZD(newCovMatrix, alphaNew);
}
Also used : ICovarianceMatrix(edu.cmu.tetrad.data.ICovarianceMatrix) Node(edu.cmu.tetrad.graph.Node)

Example 77 with Node

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

the class IndTestFisherZD 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 78 with Node

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

the class IndTestFisherZGeneralizedInverse method determines.

public boolean determines(List<Node> zList, Node xVar) {
    if (zList == null) {
        throw new NullPointerException();
    }
    if (zList.isEmpty()) {
        return false;
    }
    for (Node node : zList) {
        if (node == null) {
            throw new NullPointerException();
        }
    }
    int size = zList.size();
    int[] zCols = new int[size];
    int xIndex = getVariables().indexOf(xVar);
    for (int i = 0; i < zList.size(); i++) {
        zCols[i] = getVariables().indexOf(zList.get(i));
    }
    int[] zRows = new int[data.rows()];
    for (int i = 0; i < data.rows(); i++) {
        zRows[i] = i;
    }
    DoubleMatrix2D Z = data.viewSelection(zRows, zCols);
    DoubleMatrix1D x = data.viewColumn(xIndex);
    DoubleMatrix2D Zt = new Algebra().transpose(Z);
    DoubleMatrix2D ZtZ = new Algebra().mult(Zt, Z);
    TetradMatrix _ZtZ = new TetradMatrix(ZtZ.toArray());
    TetradMatrix ginverse = _ZtZ.inverse();
    DoubleMatrix2D G = new DenseDoubleMatrix2D(ginverse.toArray());
    // DoubleMatrix2D G = MatrixUtils.ginverse(ZtZ);
    DoubleMatrix2D Zt2 = Zt.copy();
    DoubleMatrix2D GZt = new Algebra().mult(G, Zt2);
    DoubleMatrix1D b_x = new Algebra().mult(GZt, x);
    DoubleMatrix1D xPred = new Algebra().mult(Z, b_x);
    DoubleMatrix1D xRes = xPred.copy().assign(x, Functions.minus);
    double SSE = xRes.aggregate(Functions.plus, Functions.square);
    double variance = SSE / (data.rows() - (zList.size() + 1));
    // ChiSquare chiSquare = new ChiSquare(data.rows(),
    // PersistentRandomUtil.getInstance().getEngine());
    // 
    // double p = chiSquare.cdf(sum);
    // boolean determined = p < 1 - getAlternativePenalty();
    // 
    boolean determined = variance < getAlpha();
    if (determined) {
        StringBuilder sb = new StringBuilder();
        sb.append("Determination found: ").append(xVar).append(" is determined by {");
        for (int i = 0; i < zList.size(); i++) {
            sb.append(zList.get(i));
            if (i < zList.size() - 1) {
                sb.append(", ");
            }
        }
        sb.append("}");
        // sb.append(" p = ").append(nf.format(p));
        sb.append(" SSE = ").append(nf.format(SSE));
        TetradLogger.getInstance().log("independencies", sb.toString());
        System.out.println(sb);
    }
    return determined;
}
Also used : Algebra(cern.colt.matrix.linalg.Algebra) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) Node(edu.cmu.tetrad.graph.Node) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D)

Example 79 with Node

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

the class GrowShrink method findMb.

/**
 * Finds the Markov blanket of the given target.
 *
 * @param targetName the name of the target
 * @return the list of node in the Markov blanket.
 */
public List<Node> findMb(String targetName) {
    Node target = getVariableForName(targetName);
    List<Node> blanket = new LinkedList<>();
    boolean changed = true;
    while (changed) {
        changed = false;
        List<Node> remaining = new LinkedList<>(variables);
        remaining.removeAll(blanket);
        remaining.remove(target);
        for (Node node : remaining) {
            if (!independenceTest.isIndependent(node, target, blanket)) {
                blanket.add(node);
                changed = true;
            }
        }
    }
    changed = true;
    while (changed) {
        changed = false;
        for (Node node : new LinkedList<>(blanket)) {
            blanket.remove(node);
            if (independenceTest.isIndependent(node, target, blanket)) {
                changed = true;
                continue;
            }
            blanket.add(node);
        }
    }
    return blanket;
}
Also used : Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList)

Example 80 with Node

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

the class IndTestRegression method isIndependent.

/**
 * Determines whether variable x is independent of variable y given a list of conditioning variables z.
 *
 * @param xVar  the one variable being compared.
 * @param yVar  the second variable being compared.
 * @param zList the list of conditioning variables.
 * @return true iff x _||_ y | z.
 * @throws RuntimeException if a matrix singularity is encountered.
 */
public boolean isIndependent(Node xVar, Node yVar, List<Node> zList) {
    if (zList == null) {
        throw new NullPointerException();
    }
    for (Node node : zList) {
        if (node == null) {
            throw new NullPointerException();
        }
    }
    List<Node> regressors = new ArrayList<>();
    regressors.add(dataSet.getVariable(yVar.getName()));
    for (Node zVar : zList) {
        regressors.add(dataSet.getVariable(zVar.getName()));
    }
    Regression regression = new RegressionDataset(dataSet);
    RegressionResult result = null;
    try {
        result = regression.regress(xVar, regressors);
    } catch (Exception e) {
        return false;
    }
    double p = result.getP()[1];
    boolean independent = p > alpha;
    if (verbose) {
        if (independent) {
            TetradLogger.getInstance().log("independencies", SearchLogUtils.independenceFactMsg(xVar, yVar, zList, p));
        } else {
            TetradLogger.getInstance().log("dependencies", SearchLogUtils.dependenceFactMsg(xVar, yVar, zList, p));
        }
    }
    return independent;
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Regression(edu.cmu.tetrad.regression.Regression) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

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