use of edu.cmu.tetrad.graph.IndependenceFact in project tetrad by cmu-phil.
the class RecursivePartialCorrelation method corr.
public double corr(Node x, Node y, List<Node> z) {
if (z.isEmpty())
return this.corr.get(nodesMap.get(x), nodesMap.get(y));
// if (z.size() == 1) {
// int _x = nodesMap.get(x);
// int _y = nodesMap.get(y);
// int _z = nodesMap.get(z.get(0));
//
// double c1 = corr.get(_x, _y);
// double c2 = corr.get(_y, _z);
// double c3 = corr.get(_x, _z);
// return (c1 - c2 * c3) / sqrt(1 - c2 * c2) * sqrt(1 - c3 * c3);
// }
IndependenceFact spec = spec(x, y, z);
if (memory.containsKey(spec)) {
Double corr = memory.get(spec);
if (corr != null)
return corr;
}
Node z0 = z.get(0);
// for (Node _z0 : z) {
// z0 = _z0;
// List<Node> _z = new ArrayList<Node>(z);
// _z.remove(_z0);
// if (memory.containsKey(spec(x, y, _z))) {
// break;
// }
// }
List<Node> _z = new ArrayList<>(z);
_z.remove(z0);
double corr0 = corr(x, y, _z);
double corr1 = corr(x, z0, _z);
double corr2 = corr(z0, y, _z);
double corr3 = (corr0 - corr1 * corr2) / sqrt(1. - corr1 * corr1) * sqrt(1. - corr2 * corr2);
memory.put(spec, corr3);
return corr3;
}
use of edu.cmu.tetrad.graph.IndependenceFact in project tetrad by cmu-phil.
the class IndependenceFactsModel method loadFacts.
public static IndependenceFactsModel loadFacts(Reader reader) throws IOException {
IndependenceFactsModel facts = new IndependenceFactsModel();
Set<String> names = new HashSet<>();
Map<String, Node> nodes = new HashMap<>();
BufferedReader in = new BufferedReader(reader);
String line;
while ((line = in.readLine()) != null) {
String[] tokens = line.split("[ ,;_|]+");
if (tokens.length == 0)
continue;
if (tokens.length < 2)
throw new IllegalArgumentException("Must specify at least two variables--e.g. X1 X2, for X1 _||_ X2.");
for (String token : tokens) {
names.add(token);
if (!nodes.containsKey(token)) {
nodes.put(token, new GraphNode(token));
}
}
List<Node> z = new ArrayList<>();
for (int i = 2; i < tokens.length; i++) {
z.add(nodes.get(tokens[i]));
}
facts.add(new IndependenceFact(nodes.get(tokens[0]), nodes.get(tokens[1]), z));
}
return facts;
}
use of edu.cmu.tetrad.graph.IndependenceFact in project tetrad by cmu-phil.
the class TestIndependenceFact method testSimpleCase.
@Test
public void testSimpleCase() {
Node x = new GraphNode("X");
Node y = new GraphNode("Y");
Node w = new GraphNode("W");
IndependenceFact fact1 = new IndependenceFact(x, y);
IndependenceFact fact2 = new IndependenceFact(y, x);
assertEquals(fact1, fact2);
IndependenceFact fact3 = new IndependenceFact(x, w);
assertNotEquals(fact1, fact3);
List<IndependenceFact> facts = new ArrayList<>();
facts.add(fact1);
assertTrue(facts.contains(fact2));
}
use of edu.cmu.tetrad.graph.IndependenceFact in project tetrad by cmu-phil.
the class IndTestDSep method isIndependent.
/**
* Checks the indicated d-separation fact.
*
* @param x one node.
* @param y a second node.
* @param z a List of nodes (conditioning variables)
* @return true iff x _||_ y | z
*/
public boolean isIndependent(Node x, Node y, List<Node> z) {
if (z == null) {
throw new NullPointerException();
}
for (Node node : z) {
if (node == null) {
throw new NullPointerException();
}
}
if (!observedVars.contains(x)) {
throw new IllegalArgumentException("Not an observed variable: " + x);
}
if (!observedVars.contains(y)) {
throw new IllegalArgumentException("Not an observed variable: " + y);
}
for (Node _z : z) {
if (!observedVars.contains(_z)) {
throw new IllegalArgumentException("Not an observed variable: " + _z);
}
}
boolean dSeparated = !getGraph().isDConnectedTo(x, y, z);
if (verbose) {
if (dSeparated) {
double pValue = 1.0;
TetradLogger.getInstance().log("independencies", SearchLogUtils.independenceFactMsg(x, y, z, pValue));
System.out.println(SearchLogUtils.independenceFactMsg(x, y, z, pValue));
} else {
double pValue = 0.0;
TetradLogger.getInstance().log("dependencies", SearchLogUtils.dependenceFactMsg(x, y, z, pValue));
System.out.println(SearchLogUtils.dependenceFactMsg(x, y, z, pValue));
}
}
if (dSeparated) {
if (this.facts != null) {
this.facts.add(new IndependenceFact(x, y, z));
}
pvalue = 1.0;
} else {
pvalue = 0.0;
}
return dSeparated;
}
use of edu.cmu.tetrad.graph.IndependenceFact in project tetrad by cmu-phil.
the class IndTestChiSquare method isIndependent.
/**
* Determines whether variable x is independent of variable y given a list of conditioning varNames z.
*
* @param x the one variable being compared.
* @param y the second variable being compared.
* @param z the list of conditioning varNames.
* @return true iff x _||_ y | z.
*/
public boolean isIndependent(Node x, Node y, List<Node> z) {
NumberFormat nf = NumberFormatUtil.getInstance().getNumberFormat();
if (z == null) {
throw new NullPointerException();
}
for (Node v : z) {
if (v == null) {
throw new NullPointerException();
}
}
// For testing x, y given z1,...,zn, set up an array of length
// n + 2 containing the indices of these variables in order.
int[] testIndices = new int[2 + z.size()];
testIndices[0] = variables.indexOf(x);
testIndices[1] = variables.indexOf(y);
for (int i = 0; i < z.size(); i++) {
testIndices[i + 2] = variables.indexOf(z.get(i));
}
// the following is lame code--need a better test
for (int i = 0; i < testIndices.length; i++) {
if (testIndices[i] < 0) {
throw new IllegalArgumentException("Variable " + i + " was not used in the constructor.");
}
}
ChiSquareTest.Result result = chiSquareTest.calcChiSquare(testIndices);
this.xSquare = result.getXSquare();
this.df = result.getDf();
this.pValue = result.getPValue();
if (result.isIndep()) {
StringBuilder sb = new StringBuilder();
sb.append("INDEPENDENCE ACCEPTED: ");
sb.append(SearchLogUtils.independenceFact(x, y, z));
sb.append("\tp = ").append(nf.format(result.getPValue())).append("\tx^2 = ").append(nf.format(result.getXSquare())).append("\tdf = ").append(result.getDf());
TetradLogger.getInstance().log("independencies", sb.toString());
}
if (facts != null) {
this.facts.add(new IndependenceFact(x, y, z));
}
return result.isIndep();
}
Aggregations