use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class ProbFci method rulesR1R2cycle.
// Does all 3 of these rules at once instead of going through all
// triples multiple times per iteration of doFinalOrientation.
private void rulesR1R2cycle() {
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);
ruleR1(C, B, A);
ruleR2(A, B, C);
ruleR2(C, B, A);
// awayFromCycle(A, B, C);
// awayFromCycle(C, B, A);
}
}
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class OrientCollidersMaxP 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) {
if (Thread.currentThread().isInterrupted()) {
break;
}
Node a = adjacentNodes.get(combination[0]);
Node c = adjacentNodes.get(combination[1]);
// Skip triples that are shielded.
if (graph.isAdjacentTo(a, c)) {
continue;
}
if (useHeuristic) {
if (existsShortPath(a, c, maxPathLength, graph)) {
testColliderMaxP(graph, scores, a, b, c);
} else {
testColliderHeuristic(graph, scores, a, b, c);
}
} else {
testColliderMaxP(graph, scores, a, b, c);
}
}
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class PcAll method orientUnshieldedTriplesConservative.
private void orientUnshieldedTriplesConservative(IKnowledge knowledge) {
TetradLogger.getInstance().log("info", "Starting Collider Orientation:");
colliderTriples = new HashSet<>();
noncolliderTriples = new HashSet<>();
ambiguousTriples = new HashSet<>();
List<Node> nodes = graph.getNodes();
for (Node y : nodes) {
List<Node> adjacentNodes = graph.getAdjacentNodes(y);
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 x = adjacentNodes.get(combination[0]);
Node z = adjacentNodes.get(combination[1]);
if (this.graph.isAdjacentTo(x, z)) {
continue;
}
List<List<Node>> sepsetsxz = getSepsets(x, z, graph);
if (isColliderSepset(y, sepsetsxz)) {
if (colliderAllowed(x, y, z, knowledge)) {
orientCollider(x, y, z, conflictRule, graph);
}
colliderTriples.add(new Triple(x, y, z));
} else if (isNoncolliderSepset(y, sepsetsxz)) {
noncolliderTriples.add(new Triple(x, y, z));
} else {
Triple triple = new Triple(x, y, z);
ambiguousTriples.add(triple);
graph.addAmbiguousTriple(triple.getX(), triple.getY(), triple.getZ());
}
getAllTriples().add(new Triple(x, y, z));
}
}
TetradLogger.getInstance().log("info", "Finishing Collider Orientation.");
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class PcAll method getSepsets.
private List<List<Node>> getSepsets(Node i, Node k, Graph g) {
List<Node> adji = g.getAdjacentNodes(i);
List<Node> adjk = g.getAdjacentNodes(k);
List<List<Node>> sepsets = new ArrayList<>();
for (int d = 0; d <= Math.max(adji.size(), adjk.size()); d++) {
if (adji.size() >= 2 && d <= adji.size()) {
ChoiceGenerator gen = new ChoiceGenerator(adji.size(), d);
int[] choice;
while ((choice = gen.next()) != null) {
if (Thread.currentThread().isInterrupted()) {
break;
}
List<Node> v = GraphUtils.asList(choice, adji);
if (getIndependenceTest().isIndependent(i, k, v))
sepsets.add(v);
}
}
if (adjk.size() >= 2 && d <= adjk.size()) {
ChoiceGenerator gen = new ChoiceGenerator(adjk.size(), d);
int[] choice;
while ((choice = gen.next()) != null) {
if (Thread.currentThread().isInterrupted()) {
break;
}
List<Node> v = GraphUtils.asList(choice, adjk);
if (getIndependenceTest().isIndependent(i, k, v))
sepsets.add(v);
}
}
}
return sepsets;
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class MimAdjacencySearch method adjStep.
/**
* Removes edges from the a graph based on conditional independence facts using conditioning sets of size n and
* records these conditioning set ("separating sets"). More specifically, for every node x and node y (connected to
* x), and every set s of size n of nodes connected to x (not including y), checks if x and y are independent
* conditional on s and, if so, removes the edge from x to y from the graph and records the separating set used to
* remove that edge.
*
* @param knowledge background knowledge
* @param n the size of sets to check.
* @param graph the graph being checked.
* @param ind the independence checker.
* @param sepset As edges are removed, their separating sets (the sets conditional on which their endpoints are
* independent) are recorded in this sepset. This is for use later on in the algorithm.
* @return false if no independencies are found for this step. (This means we are finished with adjacency search.)
*/
private boolean adjStep(Graph graph, IndependenceTest ind, IKnowledge knowledge, SepsetMap sepset, int n) {
// Note: as a stateful object, all of these arguments should belong
// to the state. There is no need to pass them in every time.
Iterator<Node> it = latents.iterator();
boolean result = false;
// list of visited nodes
List<Node> visited = new LinkedList<>();
// for each node x...
while (it.hasNext()) {
Node nodeX = it.next();
Set<Node> set = new HashSet<>();
for (Node node : graph.getAdjacentNodes(nodeX)) {
if (latents.contains(node)) {
set.add(node);
}
}
// trim all unnecessary comapres, we have already visited them
for (Node aVisited : visited) {
set.remove(aVisited);
}
visited.add(nodeX);
Iterator<Node> it1 = (new HashSet<>(set)).iterator();
// for each node y connected to x ...
while (it1.hasNext()) {
Node nodeY = it1.next();
Set<String> set1 = new HashSet<>();
for (Node node : graph.getAdjacentNodes(nodeX)) {
if (latents.contains(node)) {
set1.add(node.toString());
}
}
// find parents of Y
Set<String> set2 = new HashSet<>();
for (Node node : graph.getAdjacentNodes(nodeY)) {
if (latents.contains(node)) {
set2.add(node.toString());
}
}
set1.addAll(set2);
set1.remove(nodeY.toString());
set1.remove(nodeX.toString());
// seta: all nodes adjacent to x other than y and come
// before x or before y temporally
Object[] seta = forbidFilter(set1, nodeX.getName(), knowledge);
if (seta.length >= n) {
result = true;
ChoiceGenerator cg = new ChoiceGenerator(seta.length, n);
int[] subset;
// for each subset of size n ...
while ((subset = cg.next()) != null) {
List<Node> condSet = asList(subset, seta);
if (ind.isIndependent(nodeX, nodeY, condSet) && knowledge.noEdgeRequired(nodeX.getName(), nodeY.getName())) {
// double pValue = ind.getLikelihoodRatioP();
// SearchLogUtils.logIndependenceFact(nodeX, nodeY, condSet,
// pValue, LOGGER);
set.remove(nodeY);
graph.removeEdge(nodeX, nodeY);
sepset.set(nodeX, nodeY, new LinkedList(condSet));
break;
}
}
}
}
}
return result;
}
Aggregations