Search in sources :

Example 1 with BayesProperties

use of edu.cmu.tetrad.bayes.BayesProperties in project tetrad by cmu-phil.

the class PcStableLocalSearchEditor method reportIfDiscrete.

private String reportIfDiscrete(Graph dag, DataSet dataSet) {
    List vars = dataSet.getVariables();
    Map<String, DiscreteVariable> nodesToVars = new HashMap<>();
    for (int i = 0; i < dataSet.getNumColumns(); i++) {
        DiscreteVariable var = (DiscreteVariable) vars.get(i);
        String name = var.getName();
        Node node = new GraphNode(name);
        nodesToVars.put(node.getName(), var);
    }
    BayesPm bayesPm = new BayesPm(new Dag(dag));
    List<Node> nodes = bayesPm.getDag().getNodes();
    for (Node node : nodes) {
        Node var = nodesToVars.get(node.getName());
        if (var instanceof DiscreteVariable) {
            DiscreteVariable var2 = nodesToVars.get(node.getName());
            int numCategories = var2.getNumCategories();
            List<String> categories = new ArrayList<>();
            for (int j = 0; j < numCategories; j++) {
                categories.add(var2.getCategory(j));
            }
            bayesPm.setCategories(node, categories);
        }
    }
    BayesProperties properties = new BayesProperties(dataSet);
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(4);
    StringBuilder buf = new StringBuilder();
    buf.append("\nP-value = ").append(properties.getLikelihoodRatioP(dag));
    buf.append("\nDf = ").append(properties.getDof());
    buf.append("\nChi square = ").append(nf.format(properties.getChisq()));
    buf.append("\nBIC score = ").append(nf.format(properties.getBic()));
    buf.append("\n\nH0: Completely disconnected graph.");
    return buf.toString();
}
Also used : BayesProperties(edu.cmu.tetrad.bayes.BayesProperties) PatternToDag(edu.cmu.tetrad.search.PatternToDag) List(java.util.List) BayesPm(edu.cmu.tetrad.bayes.BayesPm) NumberFormat(java.text.NumberFormat)

Example 2 with BayesProperties

use of edu.cmu.tetrad.bayes.BayesProperties in project tetrad by cmu-phil.

the class PcGesSearchEditor method reportIfDiscrete.

private String reportIfDiscrete(Graph dag, DataSet dataSet) {
    List vars = dataSet.getVariables();
    Map<String, DiscreteVariable> nodesToVars = new HashMap<>();
    for (int i = 0; i < dataSet.getNumColumns(); i++) {
        DiscreteVariable var = (DiscreteVariable) vars.get(i);
        String name = var.getName();
        Node node = new GraphNode(name);
        nodesToVars.put(node.getName(), var);
    }
    BayesPm bayesPm = new BayesPm(new Dag(dag));
    List<Node> nodes = bayesPm.getDag().getNodes();
    for (Node node : nodes) {
        Node var = nodesToVars.get(node.getName());
        if (var instanceof DiscreteVariable) {
            DiscreteVariable var2 = nodesToVars.get(node.getName());
            int numCategories = var2.getNumCategories();
            List<String> categories = new ArrayList<>();
            for (int j = 0; j < numCategories; j++) {
                categories.add(var2.getCategory(j));
            }
            bayesPm.setCategories(node, categories);
        }
    }
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(4);
    StringBuilder buf = new StringBuilder();
    BayesProperties properties = new BayesProperties(dataSet);
    double p = properties.getLikelihoodRatioP(dag);
    double chisq = properties.getChisq();
    double bic = properties.getBic();
    double dof = properties.getDof();
    buf.append("\nP  = ").append(p);
    buf.append("\nDOF = ").append(dof);
    buf.append("\nChiSq = ").append(nf.format(chisq));
    buf.append("\nBIC = ").append(nf.format(bic));
    buf.append("\n\nH0: Complete DAG.");
    return buf.toString();
}
Also used : BayesProperties(edu.cmu.tetrad.bayes.BayesProperties) PatternToDag(edu.cmu.tetrad.search.PatternToDag) List(java.util.List) BayesPm(edu.cmu.tetrad.bayes.BayesPm) NumberFormat(java.text.NumberFormat)

Example 3 with BayesProperties

use of edu.cmu.tetrad.bayes.BayesProperties in project tetrad by cmu-phil.

the class FgesSearchEditor method reportIfDiscrete.

private String reportIfDiscrete(Graph dag, DataSet dataSet) {
    List vars = dataSet.getVariables();
    Map<String, DiscreteVariable> nodesToVars = new HashMap<>();
    for (int i = 0; i < dataSet.getNumColumns(); i++) {
        DiscreteVariable var = (DiscreteVariable) vars.get(i);
        String name = var.getName();
        Node node = new GraphNode(name);
        nodesToVars.put(node.getName(), var);
    }
    BayesPm bayesPm = new BayesPm(new Dag(dag));
    List<Node> nodes = bayesPm.getDag().getNodes();
    for (Node node : nodes) {
        Node var = nodesToVars.get(node.getName());
        if (var instanceof DiscreteVariable) {
            DiscreteVariable var2 = nodesToVars.get(node.getName());
            int numCategories = var2.getNumCategories();
            List<String> categories = new ArrayList<>();
            for (int j = 0; j < numCategories; j++) {
                categories.add(var2.getCategory(j));
            }
            bayesPm.setCategories(node, categories);
        }
    }
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(4);
    StringBuilder buf = new StringBuilder();
    BayesProperties properties = new BayesProperties(dataSet);
    double p = properties.getLikelihoodRatioP(dag);
    double chisq = properties.getChisq();
    double bic = properties.getBic();
    double dof = properties.getDof();
    buf.append("\nP  = ").append(p);
    buf.append("\nDOF = ").append(dof);
    buf.append("\nChiSq = ").append(nf.format(chisq));
    buf.append("\nBIC = ").append(nf.format(bic));
    buf.append("\n\nH0: Complete DAG.");
    return buf.toString();
}
Also used : BayesProperties(edu.cmu.tetrad.bayes.BayesProperties) List(java.util.List) BayesPm(edu.cmu.tetrad.bayes.BayesPm) NumberFormat(java.text.NumberFormat)

Example 4 with BayesProperties

use of edu.cmu.tetrad.bayes.BayesProperties in project tetrad by cmu-phil.

the class TestBayesBicScorer method testPValue.

@Test
public void testPValue() {
    RandomUtil.getInstance().setSeed(492834924L);
    // Graph graph = GraphConverter.convert("X1,X2,X4,X4,X5,X6,X7,X8");
    Graph graph = GraphConverter.convert("X1-->X2,X2-->X3,X3-->X6,X6-->X7");
    // Graph graph2 = GraphConverter.convert("X1,X2,X3,X7-->X6,X9,X10,X11,X12");
    int numCategories = 8;
    BayesPm pm = new BayesPm(graph, numCategories, numCategories);
    BayesIm im = new MlBayesIm(pm, MlBayesIm.RANDOM);
    DataSet data = im.simulateData(1000, false);
    BayesProperties scorer = new BayesProperties(data);
    double lik = scorer.getLikelihoodRatioP(graph);
    assertEquals(1, lik, 0.001);
}
Also used : BayesProperties(edu.cmu.tetrad.bayes.BayesProperties) Graph(edu.cmu.tetrad.graph.Graph) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) DataSet(edu.cmu.tetrad.data.DataSet) BayesIm(edu.cmu.tetrad.bayes.BayesIm) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) BayesPm(edu.cmu.tetrad.bayes.BayesPm) Test(org.junit.Test)

Aggregations

BayesPm (edu.cmu.tetrad.bayes.BayesPm)4 BayesProperties (edu.cmu.tetrad.bayes.BayesProperties)4 NumberFormat (java.text.NumberFormat)3 List (java.util.List)3 PatternToDag (edu.cmu.tetrad.search.PatternToDag)2 BayesIm (edu.cmu.tetrad.bayes.BayesIm)1 MlBayesIm (edu.cmu.tetrad.bayes.MlBayesIm)1 DataSet (edu.cmu.tetrad.data.DataSet)1 Graph (edu.cmu.tetrad.graph.Graph)1 Test (org.junit.Test)1