use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.
the class TestStandardizedSem method test7.
// This tests what the user is going to try to do in the GUI.
@Test
public void test7() {
RandomUtil random = RandomUtil.getInstance();
random.setSeed(9394929393L);
List<Node> nodes1 = new ArrayList<>();
for (int i1 = 0; i1 < 5; i1++) {
nodes1.add(new ContinuousVariable("X" + (i1 + 1)));
}
SemGraph graph = new SemGraph(new Dag(GraphUtils.randomGraph(nodes1, 0, 5, 30, 15, 15, false)));
List<Node> nodes = graph.getNodes();
int n1 = RandomUtil.getInstance().nextInt(nodes.size());
int n2 = RandomUtil.getInstance().nextInt(nodes.size());
while (n1 == n2) {
n2 = RandomUtil.getInstance().nextInt(nodes.size());
}
Node node1 = nodes.get(n1);
Node node2 = nodes.get(n2);
Edge _edge = Edges.bidirectedEdge(node1, node2);
graph.addEdge(_edge);
SemPm pm = new SemPm(graph);
SemIm im = new SemIm(pm);
StandardizedSemIm sem = new StandardizedSemIm(im);
DataSet data3 = sem.simulateDataReducedForm(1000, false);
graph.setShowErrorTerms(false);
for (int i = 0; i < 1; i++) {
for (Edge edge : graph.getEdges()) {
Node a = edge.getNode1();
Node b = edge.getNode2();
if (Edges.isDirectedEdge(edge)) {
double initial = sem.getEdgeCoef(a, b);
StandardizedSemIm.ParameterRange range = sem.getCoefficientRange(a, b);
assertEquals(initial, sem.getEdgeCoef(a, b), 0.1);
double low = range.getLow();
double high = range.getHigh();
double _coef = sem.getEdgeCoef(a, b);
double coef = low + random.nextDouble() * (high - low);
assertTrue(sem.setEdgeCoefficient(a, b, coef));
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));
} else if (Edges.isBidirectedEdge(edge)) {
sem.setErrorCovariance(node1, node2, .15);
assertTrue(isStandardized(sem));
StandardizedSemIm.ParameterRange range2 = sem.getCovarianceRange(a, b);
double low = range2.getLow();
double high = range2.getHigh();
if (low == Double.NEGATIVE_INFINITY)
low = -10000;
if (high == Double.POSITIVE_INFINITY)
high = 10000;
double _coef = sem.getErrorCovariance(a, b);
double coef = low + random.nextDouble() * (high - low);
assertTrue(sem.setErrorCovariance(a, b, coef));
sem.setErrorCovariance(a, b, _coef);
if (high != 10000) {
coef = high + random.nextDouble() * (high - low);
assertFalse(sem.setErrorCovariance(a, b, coef));
}
if (low != -10000) {
coef = low - random.nextDouble() * (high - low);
assertFalse(sem.setErrorCovariance(a, b, coef));
}
}
}
}
}
use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.
the class TestSemEvidence method testSemEvidence.
@Test
public void testSemEvidence() {
Graph graph = constructGraph1();
SemPm semPm = new SemPm(graph);
SemIm semIm = new SemIm(semPm);
SemEvidence evidence = new SemEvidence(semIm);
evidence.setManipulated(1, true);
assertTrue(evidence.isManipulated(1));
}
use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.
the class TestSemUpdater method testUpdate.
@Test
public void testUpdate() {
Graph graph = constructGraph1();
SemPm semPm = new SemPm(graph);
SemIm semIm = new SemIm(semPm);
List<Node> nodes = semIm.getVariableNodes();
SemUpdater semUpdater = new SemUpdater(semIm);
SemEvidence evidence = new SemEvidence(semIm);
evidence.getProposition().setValue(nodes.get(4), 10.0);
evidence.getProposition().setValue(nodes.get(2), 1.5);
semUpdater.setEvidence(evidence);
evidence.setManipulated(1, true);
semUpdater.getManipulatedSemIm();
semUpdater.getUpdatedSemIm();
}
use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.
the class TestSemXml method sampleSemIm1.
private static SemIm sampleSemIm1() {
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < 5; i++) {
nodes.add(new ContinuousVariable("X" + (i + 1)));
}
Graph graph = new Dag(GraphUtils.randomGraph(nodes, 0, 5, 30, 15, 15, true));
SemPm pm = new SemPm(graph);
return new SemIm(pm);
}
use of edu.cmu.tetrad.sem.SemIm in project tetrad by cmu-phil.
the class TestStatUtils method testConditionalCorrelation.
/**
* Tests that the unconditional correlations and covariances are correct,
* at least for the unconditional tests.
*/
@Test
public void testConditionalCorrelation() {
RandomUtil.getInstance().setSeed(30299533L);
// Make sure the unconditional correlations and covariances are OK.
List<Node> nodes1 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
nodes1.add(new ContinuousVariable("X" + (i + 1)));
}
Graph graph = new Dag(GraphUtils.randomGraph(nodes1, 0, 5, 3, 3, 3, false));
SemPm pm = new SemPm(graph);
SemIm im = new SemIm(pm);
DataSet dataSet = im.simulateData(1000, false);
double[] x = dataSet.getDoubleData().getColumn(0).toArray();
double[] y = dataSet.getDoubleData().getColumn(1).toArray();
double r1 = StatUtils.correlation(x, y);
double s1 = StatUtils.covariance(x, y);
double v1 = StatUtils.variance(x);
double sd1 = StatUtils.sd(x);
ICovarianceMatrix cov = new CovarianceMatrix(dataSet);
TetradMatrix _cov = cov.getMatrix();
double r2 = StatUtils.partialCorrelation(_cov, 0, 1);
double s2 = StatUtils.partialCovariance(_cov, 0, 1);
double v2 = StatUtils.partialVariance(_cov, 0);
double sd2 = StatUtils.partialStandardDeviation(_cov, 0);
assertEquals(r1, r2, .1);
assertEquals(s1, s2, .1);
assertEquals(v1, v2, .1);
assertEquals(sd1, sd2, 0.1);
}
Aggregations