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();
}
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();
}
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();
}
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);
}
Aggregations