use of edu.cmu.tetrad.util.DepthChoiceGenerator in project tetrad by cmu-phil.
the class Nlo method fullOrient4.
public Graph fullOrient4(Graph graph) {
graph = GraphUtils.undirectedGraph(graph);
graph = GraphUtils.replaceNodes(graph, dataSet.getVariables());
Map<Node, List<Node>> cond = new HashMap<>();
for (Node Y : graph.getNodes()) {
System.out.println("Y = " + Y);
double maxQ = 0.0;
List<Node> adjNodes = graph.getAdjacentNodes(Y);
DepthChoiceGenerator gen = new DepthChoiceGenerator(adjNodes.size(), 6);
int[] choice;
List<Node> parents = new ArrayList<>();
while ((choice = gen.next()) != null) {
List<Node> W = GraphUtils.asList(choice, adjNodes);
if (W.isEmpty())
continue;
double[] r = new double[0];
try {
r = cci.residuals(name(Y), names(W));
} catch (Exception e) {
e.printStackTrace();
continue;
}
boolean indep = testIndependence(W, r);
System.out.println("\tW = " + W + " indep = " + (indep) + " q = " + q);
if (indep) {
for (Node x : W) {
if (!parents.contains(x)) {
parents.add(x);
}
}
}
}
cond.put(Y, parents);
}
// Don't orient bidirected edges.
for (Node Y : graph.getNodes()) {
System.out.println("Y = " + Y + " cond = " + cond.get(Y));
for (Node X : cond.get(Y)) {
System.out.println("\tcond " + X + " = " + cond.get(X));
boolean contains = cond.get(X).contains(Y);
if (contains) {
System.out.println("\t\t" + cond.get(X) + " contains " + Y + " ... not orienting.");
}
if (!contains) {
graph.setEndpoint(X, Y, Endpoint.ARROW);
}
}
}
return graph;
}
use of edu.cmu.tetrad.util.DepthChoiceGenerator in project tetrad by cmu-phil.
the class PcStableMaxLocal method reorientNode2.
private void reorientNode2(Node y) {
List<Node> adjy = graph.getAdjacentNodes(y);
adjy.removeAll(graph.getChildren(y));
DepthChoiceGenerator gen = new DepthChoiceGenerator(adjy.size(), adjy.size());
int[] choice;
double maxScore = Double.NEGATIVE_INFINITY;
List<Node> maxParents = new ArrayList<>();
unorientAdjacents(y);
while ((choice = gen.next()) != null) {
List<Node> parents = GraphUtils.asList(choice, adjy);
Iterator<Node> pi = parents.iterator();
int[] parentIndices = new int[parents.size()];
int count = 0;
while (pi.hasNext()) {
Node nextParent = pi.next();
parentIndices[count++] = hashIndices.get(nextParent);
}
int yIndex = hashIndices.get(y);
double _score = score.localScore(yIndex, parentIndices);
if (_score > maxScore) {
maxScore = _score;
maxParents = parents;
}
}
for (Node v : maxParents) {
graph.removeEdge(v, y);
graph.addDirectedEdge(v, y);
}
}
use of edu.cmu.tetrad.util.DepthChoiceGenerator in project tetrad by cmu-phil.
the class SemBicScoreImages method printMinimalLinearlyDependentSet.
// Prints a smallest subset of parents that causes a singular matrix exception.
private void 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) {
out.println("### Linear dependence among variables: " + _sel);
}
}
}
use of edu.cmu.tetrad.util.DepthChoiceGenerator in project tetrad by cmu-phil.
the class HitonMb method hitonMb.
private List<Node> hitonMb(Node t) {
// MB <- {}
Set<Node> mb = new HashSet<>();
Set<Node> _pcpc = new HashSet<>();
for (Node node : getPc(t)) {
List<Node> f = getPc(node);
this.pc.put(node, f);
_pcpc.addAll(f);
}
List<Node> pcpc = new LinkedList<>(_pcpc);
Set<Node> currentMb = new HashSet<>(getPc(t));
currentMb.addAll(pcpc);
currentMb.remove(t);
HashSet<Node> diff = new HashSet<>(currentMb);
diff.removeAll(getPc(t));
diff.remove(t);
// for each x in PCPC \ PC
for (Node x : diff) {
List<Node> s = null;
// Find an S such PC such that x _||_ t | S
DepthChoiceGenerator generator = new DepthChoiceGenerator(pcpc.size(), depth);
int[] choice;
while ((choice = generator.next()) != null) {
List<Node> _s = new LinkedList<>();
for (int index : choice) {
_s.add(pcpc.get(index));
}
numIndTests++;
if (independenceTest.isIndependent(t, x, _s)) {
s = _s;
break;
}
}
if (s == null) {
System.out.println("S not found.");
// mb.add(x);
continue;
}
// y_set <- {y in PC(t) : x in PC(y)}
Set<Node> ySet = new HashSet<>();
for (Node y : getPc(t)) {
if (this.pc.get(y).contains(x)) {
ySet.add(y);
}
}
// For each y in y_set
for (Node y : ySet) {
if (x == y)
continue;
List<Node> _s = new LinkedList<>(s);
_s.add(y);
// If x NOT _||_ t | S U {y}
numIndTests++;
if (!independenceTest.isIndependent(t, x, _s)) {
mb.add(x);
break;
}
}
}
mb.addAll(getPc(t));
return new LinkedList<>(mb);
}
use of edu.cmu.tetrad.util.DepthChoiceGenerator in project tetrad by cmu-phil.
the class SemBicScoreDeterministic method getMinimalLinearlyDependentSet.
private int[] getMinimalLinearlyDependentSet(int i, int[] parents, ICovarianceMatrix cov) {
double small = getDeterminismThreshold();
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);
double s2 = getCovariances().getValue(i, i);
TetradMatrix covxx = getSelection(getCovariances(), parents, parents);
TetradVector covxy = getSelection(getCovariances(), parents, new int[] { i }).getColumn(0);
s2 -= covxx.inverse().times(covxy).dotProduct(covxy);
if (s2 <= small) {
out.println("### Linear dependence among variables: " + _sel);
out.println("### Removing " + _sel.get(0));
return 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 sel;
}
}
return new int[0];
}
Aggregations