Search in sources :

Example 6 with GeneralizedSemIm

use of edu.cmu.tetrad.sem.GeneralizedSemIm in project tetrad by cmu-phil.

the class MixedUtils method main.

// main for testing
public static void main(String[] args) {
    // Graph g = GraphConverter.convert("X1-->X2,X2-->X3,X3-->X4");
    Graph g = GraphConverter.convert("X1-->X2,X2-->X3,X3-->X4, X5-->X4");
    // simple graph pm im gen example
    HashMap<String, Integer> nd = new HashMap<>();
    nd.put("X1", 0);
    nd.put("X2", 0);
    nd.put("X3", 4);
    nd.put("X4", 4);
    nd.put("X5", 0);
    g = makeMixedGraph(g, nd);
    /*Graph g = new EdgeListGraph();
        g.addNode(new ContinuousVariable("X1"));
        g.addNode(new ContinuousVariable("X2"));
        g.addNode(new DiscreteVariable("X3", 4));
        g.addNode(new DiscreteVariable("X4", 4));
        g.addNode(new ContinuousVariable("X5"));
        g.addDirectedEdge(g.getNode("X1"), g.getNode("X2"));
        g.addDirectedEdge(g.getNode("X2"), g.getNode("X3"));
        g.addDirectedEdge(g.getNode("X3"), g.getNode("X4"));
        g.addDirectedEdge(g.getNode("X4"), g.getNode("X5"));
        */
    GeneralizedSemPm pm = GaussianCategoricalPm(g, "Split(-1.5,-1,1,1.5)");
    System.out.println(pm);
    System.out.println("STARTS WITH");
    System.out.println(pm.getStartsWithParameterTemplate("C"));
    try {
        MixedUtils.setStartsWith("C", "Split(-.9,-.5,.5,.9)", pm);
    } catch (Throwable t) {
        t.printStackTrace();
    }
    System.out.println("STARTS WITH");
    System.out.println(pm.getStartsWithParameterTemplate("C"));
    System.out.println(pm);
    GeneralizedSemIm im = GaussianCategoricalIm(pm);
    System.out.println(im);
    int samps = 15;
    DataSet ds = im.simulateDataFisher(samps);
    System.out.println(ds);
    System.out.println("num cats " + ((DiscreteVariable) g.getNode("X4")).getNumCategories());
/*Graph trueGraph = DataGraphUtils.loadGraphTxt(new File(MixedUtils.class.getResource("test_data").getPath(), "DAG_0_graph.txt"));
        HashMap<String, Integer> nd = new HashMap<>();
        List<Node> nodes = trueGraph.getNodes();
        for(int i = 0; i < nodes.size(); i++){
            int coin = RandomUtil.getInstance().nextInt(2);
            int dist = (coin==0) ? 0 : 3; //continuous if coin == 0
            nd.put(nodes.get(i).getNode(), dist);
        }
        //System.out.println(getEdgeParams(g.getNode("X3"), g.getNode("X2"), pm).toString());
        //System.out.println(getEdgeParams(g.getNode("X4"), g.getNode("X3"), pm).toString());
        //System.out.println(getEdgeParams(g.getNode("X5"), g.getNode("X4"), pm).toString());
        //System.out.println("num cats " + ((DiscreteVariable) g.getNode("X4")).getNumCategories());
        /*
        HashMap<String, String> nd2 = new HashMap<>();
        nd2.put("X1", "Norm");
        nd2.put("X2", "Norm");
        nd2.put("X3", "Disc");
        nd2.put("X4", "Disc");
        nd2.put("X5", "Disc");
        GeneralizedSemPm pm2 = GaussianTrinaryPm(g, nd2, 10, "Split(-1.5,-.5,.5,1.5)");
        System.out.println("OLD pm:");
        System.out.print(pm2);
        */
}
Also used : GeneralizedSemIm(edu.cmu.tetrad.sem.GeneralizedSemIm) GeneralizedSemPm(edu.cmu.tetrad.sem.GeneralizedSemPm)

Example 7 with GeneralizedSemIm

use of edu.cmu.tetrad.sem.GeneralizedSemIm in project tetrad by cmu-phil.

the class TestSimulatedFmri method testClark2.

// @Test
public void testClark2() {
    Node x = new ContinuousVariable("X");
    Node y = new ContinuousVariable("Y");
    Node z = new ContinuousVariable("Z");
    Graph g = new EdgeListGraph();
    g.addNode(x);
    g.addNode(y);
    g.addNode(z);
    g.addDirectedEdge(x, y);
    g.addDirectedEdge(x, z);
    g.addDirectedEdge(y, z);
    GeneralizedSemPm pm = new GeneralizedSemPm(g);
    try {
        pm.setNodeExpression(g.getNode("X"), "E_X");
        pm.setNodeExpression(g.getNode("Y"), "0.4 * X + E_Y");
        pm.setNodeExpression(g.getNode("Z"), "0.4 * X + 0.4 * Y + E_Z");
        String error = "pow(Uniform(0, 1), 1.5)";
        pm.setNodeExpression(pm.getErrorNode(g.getNode("X")), error);
        pm.setNodeExpression(pm.getErrorNode(g.getNode("Y")), error);
        pm.setNodeExpression(pm.getErrorNode(g.getNode("Z")), error);
    } catch (ParseException e) {
        System.out.println(e);
    }
    GeneralizedSemIm im = new GeneralizedSemIm(pm);
    DataSet data = im.simulateData(1000, false);
    edu.cmu.tetrad.search.SemBicScore score = new edu.cmu.tetrad.search.SemBicScore(new CovarianceMatrixOnTheFly(data, false));
    Fask fask = new Fask(data, score);
    fask.setPenaltyDiscount(1);
    fask.setAlpha(0.5);
    Graph out = fask.search();
    System.out.println(out);
}
Also used : DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) Fask(edu.cmu.tetrad.search.Fask) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) GeneralizedSemIm(edu.cmu.tetrad.sem.GeneralizedSemIm) ParseException(java.text.ParseException) CovarianceMatrixOnTheFly(edu.cmu.tetrad.data.CovarianceMatrixOnTheFly) GeneralizedSemPm(edu.cmu.tetrad.sem.GeneralizedSemPm) SemBicScore(edu.cmu.tetrad.algcomparison.score.SemBicScore)

Example 8 with GeneralizedSemIm

use of edu.cmu.tetrad.sem.GeneralizedSemIm in project tetrad by cmu-phil.

the class SpecialDataClark method simulate.

private DataSet simulate(Graph graph, Parameters parameters) {
    int N = parameters.getInt("sampleSize");
    try {
        GeneralizedSemPm pm = new GeneralizedSemPm(graph);
        Graph g = pm.getGraph();
        for (String p : pm.getParameters()) {
            double coef = RandomUtil.getInstance().nextUniform(0.3, 0.6);
            if (RandomUtil.getInstance().nextDouble() < 0.5) {
                coef *= -1;
            }
            pm.setParameterExpression(p, "" + coef);
        }
        for (Node x : g.getNodes()) {
            if (!(x.getNodeType() == NodeType.ERROR)) {
                String error;
                double s = RandomUtil.getInstance().nextUniform(.1, .4);
                double f = getF(s, N);
                if (s > 0) {
                    error = "pow(Uniform(0, 1), " + (1.0 + f) + ")";
                } else {
                    error = "-pow(Uniform(0, 1), " + (1.0 + f) + ")";
                }
                pm.setNodeExpression(pm.getErrorNode(x), error);
            }
        }
        GeneralizedSemIm im = new GeneralizedSemIm(pm);
        return im.simulateData(N, false);
    } catch (Exception e) {
        throw new IllegalArgumentException("Sorry, I couldn't simulate from that Bayes IM; perhaps not all of\n" + "the parameters have been specified.");
    }
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) RandomGraph(edu.cmu.tetrad.algcomparison.graph.RandomGraph) SingleGraph(edu.cmu.tetrad.algcomparison.graph.SingleGraph) Node(edu.cmu.tetrad.graph.Node) GeneralizedSemIm(edu.cmu.tetrad.sem.GeneralizedSemIm) GeneralizedSemPm(edu.cmu.tetrad.sem.GeneralizedSemPm)

Aggregations

GeneralizedSemIm (edu.cmu.tetrad.sem.GeneralizedSemIm)8 GeneralizedSemPm (edu.cmu.tetrad.sem.GeneralizedSemPm)7 DataSet (edu.cmu.tetrad.data.DataSet)4 Graph (edu.cmu.tetrad.graph.Graph)4 Node (edu.cmu.tetrad.graph.Node)4 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)3 RandomGraph (edu.cmu.tetrad.algcomparison.graph.RandomGraph)2 SemBicScore (edu.cmu.tetrad.algcomparison.score.SemBicScore)2 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)2 CovarianceMatrixOnTheFly (edu.cmu.tetrad.data.CovarianceMatrixOnTheFly)2 Fask (edu.cmu.tetrad.search.Fask)2 ParseException (java.text.ParseException)2 SingleGraph (edu.cmu.tetrad.algcomparison.graph.SingleGraph)1 HashMap (java.util.HashMap)1