Search in sources :

Example 61 with ContinuousVariable

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);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList)

Example 62 with ContinuousVariable

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();
    }
}
Also used : DataSet(edu.cmu.tetrad.data.DataSet) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 63 with ContinuousVariable

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);
    }
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) TetradVector(edu.cmu.tetrad.util.TetradVector) Test(org.junit.Test)

Example 64 with ContinuousVariable

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);
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) DataSet(edu.cmu.tetrad.data.DataSet) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) Test(org.junit.Test)

Example 65 with ContinuousVariable

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();
    }
}
Also used : Element(nu.xom.Element) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) ParsingException(nu.xom.ParsingException) Test(org.junit.Test)

Aggregations

ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)91 DataSet (edu.cmu.tetrad.data.DataSet)48 Node (edu.cmu.tetrad.graph.Node)46 Test (org.junit.Test)42 ArrayList (java.util.ArrayList)28 Graph (edu.cmu.tetrad.graph.Graph)22 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)19 SemPm (edu.cmu.tetrad.sem.SemPm)18 SemIm (edu.cmu.tetrad.sem.SemIm)16 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)15 LinkedList (java.util.LinkedList)13 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)12 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)8 DMSearch (edu.cmu.tetrad.search.DMSearch)7 Dag (edu.cmu.tetrad.graph.Dag)6 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)5 RandomUtil (edu.cmu.tetrad.util.RandomUtil)5 ParseException (java.text.ParseException)4 CovarianceMatrixOnTheFly (edu.cmu.tetrad.data.CovarianceMatrixOnTheFly)3 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)3