use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class GFciMax method modifiedR0.
// Due to Spirtes.
public void modifiedR0(Graph fgesGraph) {
graph.reorientAllWith(Endpoint.CIRCLE);
fciOrientbk(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 FgesD method calculateArrowsForward.
// Calculates the new arrows for an a->b edge.
private void calculateArrowsForward(Node a, Node b) {
if (mode == Mode.heuristicSpeedup && !effectEdgesGraph.isAdjacentTo(a, b))
return;
if (adjacencies != null && !adjacencies.isAdjacentTo(a, b))
return;
this.neighbors.put(b, getNeighbors(b));
if (a == b)
throw new IllegalArgumentException();
if (existsKnowledge()) {
if (getKnowledge().isForbidden(a.getName(), b.getName())) {
return;
}
}
Set<Node> naYX = getNaYX(a, b);
if (!isClique(naYX))
return;
List<Node> TNeighbors = getTNeighbors(a, b);
Set<Set<Node>> previousCliques = new HashSet<>();
previousCliques.add(new HashSet<Node>());
Set<Set<Node>> newCliques = new HashSet<>();
FOR: for (int i = 0; i <= TNeighbors.size(); i++) {
final ChoiceGenerator gen = new ChoiceGenerator(TNeighbors.size(), i);
int[] choice;
while ((choice = gen.next()) != null) {
Set<Node> T = GraphUtils.asSet(choice, TNeighbors);
Set<Node> union = new HashSet<>(naYX);
union.addAll(T);
boolean foundAPreviousClique = false;
for (Set<Node> clique : previousCliques) {
if (union.containsAll(clique)) {
foundAPreviousClique = true;
break;
}
}
if (!foundAPreviousClique) {
break FOR;
}
if (!isClique(union))
continue;
newCliques.add(union);
double bump = insertEval(a, b, T, naYX, hashIndices);
if (bump > 0) {
addArrow(a, b, naYX, T, bump);
}
// if (mode == Mode.heuristicSpeedup && union.isEmpty() && score.isEffectEdge(bump) &&
// !effectEdgesGraph.isAdjacentTo(a, b) && graph.getParents(b).isEmpty()) {
// effectEdgesGraph.addUndirectedEdge(a, b);
// }
}
previousCliques = newCliques;
newCliques = new HashSet<>();
}
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class FgesD method calculateArrowsBackward.
// Calculates the arrows for the removal in the backward direction.
private void calculateArrowsBackward(Node a, Node b) {
if (existsKnowledge()) {
if (!getKnowledge().noEdgeRequired(a.getName(), b.getName())) {
return;
}
}
Set<Node> naYX = getNaYX(a, b);
List<Node> _naYX = new ArrayList<>(naYX);
final int _depth = _naYX.size();
for (int i = 0; i <= _depth; i++) {
final ChoiceGenerator gen = new ChoiceGenerator(_naYX.size(), i);
int[] choice;
while ((choice = gen.next()) != null) {
Set<Node> diff = GraphUtils.asSet(choice, _naYX);
Set<Node> h = new HashSet<>(_naYX);
h.removeAll(diff);
if (existsKnowledge()) {
if (!validSetByKnowledge(b, h)) {
continue;
}
}
double bump = deleteEval(a, b, diff, naYX, hashIndices);
if (bump > 0.0) {
addArrow(a, b, naYX, h, bump);
}
}
}
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class FgesOld method calculateArrowsForward.
// Calculates the new arrows for an a->b edge.
private void calculateArrowsForward(Node a, Node b) {
if (isHeuristicSpeedup() && !effectEdgesGraph.isAdjacentTo(a, b))
return;
if (adjacencies != null && !adjacencies.isAdjacentTo(a, b))
return;
this.neighbors.put(b, getNeighbors(b));
if (existsKnowledge()) {
if (getKnowledge().isForbidden(a.getName(), b.getName())) {
return;
}
}
Set<Node> naYX = getNaYX(a, b);
if (!GraphUtils.isClique(naYX, this.graph))
return;
List<Node> TNeighbors = getTNeighbors(a, b);
final int _depth = Math.min(TNeighbors.size(), depth == -1 ? 1000 : depth);
Set<Set<Node>> previousCliques = new HashSet<>();
previousCliques.add(new HashSet<Node>());
Set<Set<Node>> newCliques = new HashSet<>();
FOR: for (int i = 0; i <= _depth; i++) {
final ChoiceGenerator gen = new ChoiceGenerator(TNeighbors.size(), i);
int[] choice;
while ((choice = gen.next()) != null) {
Set<Node> T = GraphUtils.asSet(choice, TNeighbors);
Set<Node> union = new HashSet<>(naYX);
union.addAll(T);
boolean foundAPreviousClique = false;
for (Set<Node> clique : previousCliques) {
if (union.containsAll(clique)) {
foundAPreviousClique = true;
break;
}
}
if (!foundAPreviousClique) {
break FOR;
}
if (!GraphUtils.isClique(union, this.graph))
continue;
newCliques.add(union);
double bump = insertEval(a, b, T, naYX, hashIndices);
if (bump > 0.0) {
addArrow(a, b, naYX, T, bump);
}
if (isHeuristicSpeedup() && union.isEmpty() && fgesScore.isEffectEdge(bump) && !effectEdgesGraph.isAdjacentTo(a, b) && graph.getParents(b).isEmpty()) {
effectEdgesGraph.addUndirectedEdge(a, b);
}
}
previousCliques = newCliques;
newCliques = new HashSet<>();
}
}
use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.
the class GFci method modifiedR0.
// Due to Spirtes.
public void modifiedR0(Graph fgesGraph) {
graph.reorientAllWith(Endpoint.CIRCLE);
fciOrientbk(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);
}
}
}
}
}
Aggregations