use of edu.cmu.tetrad.graph.EdgeListGraph in project tetrad by cmu-phil.
the class TestModelGenerator method testGenerate.
@Test
public void testGenerate() {
Node x1 = new GraphNode("x1");
Node x2 = new GraphNode("x2");
Node x3 = new GraphNode("x3");
Node x4 = new GraphNode("x4");
Node x5 = new GraphNode("x5");
// graph = new EdgeListGraph();
Graph graph = new EdgeListGraph();
graph.clear();
// Add and remove some nodes.
graph.addNode(x1);
graph.addNode(x2);
graph.addNode(x3);
graph.addNode(x4);
graph.addNode(x5);
graph.addDirectedEdge(x1, x2);
graph.addDirectedEdge(x2, x3);
graph.addDirectedEdge(x3, x4);
List<Graph> variants1 = ModelGenerator.generate(graph);
assertEquals(17, variants1.size());
}
use of edu.cmu.tetrad.graph.EdgeListGraph in project tetrad by cmu-phil.
the class TestSemProposition method constructGraph1.
private Graph constructGraph1() {
Graph graph = new EdgeListGraph();
Node x1 = new GraphNode("X1");
Node x2 = new GraphNode("X2");
Node x3 = new GraphNode("X3");
Node x4 = new GraphNode("X4");
Node x5 = new GraphNode("X5");
graph.addNode(x1);
graph.addNode(x2);
graph.addNode(x3);
graph.addNode(x4);
graph.addNode(x5);
graph.addDirectedEdge(x1, x2);
graph.addDirectedEdge(x2, x3);
graph.addDirectedEdge(x3, x4);
graph.addDirectedEdge(x1, x4);
graph.addDirectedEdge(x4, x5);
return graph;
}
use of edu.cmu.tetrad.graph.EdgeListGraph in project tetrad by cmu-phil.
the class TestSimulatedFmri method testClark2.
// @Test
public void testClark2() {
Node x = new ContinuousVariable("X");
Node y = new ContinuousVariable("Y");
Node z = new ContinuousVariable("Z");
Graph g = new EdgeListGraph();
g.addNode(x);
g.addNode(y);
g.addNode(z);
g.addDirectedEdge(x, y);
g.addDirectedEdge(x, z);
g.addDirectedEdge(y, z);
GeneralizedSemPm pm = new GeneralizedSemPm(g);
try {
pm.setNodeExpression(g.getNode("X"), "E_X");
pm.setNodeExpression(g.getNode("Y"), "0.4 * X + E_Y");
pm.setNodeExpression(g.getNode("Z"), "0.4 * X + 0.4 * Y + E_Z");
String error = "pow(Uniform(0, 1), 1.5)";
pm.setNodeExpression(pm.getErrorNode(g.getNode("X")), error);
pm.setNodeExpression(pm.getErrorNode(g.getNode("Y")), error);
pm.setNodeExpression(pm.getErrorNode(g.getNode("Z")), error);
} catch (ParseException e) {
System.out.println(e);
}
GeneralizedSemIm im = new GeneralizedSemIm(pm);
DataSet data = im.simulateData(1000, false);
edu.cmu.tetrad.search.SemBicScore score = new edu.cmu.tetrad.search.SemBicScore(new CovarianceMatrixOnTheFly(data, false));
Fask fask = new Fask(data, score);
fask.setPenaltyDiscount(1);
fask.setAlpha(0.5);
Graph out = fask.search();
System.out.println(out);
}
use of edu.cmu.tetrad.graph.EdgeListGraph in project tetrad by cmu-phil.
the class InverseCorrelation method search.
public Graph search() {
CovarianceMatrix cov = new CovarianceMatrix(data);
TetradMatrix _data = cov.getMatrix();
TetradMatrix inverse = _data.inverse();
System.out.println(inverse);
Graph graph = new EdgeListGraph(data.getVariables());
for (int i = 0; i < inverse.rows(); i++) {
for (int j = i + 1; j < inverse.columns(); j++) {
double a = inverse.get(i, j);
double b = inverse.get(i, i);
double c = inverse.get(j, j);
double r = -a / Math.sqrt(b * c);
int sampleSize = data.getNumRows();
int z = data.getNumColumns();
double fisherZ = Math.sqrt(sampleSize - z - 3.0) * 0.5 * (Math.log(1.0 + r) - Math.log(1.0 - r));
double p = getPValue(fisherZ);
if (p < threshold) {
Node x = graph.getNodes().get(i);
Node y = graph.getNodes().get(j);
graph.addUndirectedEdge(x, y);
}
// if (abs(fisherZ) > threshold) {
// System.out.println(fisherZ + " &&& " + p);
// Node x = graph.getNodes().get(i);
// Node y = graph.getNodes().get(j);
// graph.addUndirectedEdge(x, y);
// }
// if (Math.abs(inverse.get(i, j)) > threshold) {
// Node x = graph.getNodes().get(i);
// Node y = graph.getNodes().get(j);
// graph.addUndirectedEdge(x, y);
// }
}
}
return graph;
}
use of edu.cmu.tetrad.graph.EdgeListGraph in project tetrad by cmu-phil.
the class TestDM method test5.
// Three latent collider case
@Test
public void test5() {
// setting seed for debug.
RandomUtil.getInstance().setSeed(29483818483L);
Graph graph = emptyGraph(6);
graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X3"));
graph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("X4"));
graph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("X4"));
graph.addDirectedEdge(new ContinuousVariable("X2"), new ContinuousVariable("X4"));
graph.addDirectedEdge(new ContinuousVariable("X2"), new ContinuousVariable("X5"));
SemPm pm = new SemPm(graph);
SemIm im = new SemIm(pm);
DataSet data = im.simulateData(100000, false);
DMSearch search = new DMSearch();
search.setInputs(new int[] { 0, 1, 2 });
search.setOutputs(new int[] { 3, 4, 5 });
search.setData(data);
search.setTrueInputs(search.getInputs());
Graph foundGraph = search.search();
print("Three Latent Collider Case");
Graph trueGraph = new EdgeListGraph();
trueGraph.addNode(new ContinuousVariable("X0"));
trueGraph.addNode(new ContinuousVariable("X1"));
trueGraph.addNode(new ContinuousVariable("X2"));
trueGraph.addNode(new ContinuousVariable("X3"));
trueGraph.addNode(new ContinuousVariable("X4"));
trueGraph.addNode(new ContinuousVariable("X5"));
trueGraph.addNode(new ContinuousVariable("L0"));
trueGraph.addNode(new ContinuousVariable("L1"));
trueGraph.addNode(new ContinuousVariable("L2"));
trueGraph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("L0"));
trueGraph.addDirectedEdge(new ContinuousVariable("L0"), new ContinuousVariable("X3"));
trueGraph.addDirectedEdge(new ContinuousVariable("X0"), new ContinuousVariable("L1"));
trueGraph.addDirectedEdge(new ContinuousVariable("X1"), new ContinuousVariable("L1"));
trueGraph.addDirectedEdge(new ContinuousVariable("X2"), new ContinuousVariable("L1"));
trueGraph.addDirectedEdge(new ContinuousVariable("L1"), new ContinuousVariable("X4"));
trueGraph.addDirectedEdge(new ContinuousVariable("X2"), new ContinuousVariable("L2"));
trueGraph.addDirectedEdge(new ContinuousVariable("L2"), new ContinuousVariable("X5"));
assertTrue(foundGraph.equals(trueGraph));
}
Aggregations