use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class GFci method search.
// ========================PUBLIC METHODS==========================//
public Graph search() {
long time1 = System.currentTimeMillis();
List<Node> nodes = getIndependenceTest().getVariables();
logger.log("info", "Starting FCI algorithm.");
logger.log("info", "Independence test = " + getIndependenceTest() + ".");
this.graph = new EdgeListGraphSingleConnections(nodes);
Fges fges = new Fges(score);
fges.setKnowledge(getKnowledge());
fges.setVerbose(verbose);
fges.setNumPatternsToStore(0);
fges.setFaithfulnessAssumed(faithfulnessAssumed);
fges.setMaxDegree(maxDegree);
fges.setOut(out);
graph = fges.search();
Graph fgesGraph = new EdgeListGraphSingleConnections(graph);
sepsets = new SepsetsGreedy(fgesGraph, independenceTest, null, maxDegree);
for (Node b : nodes) {
if (Thread.currentThread().isInterrupted()) {
break;
}
List<Node> adjacentNodes = fgesGraph.getAdjacentNodes(b);
if (adjacentNodes.size() < 2) {
continue;
}
ChoiceGenerator cg = new ChoiceGenerator(adjacentNodes.size(), 2);
int[] combination;
while ((combination = cg.next()) != null) {
if (Thread.currentThread().isInterrupted()) {
break;
}
Node a = adjacentNodes.get(combination[0]);
Node c = adjacentNodes.get(combination[1]);
if (graph.isAdjacentTo(a, c) && fgesGraph.isAdjacentTo(a, c)) {
if (sepsets.getSepset(a, c) != null) {
graph.removeEdge(a, c);
}
}
}
}
modifiedR0(fgesGraph);
FciOrient fciOrient = new FciOrient(sepsets);
fciOrient.setVerbose(verbose);
fciOrient.setOut(out);
fciOrient.setKnowledge(getKnowledge());
fciOrient.setCompleteRuleSetUsed(completeRuleSetUsed);
fciOrient.setMaxPathLength(maxPathLength);
fciOrient.doFinalOrientation(graph);
GraphUtils.replaceNodes(graph, independenceTest.getVariables());
long time2 = System.currentTimeMillis();
elapsedTime = time2 - time1;
graph.setPag(true);
return graph;
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class GPc method modifiedR0.
// Due to Spirtes.
public void modifiedR0(Graph fgesGraph) {
graph.reorientAllWith(Endpoint.TAIL);
pcOrientBk(knowledge, graph, graph.getNodes());
List<Node> nodes = graph.getNodes();
for (Node b : nodes) {
List<Node> adjacentNodes = graph.getAdjacentNodes(b);
if (adjacentNodes.size() < 2) {
continue;
}
ChoiceGenerator cg = new ChoiceGenerator(adjacentNodes.size(), 2);
int[] combination;
while ((combination = cg.next()) != null) {
Node a = adjacentNodes.get(combination[0]);
Node c = adjacentNodes.get(combination[1]);
if (fgesGraph.isDefCollider(a, b, c)) {
graph.setEndpoint(a, b, Endpoint.ARROW);
graph.setEndpoint(c, b, Endpoint.ARROW);
} else if (fgesGraph.isAdjacentTo(a, c) && !graph.isAdjacentTo(a, c)) {
List<Node> sepset = sepsets.getSepset(a, c);
if (sepset != null && !sepset.contains(b)) {
graph.setEndpoint(a, b, Endpoint.ARROW);
graph.setEndpoint(c, b, Endpoint.ARROW);
}
}
}
}
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class FciMax method doNode.
private void doNode(Graph graph, Map<Triple, Double> scores, Node b) {
List<Node> adjacentNodes = graph.getAdjacentNodes(b);
if (adjacentNodes.size() < 2) {
return;
}
ChoiceGenerator cg = new ChoiceGenerator(adjacentNodes.size(), 2);
int[] combination;
while ((combination = cg.next()) != null) {
Node a = adjacentNodes.get(combination[0]);
Node c = adjacentNodes.get(combination[1]);
// Skip triples that are shielded.
if (graph.isAdjacentTo(a, c)) {
continue;
}
List<Node> adja = graph.getAdjacentNodes(a);
double score = Double.POSITIVE_INFINITY;
List<Node> S = null;
DepthChoiceGenerator cg2 = new DepthChoiceGenerator(adja.size(), -1);
int[] comb2;
while ((comb2 = cg2.next()) != null) {
List<Node> s = GraphUtils.asList(comb2, adja);
independenceTest.isIndependent(a, c, s);
double _score = independenceTest.getScore();
if (_score < score) {
score = _score;
S = s;
}
}
List<Node> adjc = graph.getAdjacentNodes(c);
DepthChoiceGenerator cg3 = new DepthChoiceGenerator(adjc.size(), -1);
int[] comb3;
while ((comb3 = cg3.next()) != null) {
List<Node> s = GraphUtils.asList(comb3, adjc);
independenceTest.isIndependent(c, a, s);
double _score = independenceTest.getScore();
if (_score < score) {
score = _score;
S = s;
}
}
// S actually has to be non-null here, but the compiler doesn't know that.
if (S != null && !S.contains(b)) {
scores.put(new Triple(a, b, c), score);
}
}
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class FciOrientT method rulesR1R2cycle.
// Does all 3 of these rules at once instead of going through all
// triples multiple times per iteration of doFinalOrientation.
public void rulesR1R2cycle(Graph graph) {
List<Node> nodes = graph.getNodes();
for (Node B : nodes) {
List<Node> adj = graph.getAdjacentNodes(B);
if (adj.size() < 2) {
continue;
}
ChoiceGenerator cg = new ChoiceGenerator(adj.size(), 2);
int[] combination;
while ((combination = cg.next()) != null) {
Node A = adj.get(combination[0]);
Node C = adj.get(combination[1]);
// choice gen doesnt do diff orders, so must switch A & C around.
ruleR1(A, B, C, graph);
ruleR1(C, B, A, graph);
ruleR2(A, B, C, graph);
ruleR2(C, B, A, graph);
}
}
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class FciOrientT method ruleR6R7.
/**
* Implements Zhang's rules R6 and R7, applies them over the graph once. Orient single tails. R6: If A---Bo-*C then
* A---B--*C. R7: If A--oBo-*C and A,C nonadjacent, then A--oB--*C
*/
public void ruleR6R7(Graph graph) {
List<Node> nodes = graph.getNodes();
for (Node b : nodes) {
List<Node> adjacents = graph.getAdjacentNodes(b);
if (adjacents.size() < 2)
continue;
ChoiceGenerator cg = new ChoiceGenerator(adjacents.size(), 2);
for (int[] choice = cg.next(); choice != null; choice = cg.next()) {
Node a = adjacents.get(choice[0]);
Node c = adjacents.get(choice[1]);
if (graph.isAdjacentTo(a, c))
continue;
if (!(graph.getEndpoint(b, a) == Endpoint.TAIL))
continue;
if (!(graph.getEndpoint(c, b) == Endpoint.CIRCLE))
continue;
if (graph.getEndpoint(a, b) == Endpoint.TAIL) {
// We know A---Bo-*C: R6 applies!
graph.setEndpoint(c, b, Endpoint.TAIL);
logger.log("impliedOrientations", SearchLogUtils.edgeOrientedMsg("Single tails (tail)", graph.getEdge(c, b)));
changeFlag = true;
}
if (graph.getEndpoint(a, b) == Endpoint.CIRCLE) {
// if (graph.isAdjacentTo(a, c)) continue;
logger.log("impliedOrientations", SearchLogUtils.edgeOrientedMsg("Single tails (tail)", graph.getEdge(c, b)));
// We know A--oBo-*C and A,C nonadjacent: R7 applies!
graph.setEndpoint(c, b, Endpoint.TAIL);
changeFlag = true;
}
}
}
}
Aggregations