use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class TestGeneralizedSem method test16.
@Test
public void test16() {
RandomUtil.getInstance().setSeed(29999483L);
try {
Node x1 = new GraphNode("X1");
Node x2 = new GraphNode("X2");
Node x3 = new GraphNode("X3");
Node x4 = new GraphNode("X4");
Graph g = new EdgeListGraphSingleConnections();
g.addNode(x1);
g.addNode(x2);
g.addNode(x3);
g.addNode(x4);
g.addDirectedEdge(x1, x2);
g.addDirectedEdge(x2, x3);
g.addDirectedEdge(x3, x4);
g.addDirectedEdge(x1, x4);
GeneralizedSemPm pm = new GeneralizedSemPm(g);
pm.setNodeExpression(x1, "E_X1");
pm.setNodeExpression(x2, "a1 * X1 + E_X2");
pm.setNodeExpression(x3, "a2 * X2 + E_X3");
pm.setNodeExpression(x4, "a3 * X1 + a4 * X3 ^ 2 + E_X4");
pm.setNodeExpression(pm.getErrorNode(x1), "N(0, c1)");
pm.setNodeExpression(pm.getErrorNode(x2), "N(0, c2)");
pm.setNodeExpression(pm.getErrorNode(x3), "N(0, c3)");
pm.setNodeExpression(pm.getErrorNode(x4), "N(0, c4)");
// pm.setParameterExpression("c1", "1");
// pm.setParameterExpression("c2", "1");
// pm.setParameterExpression("c3", "1");
// pm.setParameterExpression("c4", "1");
GeneralizedSemIm im = new GeneralizedSemIm(pm);
im.setParameterValue("a1", 1);
im.setParameterValue("a2", 1);
im.setParameterValue("a3", 1);
im.setParameterValue("a4", 1);
im.setParameterValue("c1", 1);
im.setParameterValue("c2", 1);
im.setParameterValue("c3", 1);
im.setParameterValue("c4", 1);
print("True model: ");
print(im);
DataSet data = im.simulateDataRecursive(1000, false);
GeneralizedSemIm imInit = new GeneralizedSemIm(pm);
imInit.setParameterValue("a1", .5);
imInit.setParameterValue("a2", .5);
imInit.setParameterValue("a3", .5);
imInit.setParameterValue("a4", .7);
imInit.setParameterValue("c1", 2);
imInit.setParameterValue("c2", 2);
imInit.setParameterValue("c3", 2);
imInit.setParameterValue("c4", 2);
GeneralizedSemEstimator estimator = new GeneralizedSemEstimator();
GeneralizedSemIm estIm = estimator.estimate(pm, data);
print("\n\n\nEstimated model: ");
print(estIm);
print(estimator.getReport());
double aSquaredStar = estimator.getaSquaredStar();
assertEquals(50.38, aSquaredStar, 0.01);
} catch (ParseException e) {
e.printStackTrace();
}
}
use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class TestGeneralizedSem method test8.
@Test
public void test8() {
RandomUtil.getInstance().setSeed(29999483L);
Node x = new GraphNode("X");
Node y = new GraphNode("Y");
List<Node> nodes = new ArrayList<>();
nodes.add(x);
nodes.add(y);
Graph graph = new EdgeListGraphSingleConnections(nodes);
graph.addDirectedEdge(x, y);
SemPm spm = new SemPm(graph);
SemIm sim = new SemIm(spm);
sim.setEdgeCoef(x, y, 20);
sim.setErrVar(x, 1);
sim.setErrVar(y, 1);
GeneralizedSemPm pm = new GeneralizedSemPm(spm);
GeneralizedSemIm im = new GeneralizedSemIm(pm, sim);
print(im);
try {
pm.setParameterEstimationInitializationExpression("b1", "U(10, 30)");
pm.setParameterEstimationInitializationExpression("T1", "U(.1, 3)");
pm.setParameterEstimationInitializationExpression("T2", "U(.1, 3)");
} catch (ParseException e) {
e.printStackTrace();
}
DataSet data = im.simulateDataRecursive(1000, false);
GeneralizedSemEstimator estimator = new GeneralizedSemEstimator();
GeneralizedSemIm estIm = estimator.estimate(pm, data);
print(estIm);
// System.out.println(estimator.getReport());
double aSquaredStar = estimator.getaSquaredStar();
assertEquals(0.69, aSquaredStar, 0.01);
}
use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class TestGeneralizedSem method test2.
@Test
public void test2() {
RandomUtil.getInstance().setSeed(2999983L);
int sampleSize = 1000;
List<Node> variableNodes = new ArrayList<>();
ContinuousVariable x1 = new ContinuousVariable("X1");
ContinuousVariable x2 = new ContinuousVariable("X2");
ContinuousVariable x3 = new ContinuousVariable("X3");
ContinuousVariable x4 = new ContinuousVariable("X4");
ContinuousVariable x5 = new ContinuousVariable("X5");
variableNodes.add(x1);
variableNodes.add(x2);
variableNodes.add(x3);
variableNodes.add(x4);
variableNodes.add(x5);
Graph _graph = new EdgeListGraph(variableNodes);
SemGraph graph = new SemGraph(_graph);
graph.addDirectedEdge(x1, x3);
graph.addDirectedEdge(x2, x3);
graph.addDirectedEdge(x3, x4);
graph.addDirectedEdge(x2, x4);
graph.addDirectedEdge(x4, x5);
graph.addDirectedEdge(x2, x5);
SemPm semPm = new SemPm(graph);
SemIm semIm = new SemIm(semPm);
DataSet dataSet = semIm.simulateData(sampleSize, false);
print(semPm);
GeneralizedSemPm _semPm = new GeneralizedSemPm(semPm);
GeneralizedSemIm _semIm = new GeneralizedSemIm(_semPm, semIm);
DataSet _dataSet = _semIm.simulateDataMinimizeSurface(sampleSize, false);
print(_semPm);
for (int j = 0; j < dataSet.getNumColumns(); j++) {
double[] col = dataSet.getDoubleData().getColumn(j).toArray();
double[] _col = _dataSet.getDoubleData().getColumn(j).toArray();
double mean = StatUtils.mean(col);
double _mean = StatUtils.mean(_col);
double variance = StatUtils.variance(col);
double _variance = StatUtils.variance(_col);
assertEquals(mean, _mean, 0.3);
assertEquals(1.0, variance / _variance, .2);
}
}
use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class TestGeneralizedSem method test11.
@Test
public void test11() {
RandomUtil.getInstance().setSeed(29999483L);
try {
Node x1 = new GraphNode("X1");
Node x2 = new GraphNode("X2");
Node x3 = new GraphNode("X3");
Node x4 = new GraphNode("X4");
Graph g = new EdgeListGraphSingleConnections();
g.addNode(x1);
g.addNode(x2);
g.addNode(x3);
g.addNode(x4);
g.addDirectedEdge(x1, x2);
g.addDirectedEdge(x2, x3);
g.addDirectedEdge(x3, x4);
g.addDirectedEdge(x1, x4);
GeneralizedSemPm pm = new GeneralizedSemPm(g);
pm.setNodeExpression(x1, "E_X1");
pm.setNodeExpression(x2, "a1 * tan(X1) + E_X2");
pm.setNodeExpression(x3, "a2 * tan(X2) + E_X3");
pm.setNodeExpression(x4, "a3 * tan(X1) + a4 * tan(X3) ^ 2 + E_X4");
pm.setNodeExpression(pm.getErrorNode(x1), "N(0, c1)");
pm.setNodeExpression(pm.getErrorNode(x2), "N(0, c2)");
pm.setNodeExpression(pm.getErrorNode(x3), "N(0, c3)");
pm.setNodeExpression(pm.getErrorNode(x4), "N(0, c4)");
pm.setParameterExpression("c1", "4");
pm.setParameterExpression("c2", "4");
pm.setParameterExpression("c3", "4");
pm.setParameterExpression("c4", "4");
GeneralizedSemIm im = new GeneralizedSemIm(pm);
print("True model: ");
print(im);
DataSet data = im.simulateDataRecursive(1000, false);
GeneralizedSemIm imInit = new GeneralizedSemIm(pm);
imInit.setParameterValue("c1", 8);
imInit.setParameterValue("c2", 8);
imInit.setParameterValue("c3", 8);
imInit.setParameterValue("c4", 8);
GeneralizedSemEstimator estimator = new GeneralizedSemEstimator();
GeneralizedSemIm estIm = estimator.estimate(pm, data);
print("\n\n\nEstimated model: ");
print(estIm);
print(estimator.getReport());
double aSquaredStar = estimator.getaSquaredStar();
assertEquals(7.07, aSquaredStar, 0.01);
} catch (ParseException e) {
e.printStackTrace();
}
}
use of edu.cmu.tetrad.data.DataSet in project tetrad by cmu-phil.
the class TestGeneralizedSem method test7.
@Test
public void test7() {
RandomUtil.getInstance().setSeed(29999483L);
List<Node> nodes = new ArrayList<>();
int numVars = 10;
for (int i = 0; i < numVars; i++) nodes.add(new ContinuousVariable("X" + (i + 1)));
Graph graph = GraphUtils.randomGraphRandomForwardEdges(nodes, 0, numVars, 30, 15, 15, false, true);
GeneralizedSemPm pm = new GeneralizedSemPm(graph);
GeneralizedSemIm im = new GeneralizedSemIm(pm);
print(im);
DataSet data = im.simulateDataRecursive(1000, false);
GeneralizedSemEstimator estimator = new GeneralizedSemEstimator();
GeneralizedSemIm estIm = estimator.estimate(pm, data);
print(estIm);
print(estimator.getReport());
double aSquaredStar = estimator.getaSquaredStar();
assertEquals(0.67, aSquaredStar, 0.01);
}
Aggregations