use of edu.cmu.tetrad.util.DepthChoiceGenerator in project tetrad by cmu-phil.
the class Mbfs method search.
/**
* Searches for the MB Pattern for the given target.
*
* @param target The target variable.
*/
public Graph search(Node target) {
long start = System.currentTimeMillis();
this.numIndependenceTests = 0;
this.allTriples = new HashSet<>();
this.ambiguousTriples = new HashSet<>();
this.colliderTriples = new HashSet<>();
this.noncolliderTriples = new HashSet<>();
if (target == null) {
throw new IllegalArgumentException("Null target name not permitted");
}
this.target = target;
logger.log("info", "Target = " + target);
// Some statistics.
this.maxRemainingAtDepth = new int[20];
this.maxVariableAtDepth = new Node[20];
Arrays.fill(maxRemainingAtDepth, -1);
Arrays.fill(maxVariableAtDepth, null);
logger.log("info", "target = " + getTarget());
Graph graph = new EdgeListGraph();
// Each time the addDepthZeroAssociates method is called for a node
// v, v is added to this set, and edges to elements in this set are
// not added to the graph subsequently. This is to handle the situation
// of adding v1---v2 to the graph, removing it by conditioning on
// nodes adjacent to v1, then re-adding it and not being able to
// remove it by conditioning on nodes adjacent to v2. Once an edge
// is removed, it should not be re-added to the graph.
// jdramsey 8/6/04
this.a = new HashSet<>();
// Step 1. Get associates for the target.
logger.log("info", "BEGINNING step 1 (prune target).");
graph.addNode(getTarget());
constructFan(getTarget(), graph);
logger.log("graph", "After step 1 (prune target)" + graph);
logger.log("graph", "After step 1 (prune target)" + graph);
// Step 2. Get associates for each variable adjacent to the target,
// removing edges based on those associates where possible. After this
// step, adjacencies to the target are parents or children of the target.
// Call this set PC.
logger.log("info", "BEGINNING step 2 (prune PC).");
for (Node v : graph.getAdjacentNodes(getTarget())) {
if (Thread.currentThread().isInterrupted()) {
break;
}
constructFan(v, graph);
W: for (Node w : graph.getAdjacentNodes(v)) {
if (Thread.currentThread().isInterrupted()) {
break;
}
if (a.contains(w)) {
continue;
}
List _a = new LinkedList<>(a);
_a.retainAll(graph.getAdjacentNodes(w));
if (_a.size() > 1)
continue;
List<Node> adjT = graph.getAdjacentNodes(getTarget());
DepthChoiceGenerator cg = new DepthChoiceGenerator(adjT.size(), depth);
int[] choice;
while ((choice = cg.next()) != null) {
if (Thread.currentThread().isInterrupted()) {
break;
}
List<Node> s = GraphUtils.asList(choice, adjT);
if (!s.contains(v))
continue;
if (independent(getTarget(), w, s)) {
graph.removeEdge(v, w);
continue W;
}
}
}
}
logger.log("graph", "After step 2 (prune PC)" + graph);
// Step 3. Get associates for each node now two links away from the
// target, removing edges based on those associates where possible.
// After this step, adjacencies to adjacencies of the target are parents
// or children of adjacencies to the target. Call this set PCPC.
logger.log("info", "BEGINNING step 3 (prune PCPC).");
for (Node v : graph.getAdjacentNodes(getTarget())) {
for (Node w : graph.getAdjacentNodes(v)) {
if (getA().contains(w)) {
continue;
}
constructFan(w, graph);
}
}
logger.log("graph", "After step 3 (prune PCPC)" + graph);
logger.log("info", "BEGINNING step 4 (PC Orient).");
SearchGraphUtils.pcOrientbk(knowledge, graph, graph.getNodes());
List<Node> _visited = new LinkedList<>(getA());
orientUnshieldedTriples(knowledge, graph, getTest(), getDepth(), _visited);
MeekRules meekRules = new MeekRules();
meekRules.setAggressivelyPreventCycles(this.aggressivelyPreventCycles);
meekRules.setKnowledge(knowledge);
meekRules.orientImplied(graph);
logger.log("graph", "After step 4 (PC Orient)" + graph);
logger.log("info", "BEGINNING step 5 (Trim graph to {T} U PC U " + "{Parents(Children(T))}).");
MbUtils.trimToMbNodes(graph, getTarget(), false);
logger.log("graph", "After step 5 (Trim graph to {T} U PC U {Parents(Children(T))})" + graph);
logger.log("info", "BEGINNING step 6 (Remove edges among P and P of C).");
MbUtils.trimEdgesAmongParents(graph, getTarget());
MbUtils.trimEdgesAmongParentsOfChildren(graph, getTarget());
logger.log("graph", "After step 6 (Remove edges among P and P of C)" + graph);
// logger.log("details", "Bounds: ");
//
// for (int i = 0; i < maxRemainingAtDepth.length; i++) {
// if (maxRemainingAtDepth[i] != -1) {
// logger.log("details", "\ta" + i + " = " + maxRemainingAtDepth[i] +
// " (" + maxVariableAtDepth[i] + ")");
// }
// }
// System.out.println("Number of fan constructions = " + visited.size());
finishUp(start, graph);
this.logger.log("graph", "\nReturning this graph: " + graph);
this.graph = graph;
return graph;
}
use of edu.cmu.tetrad.util.DepthChoiceGenerator in project tetrad by cmu-phil.
the class SemBicScore method printMinimalLinearlyDependentSet.
// Prints a smallest subset of parents that causes a singular matrix exception.
private boolean printMinimalLinearlyDependentSet(int[] parents, ICovarianceMatrix cov) {
List<Node> _parents = new ArrayList<>();
for (int p : parents) _parents.add(variables.get(p));
DepthChoiceGenerator gen = new DepthChoiceGenerator(_parents.size(), _parents.size());
int[] choice;
while ((choice = gen.next()) != null) {
int[] sel = new int[choice.length];
List<Node> _sel = new ArrayList<>();
for (int m = 0; m < choice.length; m++) {
sel[m] = parents[m];
_sel.add(variables.get(sel[m]));
}
TetradMatrix m = cov.getSelection(sel, sel);
try {
m.inverse();
} catch (Exception e2) {
forbidden.add(sel[0]);
out.println("### Linear dependence among variables: " + _sel);
out.println("### Removing " + _sel.get(0));
return true;
}
}
return false;
}
use of edu.cmu.tetrad.util.DepthChoiceGenerator in project tetrad by cmu-phil.
the class MixedBicScore method printMinimalLinearlyDependentSet.
// Prints a smallest subset of parents that causes a singular matrix exception.
private boolean printMinimalLinearlyDependentSet(int[] parents, ICovarianceMatrix cov) {
List<Node> _parents = new ArrayList<>();
for (int p : parents) _parents.add(variables.get(p));
DepthChoiceGenerator gen = new DepthChoiceGenerator(_parents.size(), _parents.size());
int[] choice;
while ((choice = gen.next()) != null) {
int[] sel = new int[choice.length];
List<Node> _sel = new ArrayList<>();
for (int m = 0; m < choice.length; m++) {
sel[m] = parents[m];
_sel.add(variables.get(sel[m]));
}
TetradMatrix m = cov.getSelection(sel, sel);
try {
m.inverse();
} catch (Exception e2) {
forbidden.add(sel[0]);
out.println("### Linear dependence among variables: " + _sel);
out.println("### Removing " + _sel.get(0));
return true;
}
}
return false;
}
use of edu.cmu.tetrad.util.DepthChoiceGenerator in project tetrad by cmu-phil.
the class OrientCollidersMaxP method testColliderMaxP.
private void testColliderMaxP(Graph graph, Map<Triple, Double> scores, Node a, Node b, Node c) {
List<Node> adja = graph.getAdjacentNodes(a);
List<Node> adjc = graph.getAdjacentNodes(c);
adja.remove(c);
adjc.remove(a);
double score = Double.POSITIVE_INFINITY;
List<Node> S = null;
DepthChoiceGenerator cg1 = new DepthChoiceGenerator(adja.size(), -1);
int[] comb2;
while ((comb2 = cg1.next()) != null) {
if (Thread.currentThread().isInterrupted()) {
break;
}
List<Node> s = GraphUtils.asList(comb2, adja);
independenceTest.isIndependent(a, c, s);
double _score = independenceTest.getScore();
if (_score < score) {
score = _score;
S = s;
}
}
DepthChoiceGenerator cg2 = new DepthChoiceGenerator(adjc.size(), -1);
int[] comb3;
while ((comb3 = cg2.next()) != null) {
if (Thread.currentThread().isInterrupted()) {
break;
}
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.DepthChoiceGenerator in project tetrad by cmu-phil.
the class Nlo method fullOrient6.
public Graph fullOrient6(Graph graph) {
graph = GraphUtils.undirectedGraph(graph);
graph = GraphUtils.replaceNodes(graph, dataSet.getVariables());
Map<Node, List<Node>> cond = new HashMap<>();
Map<Node, List<Double>> pVal = new HashMap<>();
for (Node Y : graph.getNodes()) {
System.out.println("Y = " + Y);
List<Node> adjNodes = graph.getAdjacentNodes(Y);
DepthChoiceGenerator gen = new DepthChoiceGenerator(adjNodes.size(), 4);
int[] choice;
List<Node> bestParents = null;
while ((choice = gen.next()) != null) {
List<Node> W = GraphUtils.asList(choice, adjNodes);
double[] r = cci.residuals(name(Y), names(W));
boolean indep = testIndependence(W, r);
System.out.println("\tW = " + W + " indep = " + (indep) + " p = " + q);
if (indep) {
bestParents = W;
}
}
cond.put(Y, bestParents);
}
for (Node Y : graph.getNodes()) {
System.out.println("Y = " + Y + " cont = " + cond.get(Y));
for (Node X : cond.get(Y)) {
System.out.println("\tcond " + X + " = " + cond.get(X));
if (!cond.get(X).contains(Y)) {
graph.setEndpoint(X, Y, Endpoint.ARROW);
}
}
}
return graph;
}
Aggregations