use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.
the class BayesImNodeEditingTable method getNumParents.
private int getNumParents() {
Model editingTableModel = (Model) getModel();
BayesIm bayesIm = editingTableModel.getBayesIm();
int nodeIndex = editingTableModel.getNodeIndex();
return bayesIm.getNumParents(nodeIndex);
}
use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.
the class ADTreeTest method main.
public static void main(String[] args) throws Exception {
int columns = 40;
int numEdges = 40;
int rows = 500;
List<Node> variables = new ArrayList<>();
List<String> varNames = new ArrayList<>();
for (int i = 0; i < columns; i++) {
final String name = "X" + (i + 1);
varNames.add(name);
variables.add(new ContinuousVariable(name));
}
Graph graph = GraphUtils.randomGraphRandomForwardEdges(variables, 0, numEdges, 30, 15, 15, false, true);
BayesPm pm = new BayesPm(graph);
BayesIm im = new MlBayesIm(pm, MlBayesIm.RANDOM);
DataSet data = im.simulateData(rows, false);
// This implementation uses a DataTable to represent the data
// The first type parameter is the type for the variables
// The second type parameter is the type for the values of the variables
DataTableImpl<Node, Short> dataTable = new DataTableImpl<>(variables);
for (int i = 0; i < rows; i++) {
ArrayList<Short> intArray = new ArrayList<>();
for (int j = 0; j < columns; j++) {
intArray.add((short) data.getInt(i, j));
}
dataTable.addRow(intArray);
}
// create the tree
long start = System.currentTimeMillis();
ADTree<Node, Short> adTree = new ADTree<>(dataTable);
System.out.println(String.format("Generated tree in %s millis", System.currentTimeMillis() - start));
// the query is an arbitrary map of vars and their values
TreeMap<Node, Short> query = new TreeMap<>();
query.put(node(pm, "X1"), (short) 1);
query.put(node(pm, "X5"), (short) 0);
start = System.currentTimeMillis();
System.out.println(String.format("Count is %d", adTree.count(query)));
System.out.println(String.format("Query in %s ms", System.currentTimeMillis() - start));
query.clear();
query.put(node(pm, "X1"), (short) 1);
query.put(node(pm, "X2"), (short) 1);
query.put(node(pm, "X5"), (short) 0);
query.put(node(pm, "X10"), (short) 1);
start = System.currentTimeMillis();
System.out.println(String.format("Count is %d", adTree.count(query)));
System.out.println(String.format("Query in %s ms", System.currentTimeMillis() - start));
}
use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.
the class LoadBayesImXsdlXmlAction method actionPerformed.
public void actionPerformed(ActionEvent e) {
if (bayesImWrapper == null) {
throw new RuntimeException("Not a Bayes IM.");
}
JFileChooser chooser = getJFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
if (file != null) {
Preferences.userRoot().put("fileSaveLocation", file.getParent());
}
try {
Builder builder = new Builder();
Document document = builder.build(file);
printDocument(document);
XdslXmlParser parser = new XdslXmlParser();
BayesIm bayesIm = parser.getBayesIm(document.getRootElement());
System.out.println(bayesIm);
boolean allSpecified = true;
for (edu.cmu.tetrad.graph.Node node : bayesIm.getBayesPm().getDag().getNodes()) {
if (node.getCenterX() == -1 || node.getCenterY() == -1) {
allSpecified = false;
}
}
if (!allSpecified) {
GraphUtils.circleLayout(bayesIm.getBayesPm().getDag(), 200, 200, 150);
}
bayesImWrapper.setBayesIm(bayesIm);
bayesImEditor.getBayesIm(bayesIm);
} catch (ParsingException e2) {
e2.printStackTrace();
throw new RuntimeException("Had trouble parsing that...");
} catch (IOException e2) {
e2.printStackTrace();
throw new RuntimeException("Had trouble reading the file...");
}
}
use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.
the class LoadBayesImXmlAction method actionPerformed.
public void actionPerformed(ActionEvent e) {
if (bayesImWrapper == null) {
throw new RuntimeException("Not a Bayes IM.");
}
JFileChooser chooser = getJFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
if (file != null) {
Preferences.userRoot().put("fileSaveLocation", file.getParent());
}
try {
Builder builder = new Builder();
Document document = builder.build(file);
printDocument(document);
BayesXmlParser parser = new BayesXmlParser();
BayesIm bayesIm = parser.getBayesIm(document.getRootElement());
System.out.println(bayesIm);
boolean allSpecified = true;
for (edu.cmu.tetrad.graph.Node node : bayesIm.getBayesPm().getDag().getNodes()) {
if (node.getCenterX() == -1 || node.getCenterY() == -1) {
allSpecified = false;
}
}
if (!allSpecified) {
GraphUtils.circleLayout(bayesIm.getBayesPm().getDag(), 200, 200, 150);
}
bayesImWrapper.setBayesIm(bayesIm);
bayesImEditor.getBayesIm(bayesIm);
} catch (ParsingException e2) {
e2.printStackTrace();
throw new RuntimeException("Had trouble parsing that...");
} catch (IOException e2) {
e2.printStackTrace();
throw new RuntimeException("Had trouble reading the file...");
}
}
use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.
the class TestEvidence method testUpdate1.
/**
* Richard's 2-variable example worked by hand.
*/
@Test
public void testUpdate1() {
BayesIm bayesIm = sampleBayesIm2();
Evidence evidence = Evidence.tautology(bayesIm);
evidence.getProposition().removeCategory(0, 1);
evidence.getProposition().setVariable(1, false);
evidence.setManipulated(0, true);
Evidence evidence2 = new Evidence(evidence, bayesIm);
assertEquals(evidence2, evidence);
assertEquals(evidence, new Evidence(evidence));
BayesIm bayesIm2 = new MlBayesIm(bayesIm);
Evidence evidence3 = new Evidence(evidence, bayesIm2);
assertTrue(!(evidence3.equals(evidence2)));
}
Aggregations