use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class Rfci method setMinSepSet.
// ///////////////////////////////////////////////////////////////////////////
// set the sepSet of x and y to the minimal such subset of the given sepSet
// and remove the edge <x, y> if background knowledge allows
// ///////////////////////////////////////////////////////////////////////////
private void setMinSepSet(List<Node> sepSet, Node x, Node y) {
// It is assumed that BK has been considered before calling this method
// (for example, setting independent1 and independent2 in ruleR0_RFCI)
/*
// background knowledge requires this edge
if (knowledge.noEdgeRequired(x.getNode(), y.getNode()))
{
return;
}
*/
List<Node> empty = Collections.emptyList();
boolean indep;
try {
indep = independenceTest.isIndependent(x, y, empty);
} catch (Exception e) {
indep = false;
}
if (indep) {
getSepsets().set(x, y, empty);
return;
}
int sepSetSize = sepSet.size();
for (int i = 1; i <= sepSetSize; i++) {
ChoiceGenerator cg = new ChoiceGenerator(sepSetSize, i);
int[] combination;
while ((combination = cg.next()) != null) {
if (Thread.currentThread().isInterrupted()) {
break;
}
List<Node> condSet = GraphUtils.asList(combination, sepSet);
try {
indep = independenceTest.isIndependent(x, y, condSet);
} catch (Exception e) {
indep = false;
}
if (indep) {
getSepsets().set(x, y, condSet);
return;
}
}
}
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class PossibleDsepCfci method tryRemovingUsingDsep.
private boolean tryRemovingUsingDsep(Node node1, Node node2, int maxPathLength) {
List<Node> possDsep = new LinkedList<>(getPossibleDsep(node1, node2, maxPathLength));
boolean noEdgeRequired = getKnowledge().noEdgeRequired(node1.getName(), node2.getName());
// Added this in accordance with the algorithm spec.
// jdramsey 1/8/04
possDsep.remove(node1);
possDsep.remove(node2);
List<Node> possibleParents = possibleParents(node1, possDsep, getKnowledge());
int _depth = possibleParents.size();
if (getDepth() != -1 && _depth > getDepth()) {
_depth = getDepth();
}
for (int num = 1; num <= _depth; num++) {
ChoiceGenerator cg = new ChoiceGenerator(possibleParents.size(), num);
int[] indSet;
while ((indSet = cg.next()) != null) {
List<Node> condSet = GraphUtils.asList(indSet, possibleParents);
boolean independent = test.isIndependent(node1, node2, condSet);
if (independent && noEdgeRequired) {
System.out.println("*** DSEP removed " + graph.getEdge(node1, node2));
graph.removeEdge(node1, node2);
List<Node> z = new LinkedList<>(condSet);
sepset.set(node1, node2, z);
return true;
}
}
}
return false;
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class IndependenceFactsEditor method generateResults.
private void generateResults() {
results = new ArrayList<>();
List<String> dataVars = getDataVars();
if (getVars().size() < 2) {
tableModel.fireTableDataChanged();
return;
}
// Need a choice generator over the ?'s, and a depth choice generator over the +'s. The +'s all have to come
// at the end at index >= 2.
int numQuestionMarksFirstTwo = 0;
int numQuestionMarksRest = 0;
int numPluses = 0;
int numFixed = 0;
for (int i = 0; i < vars.size(); i++) {
String var = vars.get(i);
if ("?".equals(var) && i < 2)
numQuestionMarksFirstTwo++;
else if ("?".equals(var))
numQuestionMarksRest++;
else if ("+".equals(var))
numPluses++;
else
numFixed++;
}
int[] questionMarkFirstTwoIndices = new int[numQuestionMarksFirstTwo];
int[] questionMarkRestIndices = new int[numQuestionMarksRest];
int[] plusIndices = new int[numPluses];
int[] fixedIndices = new int[numFixed];
String[] fixedVars = new String[numFixed];
int _i = -1;
int _j = -1;
int _k = -1;
int _l = -1;
for (int i = 0; i < vars.size(); i++) {
if ("?".equals(vars.get(i)) && i < 2)
questionMarkFirstTwoIndices[++_i] = i;
else if ("?".equals(vars.get(i)))
questionMarkRestIndices[++_j] = i;
else if ("+".equals(vars.get(i)))
plusIndices[++_k] = i;
else {
fixedIndices[++_l] = i;
fixedVars[_l] = vars.get(i);
}
}
List<String> vars1 = new ArrayList<>(dataVars);
vars1.removeAll(vars);
ChoiceGenerator gen1 = new ChoiceGenerator(vars1.size(), questionMarkFirstTwoIndices.length);
int[] choice1;
LOOP: while ((choice1 = gen1.next()) != null) {
List<String> s2 = asList(choice1, vars1);
List<String> vars2 = new ArrayList<>(vars1);
vars2.removeAll(s2);
ChoiceGenerator gen2 = new ChoiceGenerator(vars2.size(), questionMarkRestIndices.length);
int[] choice2;
while ((choice2 = gen2.next()) != null) {
List<String> s3 = asList(choice2, vars2);
List<String> vars3 = new ArrayList<>(vars2);
vars3.removeAll(s3);
DepthChoiceGenerator gen3 = new DepthChoiceGenerator(vars3.size(), plusIndices.length);
int[] choice3;
while ((choice3 = gen3.next()) != null) {
results.add(new ArrayList<IndependenceResult>());
for (int prod = 0; prod < indTestProducers.size(); prod++) {
String[] vars4 = new String[fixedIndices.length + questionMarkFirstTwoIndices.length + questionMarkRestIndices.length + choice3.length];
for (int i = 0; i < fixedIndices.length; i++) {
vars4[fixedIndices[i]] = fixedVars[i];
}
for (int i = 0; i < choice1.length; i++) {
vars4[questionMarkFirstTwoIndices[i]] = vars1.get(choice1[i]);
}
for (int i = 0; i < choice2.length; i++) {
vars4[questionMarkRestIndices[i]] = vars2.get(choice2[i]);
}
for (int i = 0; i < choice3.length; i++) {
vars4[plusIndices[i]] = vars3.get(choice3[i]);
}
IndependenceTest independenceTest = getIndependenceTest(prod);
Node x = independenceTest.getVariable(vars4[0]);
Node y = independenceTest.getVariable(vars4[1]);
List<Node> z = new ArrayList<>();
for (int i = 2; i < vars4.length; i++) {
z.add(independenceTest.getVariable(vars4[i]));
}
IndependenceResult.Type indep;
double pValue;
try {
indep = independenceTest.isIndependent(x, y, z) ? IndependenceResult.Type.INDEPENDENT : IndependenceResult.Type.DEPENDENT;
pValue = independenceTest.getPValue();
} catch (Exception e) {
indep = IndependenceResult.Type.UNDETERMINED;
pValue = Double.NaN;
}
results.get(results.size() - 1).add(new IndependenceResult(results.size(), factString(x, y, z), indep, pValue));
}
if (results.size() > getListLimit())
break LOOP;
}
}
}
model.setResults(results);
tableModel.fireTableDataChanged();
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class PercentAmbiguous method getValue.
@Override
public double getValue(Graph trueGraph, Graph estGraph) {
int numAmbiguous = 0;
int numTriples = 0;
List<Node> nodes = estGraph.getNodes();
for (Node b : nodes) {
List<Node> adjb = estGraph.getAdjacentNodes(b);
if (adjb.size() < 2)
continue;
ChoiceGenerator gen = new ChoiceGenerator(adjb.size(), 2);
int[] choice;
while ((choice = gen.next()) != null) {
List<Node> _adj = GraphUtils.asList(choice, adjb);
Node a = _adj.get(0);
Node c = _adj.get(1);
if (estGraph.isAmbiguousTriple(a, b, c)) {
numAmbiguous++;
}
numTriples++;
}
}
return numAmbiguous / (double) numTriples;
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class GraphUtils method getNoncollidersFromGraph.
/**
* @return A list of triples of the form <X, Y, Z>, where <X, Y, Z> is a
* definite noncollider in the given graph.
*/
public static List<Triple> getNoncollidersFromGraph(Node node, Graph graph) {
List<Triple> noncolliders = new ArrayList<>();
List<Node> adj = graph.getAdjacentNodes(node);
if (adj.size() < 2) {
return new LinkedList<>();
}
ChoiceGenerator gen = new ChoiceGenerator(adj.size(), 2);
int[] choice;
while ((choice = gen.next()) != null) {
Node x = adj.get(choice[0]);
Node z = adj.get(choice[1]);
Endpoint endpt1 = graph.getEdge(x, node).getProximalEndpoint(node);
Endpoint endpt2 = graph.getEdge(z, node).getProximalEndpoint(node);
if (endpt1 == Endpoint.ARROW && endpt2 == Endpoint.TAIL || endpt1 == Endpoint.TAIL && endpt2 == Endpoint.ARROW || endpt1 == Endpoint.TAIL && endpt2 == Endpoint.TAIL) {
noncolliders.add(new Triple(x, node, z));
}
}
return noncolliders;
}
Aggregations