Search in sources :

Example 6 with BayesIm

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);
}
Also used : MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) BayesIm(edu.cmu.tetrad.bayes.BayesIm) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) BayesPm(edu.cmu.tetrad.bayes.BayesPm) Test(org.junit.Test)

Example 7 with BayesIm

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.");
    }
}
Also used : MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) BayesIm(edu.cmu.tetrad.bayes.BayesIm) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) BayesPm(edu.cmu.tetrad.bayes.BayesPm)

Example 8 with BayesIm

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;
}
Also used : BayesIm(edu.cmu.tetrad.bayes.BayesIm)

Example 9 with BayesIm

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());
}
Also used : ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) BayesIm(edu.cmu.tetrad.bayes.BayesIm) TableCellEditor(javax.swing.table.TableCellEditor)

Example 10 with BayesIm

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();
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) Graph(edu.cmu.tetrad.graph.Graph) GraphWorkbench(edu.cmu.tetradapp.workbench.GraphWorkbench) BayesIm(edu.cmu.tetrad.bayes.BayesIm)

Aggregations

BayesIm (edu.cmu.tetrad.bayes.BayesIm)36 MlBayesIm (edu.cmu.tetrad.bayes.MlBayesIm)21 BayesPm (edu.cmu.tetrad.bayes.BayesPm)18 Test (org.junit.Test)14 Graph (edu.cmu.tetrad.graph.Graph)7 Node (edu.cmu.tetrad.graph.Node)7 DataSet (edu.cmu.tetrad.data.DataSet)6 Dag (edu.cmu.tetrad.graph.Dag)5 Algorithm (edu.cmu.tetrad.algcomparison.algorithm.Algorithm)3 GraphNode (edu.cmu.tetrad.graph.GraphNode)3 Parameters (edu.cmu.tetrad.util.Parameters)3 GeneralBootstrapTest (edu.pitt.dbmi.algo.bootstrap.GeneralBootstrapTest)3 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Element (nu.xom.Element)3 RandomGraph (edu.cmu.tetrad.algcomparison.graph.RandomGraph)2 ChiSquare (edu.cmu.tetrad.algcomparison.independence.ChiSquare)2 IndependenceWrapper (edu.cmu.tetrad.algcomparison.independence.IndependenceWrapper)2 BdeuScore (edu.cmu.tetrad.algcomparison.score.BdeuScore)2