package causation.lab; // Sun packages import java.util.*; // our packages import tetrad.graph.*; /** * This class determines and holds all of the D-separation facts * implied by a TetradGraph. * * @version 1.0 July 20, 1999 * @author David Danks */ public class DSeparationFacts { /////////////////// Instance Variables /////////////////// private TetradGraph tg; private String[][] dsep; private int numDSep = 0; private int nvert = 0; private Node[] allNodes; private String[] returnFacts = null; /////////////////// Constructors /////////////////// /** * This constructor reads in a TetradGraph and determines the D-separation * facts implied by that TetradGraph. * * @param tgraph The TetradGraph to be analyzed */ public DSeparationFacts(TetradGraph tgraph) { this(tgraph, true); } /** * This constructor reads in a TetradGraph and, depending on the value of * analyze, possibly determines the D-separation facts. * This constructor is provided since computing D-separation facts can * be computationally expensive, and so the user might want to wait. * * @param tgraph The TetradGraph to be analyzed * @param analyze true, to determine the D-separation * facts immediately; * false, to wait. */ public DSeparationFacts(TetradGraph tgraph, boolean analyze) { tg = tgraph; nvert = tg.getNumNodes(); // lots of things are easier if we have the Nodes in an array Iterator nodeIter = tg.nodeIterator(); allNodes = new Node[nvert]; int n=0; while (nodeIter.hasNext()) { allNodes[n] = (Node)nodeIter.next(); n++; } // allocate for dsep dsep = new String[nvert*(nvert-1)/2][10]; if (analyze) computeDSepFacts(); } /////////////////// Instance Methods /////////////////// /** * Computes the D-separation facts for the stored TetradGraph. Note: This * procedure involves a significant combinatoric explosion, so it can * be quite computationally expensive. */ public void computeDSepFacts() { for (int i=0; i1, and X or Y in the "not used" // part of allNodes if (cnt == expand.length) break; } // now, iterate through for the others for (int i=1; inull, then all of the DSeparationFacts are returned. * * @param filter The filter listing the variables to be shown * @return An array of filtered d-separation facts */ private void filterFacts(String[] filter) { // first, clear out the returnFacts array, so we can re-set it returnFacts = null; String[] fullFacts = getDSepFacts(); // if there's no filter, just set returnFacts to fullFacts if (filter == null) { returnFacts = fullFacts; return; } int numRemoved = 0; FACTS: for (int i=0; i=0; j--) { if (uncond.equals((String)toAdd.elementAt(j))) { toAdd.removeElementAt(j); continue FACTS; } } } } // if there aren't any facts to add, just return initialFacts if (toAdd.size() == 0) return; // now create a new facts array String[] added = new String[returnFacts.length+toAdd.size()]; System.arraycopy(returnFacts, 0, added, 0, returnFacts.length); for (int i=0; itrue, if the two objects have the same * D-separation facts; false, otherwise. */ public boolean equals(DSeparationFacts compare) { String[] one = getDSepFacts(); String[] comp = compare.getDSepFacts(); // first, just check to make sure they're the same length if (one.length != comp.length) return false; // now, check the strings in comp, removing them from one, so that // we can easily check whether every string in one is also in comp COMP: for (int i=0; i