use of edu.cmu.tetrad.bayes.BayesPm in project tetrad by cmu-phil.
the class TestGeneralBootstrapTest method testFCId.
@Test
public void testFCId() {
double structurePrior = 1, samplePrior = 1;
int depth = -1;
int maxPathLength = -1;
int numVars = 20;
int edgesPerNode = 2;
int numLatentConfounders = 4;
int numCases = 50;
int numBootstrapSamples = 5;
boolean verbose = true;
long seed = 123;
Graph dag = makeDiscreteDAG(numVars, numLatentConfounders, edgesPerNode);
DagToPag dagToPag = new DagToPag(dag);
Graph truePag = dagToPag.convert();
System.out.println("Truth PAG_of_the_true_DAG Graph:");
System.out.println(truePag.toString());
BayesPm pm = new BayesPm(dag, 2, 3);
BayesIm im = new MlBayesIm(pm, MlBayesIm.RANDOM);
DataSet data = im.simulateData(numCases, seed, false);
Parameters parameters = new Parameters();
parameters.set("structurePrior", structurePrior);
parameters.set("samplePrior", samplePrior);
parameters.set("depth", depth);
parameters.set("maxPathLength", maxPathLength);
parameters.set("numPatternsToStore", 0);
parameters.set("verbose", verbose);
IndependenceWrapper test = new ChiSquare();
Algorithm algorithm = new Fci(test);
GeneralBootstrapTest bootstrapTest = new GeneralBootstrapTest(data, algorithm, numBootstrapSamples);
bootstrapTest.setVerbose(verbose);
bootstrapTest.setParameters(parameters);
bootstrapTest.setEdgeEnsemble(BootstrapEdgeEnsemble.Highest);
Graph resultGraph = bootstrapTest.search();
System.out.println("Estimated Bootstrapped PAG_of_the_true_DAG Graph:");
System.out.println(resultGraph.toString());
// Adjacency Confusion Matrix
int[][] adjAr = GeneralBootstrapTest.getAdjConfusionMatrix(truePag, resultGraph);
printAdjConfusionMatrix(adjAr);
// Edge Type Confusion Matrix
int[][] edgeAr = GeneralBootstrapTest.getEdgeTypeConfusionMatrix(truePag, resultGraph);
printEdgeTypeConfusionMatrix(edgeAr);
}
use of edu.cmu.tetrad.bayes.BayesPm in project tetrad by cmu-phil.
the class TestGeneralBootstrapTest method testFGESd.
@Test
public void testFGESd() {
double structurePrior = 1, samplePrior = 1;
boolean faithfulnessAssumed = false;
int maxDegree = -1;
int numVars = 20;
int edgesPerNode = 2;
int numLatentConfounders = 0;
int numCases = 50;
int numBootstrapSamples = 5;
boolean verbose = true;
long seed = 123;
Graph dag = makeDiscreteDAG(numVars, numLatentConfounders, edgesPerNode);
System.out.println("Truth Graph:");
System.out.println(dag.toString());
BayesPm pm = new BayesPm(dag, 2, 3);
BayesIm im = new MlBayesIm(pm, MlBayesIm.RANDOM);
DataSet data = im.simulateData(numCases, seed, false);
Parameters parameters = new Parameters();
parameters.set("structurePrior", structurePrior);
parameters.set("samplePrior", samplePrior);
parameters.set("faithfulnessAssumed", faithfulnessAssumed);
parameters.set("maxDegree", maxDegree);
parameters.set("numPatternsToStore", 0);
parameters.set("verbose", verbose);
ScoreWrapper score = new BdeuScore();
Algorithm algorithm = new Fges(score);
GeneralBootstrapTest bootstrapTest = new GeneralBootstrapTest(data, algorithm, numBootstrapSamples);
bootstrapTest.setVerbose(verbose);
bootstrapTest.setParameters(parameters);
bootstrapTest.setEdgeEnsemble(BootstrapEdgeEnsemble.Highest);
Graph resultGraph = bootstrapTest.search();
System.out.println("Estimated Graph:");
System.out.println(resultGraph.toString());
// Adjacency Confusion Matrix
int[][] adjAr = GeneralBootstrapTest.getAdjConfusionMatrix(dag, resultGraph);
printAdjConfusionMatrix(adjAr);
// Edge Type Confusion Matrix
int[][] edgeAr = GeneralBootstrapTest.getEdgeTypeConfusionMatrix(dag, resultGraph);
printEdgeTypeConfusionMatrix(edgeAr);
}
use of edu.cmu.tetrad.bayes.BayesPm in project tetrad by cmu-phil.
the class TestGeneralBootstrapTest method testGFCId.
@Test
public void testGFCId() {
double structurePrior = 1, samplePrior = 1;
boolean faithfulnessAssumed = false;
int maxDegree = -1;
int numVars = 20;
int edgesPerNode = 2;
int numLatentConfounders = 4;
int numCases = 50;
int numBootstrapSamples = 5;
boolean verbose = true;
long seed = 123;
Graph dag = makeDiscreteDAG(numVars, numLatentConfounders, edgesPerNode);
DagToPag dagToPag = new DagToPag(dag);
Graph truePag = dagToPag.convert();
System.out.println("Truth PAG_of_the_true_DAG Graph:");
System.out.println(truePag.toString());
BayesPm pm = new BayesPm(dag, 2, 3);
BayesIm im = new MlBayesIm(pm, MlBayesIm.RANDOM);
DataSet data = im.simulateData(numCases, seed, false);
Parameters parameters = new Parameters();
parameters.set("structurePrior", structurePrior);
parameters.set("samplePrior", samplePrior);
parameters.set("faithfulnessAssumed", faithfulnessAssumed);
parameters.set("maxDegree", maxDegree);
parameters.set("numPatternsToStore", 0);
parameters.set("verbose", verbose);
ScoreWrapper score = new BdeuScore();
IndependenceWrapper test = new ChiSquare();
Algorithm algorithm = new Gfci(test, score);
GeneralBootstrapTest bootstrapTest = new GeneralBootstrapTest(data, algorithm, numBootstrapSamples);
bootstrapTest.setVerbose(verbose);
bootstrapTest.setParameters(parameters);
bootstrapTest.setEdgeEnsemble(BootstrapEdgeEnsemble.Highest);
Graph resultGraph = bootstrapTest.search();
System.out.println("Estimated Bootstrapped PAG_of_the_true_DAG Graph:");
System.out.println(resultGraph.toString());
// Adjacency Confusion Matrix
int[][] adjAr = GeneralBootstrapTest.getAdjConfusionMatrix(truePag, resultGraph);
printAdjConfusionMatrix(adjAr);
// Edge Type Confusion Matrix
int[][] edgeAr = GeneralBootstrapTest.getEdgeTypeConfusionMatrix(truePag, resultGraph);
printEdgeTypeConfusionMatrix(edgeAr);
}
use of edu.cmu.tetrad.bayes.BayesPm in project tetrad by cmu-phil.
the class TestHistogram method testHistogram.
@Test
public void testHistogram() {
RandomUtil.getInstance().setSeed(4829384L);
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < 5; i++) {
nodes.add(new ContinuousVariable("X" + (i + 1)));
}
Dag trueGraph = new Dag(GraphUtils.randomGraph(nodes, 0, 5, 30, 15, 15, false));
int sampleSize = 1000;
// Continuous
SemPm semPm = new SemPm(trueGraph);
SemIm semIm = new SemIm(semPm);
DataSet data = semIm.simulateData(sampleSize, false);
Histogram histogram = new Histogram(data);
histogram.setTarget("X1");
histogram.setNumBins(20);
assertEquals(3.76, histogram.getMax(), 0.01);
assertEquals(-3.83, histogram.getMin(), 0.01);
assertEquals(1000, histogram.getN());
histogram.setTarget("X1");
histogram.setNumBins(10);
histogram.addConditioningVariable("X3", 0, 1);
histogram.addConditioningVariable("X4", 0, 1);
histogram.removeConditioningVariable("X3");
assertEquals(3.76, histogram.getMax(), 0.01);
assertEquals(-3.83, histogram.getMin(), 0.01);
assertEquals(188, histogram.getN());
double[] arr = histogram.getContinuousData("X2");
histogram.addConditioningVariable("X2", StatUtils.min(arr), StatUtils.mean(arr));
// Discrete
BayesPm bayesPm = new BayesPm(trueGraph);
BayesIm bayesIm = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
DataSet data2 = bayesIm.simulateData(sampleSize, false);
// For some reason these are giving different
// values when all of the unit tests are run are
// once. TODO They produce stable values when
// this particular test is run repeatedly.
Histogram histogram2 = new Histogram(data2);
histogram2.setTarget("X1");
int[] frequencies1 = histogram2.getFrequencies();
// assertEquals(928, frequencies1[0]);
// assertEquals(72, frequencies1[1]);
histogram2.setTarget("X1");
histogram2.addConditioningVariable("X2", 0);
histogram2.addConditioningVariable("X3", 1);
int[] frequencies = histogram2.getFrequencies();
// assertEquals(377, frequencies[0]);
// assertEquals(28, frequencies[1]);
}
use of edu.cmu.tetrad.bayes.BayesPm in project tetrad by cmu-phil.
the class BayesImEditorObs method init.
private void init(BayesIm bayesIm) {
if (bayesIm == null) {
throw new NullPointerException("Bayes IM must not be null.");
}
BayesPm bayesPm = bayesIm.getBayesPm();
Graph graph = bayesPm.getDag();
GraphWorkbench workbench = new GraphWorkbench(graph);
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
menuBar.add(file);
// file.add(new SaveBayesImXmlAction(this));
// file.add(new LoadBayesImXmlAction(wrapper, this));
file.add(new SaveScreenshot(this, true, "Save Screenshot..."));
file.add(new SaveComponentImage(workbench, "Save Graph Image..."));
setLayout(new BorderLayout());
add(menuBar, BorderLayout.NORTH);
wizard = new BayesImEditorWizardObs(bayesIm, workbench);
wizard.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if ("editorValueChanged".equals(evt.getPropertyName())) {
firePropertyChange("modelChanged", null, null);
}
}
});
JScrollPane workbenchScroll = new JScrollPane(workbench);
JScrollPane wizardScroll = new JScrollPane(getWizard());
workbenchScroll.setPreferredSize(new Dimension(450, 450));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, workbenchScroll, wizardScroll);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(workbenchScroll.getPreferredSize().width);
add(splitPane, BorderLayout.CENTER);
setName("Bayes IM Obs Editor");
getWizard().addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if ("editorClosing".equals(evt.getPropertyName())) {
firePropertyChange("editorClosing", null, getName());
}
if ("closeFrame".equals(evt.getPropertyName())) {
firePropertyChange("closeFrame", null, null);
firePropertyChange("editorClosing", true, true);
}
if ("modelChanged".equals(evt.getPropertyName())) {
firePropertyChange("modelChanged", evt.getOldValue(), evt.getNewValue());
}
}
});
}
Aggregations