Search in sources :

Example 1 with IndependenceFact

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

the class IndTestProbabilisticVerbose method isIndependent.

@Override
public boolean isIndependent(Node x, Node y, Node... z) {
    IndependenceFact key = new IndependenceFact(x, y, z);
    if (!H.containsKey(key)) {
        double pInd = probConstraint(BCInference.OP.independent, x, y, z);
        H.put(key, pInd);
    }
    double pInd = H.get(key);
    double p = probOp(BCInference.OP.independent, pInd);
    this.posterior = p;
    // boolean ind = RandomUtil.getInstance().nextDouble() < p;
    boolean dsep = dsepTest.isIndependent(x, y, z);
    out.print((nodes.indexOf(x) + 1) + " ");
    out.print((nodes.indexOf(y) + 1) + (z.length > 0 ? " " : ""));
    for (int i = 0; i < z.length; i++) {
        out.print(nodes.indexOf(z[i]) + 1);
        if (i < z.length - 1) {
            out.print(" ");
        }
    }
    out.print(",");
    out.print((dsep ? 1 : 0) + ",");
    out.print(p);
    out.println();
    if (dsep) {
        return true;
    } else {
        return false;
    }
}
Also used : IndependenceFact(edu.cmu.tetrad.graph.IndependenceFact)

Example 2 with IndependenceFact

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

the class IndTestProbabilistic method isIndependent.

@Override
public boolean isIndependent(Node x, Node y, Node... z) {
    IndependenceFact key = new IndependenceFact(x, y, z);
    if (!H.containsKey(key)) {
        double pInd = probConstraint(BCInference.OP.independent, x, y, z);
        H.put(key, pInd);
    }
    double pInd = H.get(key);
    double p = probOp(BCInference.OP.independent, pInd);
    this.posterior = p;
    boolean ind = RandomUtil.getInstance().nextDouble() < p;
    System.out.print((nodes.indexOf(x) + 1) + " ");
    System.out.print((nodes.indexOf(y) + 1) + (z.length > 0 ? " " : ""));
    for (int i = 0; i < z.length; i++) {
        System.out.print(nodes.indexOf(z[i]) + 1);
        if (i < z.length - 1) {
            System.out.print(" ");
        }
    }
    System.out.print(",");
    System.out.print(ind ? 1 : 0 + ",");
    System.out.print(p);
    System.out.println();
    if (ind) {
        return true;
    } else {
        return false;
    }
}
Also used : IndependenceFact(edu.cmu.tetrad.graph.IndependenceFact)

Example 3 with IndependenceFact

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

the class IndependenceFacts method getVariables.

public List<Node> getVariables() {
    Set<Node> variables = new HashSet<>();
    for (IndependenceFact fact : unsortedFacts) {
        variables.add(fact.getX());
        variables.add(fact.getY());
        for (Node z : fact.getZ()) {
            variables.add(z);
        }
    }
    return new ArrayList<>(variables);
}
Also used : Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) IndependenceFact(edu.cmu.tetrad.graph.IndependenceFact) HashSet(java.util.HashSet)

Example 4 with IndependenceFact

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

the class IndependenceFacts method isIndependent.

public boolean isIndependent(Node x, Node y, List<Node> z) {
    IndependenceFact fact = new IndependenceFact(x, y, z);
    System.out.println("Looking up " + fact + " in " + unsortedFacts);
    return unsortedFacts.contains(fact);
}
Also used : IndependenceFact(edu.cmu.tetrad.graph.IndependenceFact)

Example 5 with IndependenceFact

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

the class TestIndependenceFacts method test1.

@Test
public void test1() {
    IndependenceFactsModel facts = new IndependenceFactsModel();
    Node x1 = new GraphNode("X1");
    Node x2 = new GraphNode("X2");
    Node x3 = new GraphNode("X3");
    Node x4 = new GraphNode("X4");
    Node x5 = new GraphNode("X5");
    Node x6 = new GraphNode("X6");
    facts.add(new IndependenceFact(x1, x2, x3));
    facts.add(new IndependenceFact(x2, x3));
    facts.add(new IndependenceFact(x2, x4, x1, x2));
    facts.add(new IndependenceFact(x2, x4, x1, x3, x5));
    facts.add(new IndependenceFact(x2, x4, x3));
    facts.add(new IndependenceFact(x2, x4, x3, x6));
    facts.remove(new IndependenceFact(x1, x2, x3));
    IndependenceFacts _facts = new IndependenceFacts(facts.getFacts());
    assertTrue(_facts.isIndependent(x4, x2, x1, x2));
    assertTrue(_facts.isIndependent(x4, x2, x5, x3, x1));
    List<Node> l = new ArrayList<>();
    l.add(x1);
    l.add(x2);
    assertTrue(_facts.isIndependent(x4, x2, l));
}
Also used : IndependenceFacts(edu.cmu.tetrad.data.IndependenceFacts) IndependenceFactsModel(edu.cmu.tetradapp.model.IndependenceFactsModel) GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) GraphNode(edu.cmu.tetrad.graph.GraphNode) IndependenceFact(edu.cmu.tetrad.graph.IndependenceFact) Test(org.junit.Test)

Aggregations

IndependenceFact (edu.cmu.tetrad.graph.IndependenceFact)10 Node (edu.cmu.tetrad.graph.Node)7 GraphNode (edu.cmu.tetrad.graph.GraphNode)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)2 IndependenceFacts (edu.cmu.tetrad.data.IndependenceFacts)1 IndependenceFactsModel (edu.cmu.tetradapp.model.IndependenceFactsModel)1 BufferedReader (java.io.BufferedReader)1 NumberFormat (java.text.NumberFormat)1 HashSet (java.util.HashSet)1