use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.
the class TestGFci method testRandomDiscreteData.
@Test
public void testRandomDiscreteData() {
int sampleSize = 1000;
Graph g = GraphConverter.convert("X1-->X2,X1-->X3,X1-->X4,X2-->X3,X2-->X4,X3-->X4");
Dag dag = new Dag(g);
BayesPm bayesPm = new BayesPm(dag);
BayesIm bayesIm = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
DataSet data = bayesIm.simulateData(sampleSize, false);
IndependenceTest test = new IndTestChiSquare(data, 0.05);
BDeuScore bDeuScore = new BDeuScore(data);
bDeuScore.setSamplePrior(1.0);
bDeuScore.setStructurePrior(1.0);
GFci gFci = new GFci(test, bDeuScore);
gFci.setFaithfulnessAssumed(true);
long start = System.currentTimeMillis();
gFci.search();
long stop = System.currentTimeMillis();
System.out.println("Elapsed " + (stop - start) + " ms");
DagToPag dagToPag = new DagToPag(g);
dagToPag.setVerbose(false);
}
use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.
the class BayesNetSimulation method simulate.
private DataSet simulate(Graph graph, Parameters parameters) {
boolean saveLatentVars = parameters.getBoolean("saveLatentVars");
try {
BayesIm im = this.im;
if (im == null) {
BayesPm pm = this.pm;
if (pm == null) {
int minCategories = parameters.getInt("minCategories");
int maxCategories = parameters.getInt("maxCategories");
pm = new BayesPm(graph, minCategories, maxCategories);
im = new MlBayesIm(pm, MlBayesIm.RANDOM);
ims.add(im);
return im.simulateData(parameters.getInt("sampleSize"), saveLatentVars);
} else {
im = new MlBayesIm(pm, MlBayesIm.RANDOM);
this.im = im;
ims.add(im);
return im.simulateData(parameters.getInt("sampleSize"), saveLatentVars);
}
} else {
ims = new ArrayList<>();
ims.add(im);
return im.simulateData(parameters.getInt("sampleSize"), saveLatentVars);
}
} catch (Exception e) {
e.printStackTrace();
throw new IllegalArgumentException("Sorry, I couldn't simulate from that Bayes IM; perhaps not all of\n" + "the parameters have been specified.");
}
}
use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.
the class BayesUpdaterEditorObs method createRightPanel.
private JPanel createRightPanel(UpdaterWrapper bayesUpdater) {
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BorderLayout());
rightPanel.add(createMenuBar(), BorderLayout.NORTH);
rightPanel.add(createWizardPanel(bayesUpdater), BorderLayout.CENTER);
BayesIm bayesIm = bayesUpdater.getBayesUpdater().getBayesIm();
boolean incomplete = false;
for (int i = 0; i < bayesIm.getNumNodes(); i++) {
if (bayesIm.isIncomplete(i)) {
incomplete = true;
break;
}
}
if (incomplete) {
JLabel label = new JLabel("NOTE: The Bayes IM is not completely specified.");
label.setFont(new Font("Dialog", Font.BOLD, 12));
rightPanel.add(label, BorderLayout.SOUTH);
}
return rightPanel;
}
use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.
the class BayesEstimatorNodeEditingTable method showPopup.
private void showPopup(MouseEvent e) {
JPopupMenu popup = new JPopupMenu();
JMenuItem randomizeRow = new JMenuItem("Randomize this row");
JMenuItem randomizeIncompleteRows = new JMenuItem("Randomize incomplete rows in table");
JMenuItem randomizeEntireTable = new JMenuItem("Randomize entire table");
JMenuItem randomizeAllTables = new JMenuItem("Randomize all tables");
JMenuItem clearRow = new JMenuItem("Clear this row");
JMenuItem clearEntireTable = new JMenuItem("Clear entire table");
randomizeRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int nodeIndex = getEditingTableModel().getNodeIndex();
BayesEstimatorNodeEditingTable editingTable = BayesEstimatorNodeEditingTable.this;
TableCellEditor cellEditor = editingTable.getCellEditor();
if (cellEditor != null) {
cellEditor.cancelCellEditing();
}
Point point = new Point(getLastX(), getLastY());
int rowIndex = editingTable.rowAtPoint(point);
BayesIm bayesIm = getBayesIm();
bayesIm.randomizeRow(nodeIndex, rowIndex);
getEditingTableModel().fireTableDataChanged();
firePropertyChange("modelChanged", null, null);
}
});
randomizeIncompleteRows.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int nodeIndex = getEditingTableModel().getNodeIndex();
BayesIm bayesIm = getBayesIm();
if (!existsIncompleteRow(bayesIm, nodeIndex)) {
JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "There are no incomplete rows in this table.");
return;
}
BayesEstimatorNodeEditingTable editingTable = BayesEstimatorNodeEditingTable.this;
TableCellEditor cellEditor = editingTable.getCellEditor();
if (cellEditor != null) {
cellEditor.cancelCellEditing();
}
bayesIm.randomizeIncompleteRows(nodeIndex);
getEditingTableModel().fireTableDataChanged();
firePropertyChange("modelChanged", null, null);
}
});
randomizeEntireTable.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int nodeIndex = getEditingTableModel().getNodeIndex();
BayesIm bayesIm = getBayesIm();
if (existsCompleteRow(bayesIm, nodeIndex)) {
int ret = JOptionPane.showConfirmDialog(JOptionUtils.centeringComp(), "This will modify all values in the table. " + "Continue?", "Warning", JOptionPane.YES_NO_OPTION);
if (ret == JOptionPane.NO_OPTION) {
return;
}
}
BayesEstimatorNodeEditingTable editingTable = BayesEstimatorNodeEditingTable.this;
TableCellEditor cellEditor = editingTable.getCellEditor();
if (cellEditor != null) {
cellEditor.cancelCellEditing();
}
bayesIm.randomizeTable(nodeIndex);
getEditingTableModel().fireTableDataChanged();
firePropertyChange("modelChanged", null, null);
}
});
randomizeAllTables.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int ret = JOptionPane.showConfirmDialog(JOptionUtils.centeringComp(), "This will modify all values in the entire Bayes model! " + "Continue?", "Warning", JOptionPane.YES_NO_OPTION);
if (ret == JOptionPane.NO_OPTION) {
return;
}
BayesIm bayesIm = getBayesIm();
for (int nodeIndex = 0; nodeIndex < getBayesIm().getNumNodes(); nodeIndex++) {
BayesEstimatorNodeEditingTable editingTable = BayesEstimatorNodeEditingTable.this;
TableCellEditor cellEditor = editingTable.getCellEditor();
if (cellEditor != null) {
cellEditor.cancelCellEditing();
}
bayesIm.randomizeTable(nodeIndex);
getEditingTableModel().fireTableDataChanged();
firePropertyChange("modelChanged", null, null);
}
}
});
clearRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int nodeIndex = getEditingTableModel().getNodeIndex();
BayesEstimatorNodeEditingTable editingTable = BayesEstimatorNodeEditingTable.this;
TableCellEditor cellEditor = editingTable.getCellEditor();
if (cellEditor != null) {
cellEditor.cancelCellEditing();
}
Point point = new Point(getLastX(), getLastY());
int rowIndex = editingTable.rowAtPoint(point);
BayesIm bayesIm = getBayesIm();
bayesIm.clearRow(nodeIndex, rowIndex);
getEditingTableModel().fireTableRowsUpdated(rowIndex, rowIndex);
firePropertyChange("modelChanged", null, null);
}
});
clearEntireTable.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int nodeIndex = getEditingTableModel().getNodeIndex();
BayesIm bayesIm = getBayesIm();
if (existsCompleteRow(bayesIm, nodeIndex)) {
int ret = JOptionPane.showConfirmDialog(JOptionUtils.centeringComp(), "This will delete all values in the table. " + "Continue?", "Warning", JOptionPane.YES_NO_OPTION);
if (ret == JOptionPane.NO_OPTION) {
return;
}
}
BayesEstimatorNodeEditingTable editingTable = BayesEstimatorNodeEditingTable.this;
TableCellEditor cellEditor = editingTable.getCellEditor();
if (cellEditor != null) {
cellEditor.cancelCellEditing();
}
bayesIm.clearTable(nodeIndex);
getEditingTableModel().fireTableDataChanged();
firePropertyChange("modelChanged", null, null);
}
});
popup.add(randomizeRow);
popup.add(randomizeIncompleteRows);
popup.add(randomizeEntireTable);
popup.add(randomizeAllTables);
popup.addSeparator();
popup.add(clearRow);
popup.add(clearEntireTable);
this.lastX = e.getX();
this.lastY = e.getY();
popup.show((Component) e.getSource(), e.getX(), e.getY());
}
use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.
the class BayesImEditor method setEditorPanel.
private void setEditorPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
BayesIm bayesIm = wrapper.getBayesIm();
Graph graph = bayesIm.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 LoadBayesImXsdlXmlAction(wrapper, this));
file.add(new SaveScreenshot(this, true, "Save Screenshot..."));
file.add(new SaveComponentImage(workbench, "Save Graph Image..."));
setLayout(new BorderLayout());
panel.add(menuBar, BorderLayout.NORTH);
wizard = new BayesImEditorWizard(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);
panel.add(splitPane, BorderLayout.CENTER);
setName("Bayes IM 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());
}
}
});
targetPanel.add(panel, BorderLayout.CENTER);
validate();
}
Aggregations