use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.
the class TestDeltaTetradTest method test4aIteratedPositives.
@Test
public void test4aIteratedPositives() {
RandomUtil.getInstance().setSeed(482834823L);
int numTrials = 10;
double alpha = 0.2;
SemIm sem = getFigure4aSem();
int[] sampleSizes = new int[] { 100, 500, 1000, 5000 };
double[][] answers = { { .1, .5, .2, .1, .1, .1 }, { 0.1, 0.6, 0.0, 0.0, 0.0, 0.0 }, { 0.1, 0.7, 0.2, 0.2, 0.2, 0.2 }, { 0.3, 0.7, 0.2, 0.1, 0.1, 0.1 } };
for (int i = 0; i < 4; i++) {
System.out.println("i = " + i);
int sampleSize = sampleSizes[i];
int[] count = new int[6];
for (int k = 0; k < numTrials; k++) {
DataSet data = sem.simulateData(sampleSize, false);
Node x1 = data.getVariable("x1");
Node x2 = data.getVariable("x2");
Node x3 = data.getVariable("x3");
Node x4 = data.getVariable("x4");
Tetrad t1234 = new Tetrad(x1, x2, x3, x4);
Tetrad t1342 = new Tetrad(x1, x3, x4, x2);
Tetrad t1423 = new Tetrad(x1, x4, x2, x3);
DeltaTetradTest test = new DeltaTetradTest(new CorrelationMatrix(data));
double p1 = test.getPValue(t1234);
double p2 = test.getPValue(t1342);
double p3 = test.getPValue(t1423);
double p4 = test.getPValue(t1234, t1342);
double p5 = test.getPValue(t1234, t1423);
double p6 = test.getPValue(t1342, t1423);
if (p1 < alpha)
count[0]++;
if (p2 < alpha)
count[1]++;
if (p3 < alpha)
count[2]++;
if (p4 < alpha)
count[3]++;
if (p5 < alpha)
count[4]++;
if (p6 < alpha)
count[5]++;
}
double[] _answer = new double[6];
for (int j = 0; j < 6; j++) {
double v = count[j] / (double) numTrials;
_answer[j] = v;
}
// System.out.println(MatrixUtils.toString(_answer));
// System.out.println(MatrixUtils.toString(answers[i]));
// assertTrue(Arrays.equals(_answer, answers[i]));
}
}
use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.
the class TestDeltaTetradTest method getFigure4bSem.
private SemIm getFigure4bSem() {
Graph graph = new EdgeListGraph();
Node xi1 = new GraphNode("xi1");
Node xi2 = new GraphNode("xi2");
Node x1 = new GraphNode("x1");
Node x2 = new GraphNode("x2");
Node x3 = new GraphNode("x3");
Node x4 = new GraphNode("x4");
graph.addNode(xi1);
graph.addNode(xi2);
graph.addNode(x1);
graph.addNode(x2);
graph.addNode(x3);
graph.addNode(x4);
graph.addDirectedEdge(xi1, x1);
graph.addDirectedEdge(xi1, x2);
graph.addDirectedEdge(xi2, x3);
graph.addDirectedEdge(xi2, x4);
graph.addDirectedEdge(xi1, xi2);
SemPm pm = new SemPm(graph);
return new SemIm(pm);
}
use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.
the class TestStandardizedSem method test1.
// Test the code that standardizes a data set.
@Test
public void test1() {
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < 5; i++) {
nodes.add(new ContinuousVariable("X" + (i + 1)));
}
SemGraph graph = new SemGraph(new Dag(GraphUtils.randomGraph(nodes, 0, 5, 30, 15, 15, false)));
SemPm pm = new SemPm(graph);
SemIm im = new SemIm(pm);
DataSet dataSet = im.simulateData(1000, false);
TetradMatrix _dataSet = dataSet.getDoubleData();
_dataSet = DataUtils.standardizeData(_dataSet);
DataSet dataSetStandardized = ColtDataSet.makeData(dataSet.getVariables(), _dataSet);
DataUtils.cov(_dataSet);
DataUtils.mean(_dataSet);
SemEstimator estimator = new SemEstimator(dataSetStandardized, pm);
SemIm imStandardized = estimator.estimate();
imStandardized.getEdgeCoef();
imStandardized.getErrCovar();
new TetradVector(imStandardized.getMeans());
imStandardized.getEdgeCoef();
imStandardized.getErrCovar();
StandardizedSemIm sem = new StandardizedSemIm(im);
imStandardized.getEdgeCoef();
imStandardized.getErrCovar();
assertTrue(isStandardized(sem));
}
use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.
the class TestStandardizedSem method test4.
// This tests what the user is going to try to do in the GUI.
@Test
public void test4() {
List<Node> nodes = new ArrayList<>();
for (int i1 = 0; i1 < 10; i1++) {
nodes.add(new ContinuousVariable("X" + (i1 + 1)));
}
SemGraph graph = new SemGraph(new Dag(GraphUtils.randomGraph(nodes, 0, 10, 30, 15, 15, false)));
SemPm pm = new SemPm(graph);
SemIm im = new SemIm(pm);
StandardizedSemIm sem = new StandardizedSemIm(im);
for (int i = 0; i < 20; i++) {
List<Edge> edges = new ArrayList<>(graph.getEdges());
RandomUtil random = RandomUtil.getInstance();
int index = random.nextInt(edges.size());
Edge edge = edges.get(index);
Node a = edge.getNode1();
Node b = edge.getNode2();
StandardizedSemIm.ParameterRange range = sem.getCoefficientRange(a, b);
double high = range.getHigh();
double low = range.getLow();
double coef = low + random.nextDouble() * (high - low);
assertTrue(sem.setEdgeCoefficient(a, b, coef));
coef = high + random.nextDouble() * (high - low);
assertFalse(sem.setEdgeCoefficient(a, b, coef));
coef = low - random.nextDouble() * (high - low);
assertFalse(sem.setEdgeCoefficient(a, b, coef));
}
}
use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.
the class TestStandardizedSem method test3.
@Test
public void test3() {
RandomUtil.getInstance().setSeed(582374923L);
SemGraph graph = new SemGraph();
Node x1 = new ContinuousVariable("X1");
Node x2 = new ContinuousVariable("X2");
Node x3 = new ContinuousVariable("X3");
graph.addNode(x1);
graph.addNode(x2);
graph.addNode(x3);
graph.setShowErrorTerms(true);
graph.addDirectedEdge(x1, x3);
graph.addDirectedEdge(x2, x3);
graph.addDirectedEdge(x1, x2);
SemPm pm = new SemPm(graph);
SemIm im = new SemIm(pm);
StandardizedSemIm sem = new StandardizedSemIm(im);
DataSet data = sem.simulateData(5000, false);
assertFalse(sem.setEdgeCoefficient(x1, x2, 1.2));
assertFalse(sem.setEdgeCoefficient(x1, x2, 1.5));
assertTrue(sem.setEdgeCoefficient(x1, x2, .5));
assertTrue(sem.setEdgeCoefficient(x1, x3, -.1));
assertTrue(isStandardized(sem));
}
Aggregations