Search in sources :

Example 31 with BayesIm

use of edu.cmu.tetrad.bayes.BayesIm 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]);
}
Also used : Histogram(edu.cmu.tetrad.data.Histogram) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Dag(edu.cmu.tetrad.graph.Dag) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) BayesIm(edu.cmu.tetrad.bayes.BayesIm) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) BayesPm(edu.cmu.tetrad.bayes.BayesPm) Test(org.junit.Test)

Example 32 with BayesIm

use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.

the class BayesUpdaterEditor 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 33 with BayesIm

use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.

the class BayesEstimatorNodeEditingTable method getNumParents.

private int getNumParents() {
    Model editingTableModel = (Model) getModel();
    BayesIm bayesIm = editingTableModel.getBayesIm();
    int nodeIndex = editingTableModel.getNodeIndex();
    return bayesIm.getNumParents(nodeIndex);
}
Also used : BayesIm(edu.cmu.tetrad.bayes.BayesIm) AbstractTableModel(javax.swing.table.AbstractTableModel) TableModel(javax.swing.table.TableModel)

Example 34 with BayesIm

use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.

the class BayesImNodeEditingTable 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();
            BayesImNodeEditingTable editingTable = BayesImNodeEditingTable.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;
            }
            BayesImNodeEditingTable editingTable = BayesImNodeEditingTable.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;
                }
            }
            BayesImNodeEditingTable editingTable = BayesImNodeEditingTable.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++) {
                BayesImNodeEditingTable editingTable = BayesImNodeEditingTable.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();
            BayesImNodeEditingTable editingTable = BayesImNodeEditingTable.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;
                }
            }
            BayesImNodeEditingTable editingTable = BayesImNodeEditingTable.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 35 with BayesIm

use of edu.cmu.tetrad.bayes.BayesIm in project tetrad by cmu-phil.

the class TestBayesIm method testAddRemoveValues.

/**
 * Tests whether the BayesIm does the right thing in a very simple case
 * where values of a nodes are added or removed from the BayesPm. Start with
 * graph a -> b <- c, construct and fill in probability tables in BayesIm.
 * Then add edge c -> b "manually." This should create a table of values for
 * c that is unspecified, and it should double up the rows from b. Then
 * remove the node c. Now the table for b should be completely unspecified.
 */
@Test
public void testAddRemoveValues() {
    Node a = new GraphNode("a");
    Node b = new GraphNode("b");
    Node c = new GraphNode("c");
    Dag dag = new Dag();
    dag.addNode(a);
    dag.addNode(b);
    dag.addNode(c);
    dag.addDirectedEdge(a, b);
    dag.addDirectedEdge(c, b);
    assertTrue(Edges.isDirectedEdge(dag.getEdge(a, b)));
    BayesPm bayesPm = new BayesPm(dag, 3, 3);
    BayesIm bayesIm = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
    bayesPm.setNumCategories(a, 4);
    bayesPm.setNumCategories(c, 4);
    BayesIm bayesIm2 = new MlBayesIm(bayesPm, bayesIm, MlBayesIm.MANUAL);
    bayesPm.setNumCategories(a, 2);
    BayesIm bayesIm3 = new MlBayesIm(bayesPm, bayesIm2, MlBayesIm.MANUAL);
    bayesPm.setNumCategories(b, 2);
    BayesIm bayesIm4 = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
    for (int node = 0; node < bayesIm4.getNumNodes(); node++) {
        for (int row = 0; row < bayesIm4.getNumRows(node); row++) {
            for (int col = 0; col < bayesIm4.getNumColumns(node); col++) {
                bayesIm4.setProbability(node, row, col, Double.NaN);
            }
        }
    }
    double[][] aTable = { { .2, .8 } };
    double[][] bTable = { { .1, .9 }, { .7, .3 }, { .3, .7 }, { .5, .5 }, { .09, .91 }, { .6, .4 }, { .2, .8 }, { .8, .2 } };
    double[][] cTable = { { .1, .2, .3, .4 } };
    int _a = bayesIm.getNodeIndex(a);
    for (int row = 0; row < bayesIm4.getNumRows(_a); row++) {
        for (int col = 0; col < bayesIm4.getNumColumns(_a); col++) {
            bayesIm4.setProbability(_a, row, col, aTable[row][col]);
        }
    }
    int _b = bayesIm.getNodeIndex(b);
    for (int row = 0; row < bayesIm4.getNumRows(_b); row++) {
        for (int col = 0; col < bayesIm4.getNumColumns(_b); col++) {
            bayesIm4.setProbability(_b, row, col, bTable[row][col]);
        }
    }
    int _c = bayesIm.getNodeIndex(c);
    for (int row = 0; row < bayesIm4.getNumRows(_c); row++) {
        for (int col = 0; col < bayesIm4.getNumColumns(_c); col++) {
            bayesIm4.setProbability(_c, row, col, cTable[row][col]);
        }
    }
}
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)

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