use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.
the class TestGeneralBootstrapTest method makeContinuousDAG.
private static Graph makeContinuousDAG(int numVars, int numLatentConfounders, double edgesPerNode) {
final int numEdges = (int) (numVars * edgesPerNode);
List<Node> vars = new ArrayList<>();
for (int i = 0; i < numVars; i++) {
vars.add(new ContinuousVariable(Integer.toString(i)));
}
return GraphUtils.randomGraph(vars, numLatentConfounders, numEdges, 30, 15, 15, false);
}
use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.
the class TestGeneralizedSem method test3.
@Test
public void test3() {
RandomUtil.getInstance().setSeed(49293843L);
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.setShowErrorTerms(true);
Node e1 = graph.getExogenous(x1);
Node e2 = graph.getExogenous(x2);
Node e3 = graph.getExogenous(x3);
Node e4 = graph.getExogenous(x4);
Node e5 = graph.getExogenous(x5);
graph.addDirectedEdge(x1, x3);
graph.addDirectedEdge(x1, x2);
graph.addDirectedEdge(x2, x3);
graph.addDirectedEdge(x3, x4);
graph.addDirectedEdge(x2, x4);
graph.addDirectedEdge(x4, x5);
graph.addDirectedEdge(x2, x5);
graph.addDirectedEdge(x5, x1);
GeneralizedSemPm pm = new GeneralizedSemPm(graph);
List<Node> variablesNodes = pm.getVariableNodes();
print(variablesNodes);
List<Node> errorNodes = pm.getErrorNodes();
print(errorNodes);
try {
pm.setNodeExpression(x1, "cos(b1) + a1 * X5 + E_X1");
pm.setNodeExpression(x2, "a2 * X1 + E_X2");
pm.setNodeExpression(x3, "tan(a3*X2 + a4*X1) + E_X3");
pm.setNodeExpression(x4, "0.1 * E^X2 + X3 + E_X4");
pm.setNodeExpression(x5, "0.1 * E^X4 + a6* X2 + E_X5");
pm.setNodeExpression(e1, "U(0, 1)");
pm.setNodeExpression(e2, "U(0, 1)");
pm.setNodeExpression(e3, "U(0, 1)");
pm.setNodeExpression(e4, "U(0, 1)");
pm.setNodeExpression(e5, "U(0, 1)");
GeneralizedSemIm im = new GeneralizedSemIm(pm);
print(im);
DataSet dataSet = im.simulateDataNSteps(1000, false);
// System.out.println(dataSet);
double[] d1 = dataSet.getDoubleData().getColumn(0).toArray();
double[] d2 = dataSet.getDoubleData().getColumn(1).toArray();
double cov = StatUtils.covariance(d1, d2);
assertEquals(-0.002, cov, 0.001);
} catch (ParseException e) {
e.printStackTrace();
}
}
use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.
the class TestGeneralizedSem method test5.
@Test
public void test5() {
RandomUtil.getInstance().setSeed(29999483L);
List<Node> nodes = new ArrayList<>();
for (int i1 = 0; i1 < 5; i1++) {
nodes.add(new ContinuousVariable("X" + (i1 + 1)));
}
Graph graph = new Dag(GraphUtils.randomGraph(nodes, 0, 5, 30, 15, 15, false));
SemPm semPm = new SemPm(graph);
SemIm semIm = new SemIm(semPm);
semIm.simulateDataReducedForm(1000, false);
GeneralizedSemPm pm = new GeneralizedSemPm(semPm);
GeneralizedSemIm im = new GeneralizedSemIm(pm, semIm);
TetradVector e = new TetradVector(5);
for (int i = 0; i < e.size(); i++) {
e.set(i, RandomUtil.getInstance().nextNormal(0, 1));
}
TetradVector record1 = semIm.simulateOneRecord(e);
TetradVector record2 = im.simulateOneRecord(e);
print("XXX1" + e);
print("XXX2" + record1);
print("XXX3" + record2);
for (int i = 0; i < record1.size(); i++) {
assertEquals(record1.get(i), record2.get(i), 1e-10);
}
}
use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.
the class TestGeneralizedSem method test6.
@Test
public void test6() {
RandomUtil.getInstance().setSeed(29999483L);
int numVars = 5;
List<Node> nodes = new ArrayList<>();
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);
SemPm spm = new SemPm(graph);
Parameters params = new Parameters();
params.set("coefLow", 0.5);
params.set("coefHigh", 1.5);
params.set("varLow", 1);
params.set("varHigh", 3);
SemIm sim = new SemIm(spm, params);
GeneralizedSemPm pm = new GeneralizedSemPm(spm);
GeneralizedSemIm im = new GeneralizedSemIm(pm, sim);
DataSet data = im.simulateData(1000, false);
print(im);
GeneralizedSemEstimator estimator = new GeneralizedSemEstimator();
GeneralizedSemIm estIm = estimator.estimate(pm, data);
print(estIm);
print(estimator.getReport());
double aSquaredStar = estimator.getaSquaredStar();
assertEquals(1.04, aSquaredStar, 0.01);
}
use of edu.cmu.tetrad.data.ContinuousVariable in project tetrad by cmu-phil.
the class TestGraph method testXml.
@Test
public void testXml() {
List<Node> nodes1 = new ArrayList<>();
for (int i = 0; i < 10; i++) {
nodes1.add(new ContinuousVariable("X" + (i + 1)));
}
Graph graph = new Dag(GraphUtils.randomGraph(nodes1, 0, 10, 30, 15, 15, false));
Set<Triple> ambiguousTriples = new HashSet<>();
ambiguousTriples.add(pickRandomTriple(graph));
ambiguousTriples.add(pickRandomTriple(graph));
graph.setAmbiguousTriples(ambiguousTriples);
Set<Triple> underlineTriples = new HashSet<>();
underlineTriples.add(pickRandomTriple(graph));
underlineTriples.add(pickRandomTriple(graph));
graph.setUnderLineTriples(underlineTriples);
Set<Triple> dottedUnderlineTriples = new HashSet<>();
dottedUnderlineTriples.add(pickRandomTriple(graph));
dottedUnderlineTriples.add(pickRandomTriple(graph));
graph.setDottedUnderLineTriples(dottedUnderlineTriples);
Map<String, Node> nodes = new HashMap<>();
for (Node node : graph.getNodes()) {
nodes.put(node.getName(), node);
}
Element element = GraphUtils.convertToXml(graph);
try {
Graph _graph = GraphUtils.parseGraphXml(element, nodes);
assertEquals(graph, new Dag(_graph));
} catch (ParsingException e) {
e.printStackTrace();
}
}
Aggregations