Search in sources :

Example 11 with BayesIm

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

Example 12 with BayesIm

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));
}
Also used : MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) Graph(edu.cmu.tetrad.graph.Graph) BayesIm(edu.cmu.tetrad.bayes.BayesIm) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) BayesPm(edu.cmu.tetrad.bayes.BayesPm)

Example 13 with BayesIm

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...");
    }
}
Also used : Builder(nu.xom.Builder) IOException(java.io.IOException) Document(nu.xom.Document) XdslXmlParser(edu.cmu.tetrad.search.XdslXmlParser) BayesIm(edu.cmu.tetrad.bayes.BayesIm) ParsingException(nu.xom.ParsingException) File(java.io.File)

Example 14 with BayesIm

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...");
    }
}
Also used : Builder(nu.xom.Builder) IOException(java.io.IOException) Document(nu.xom.Document) BayesXmlParser(edu.cmu.tetrad.bayes.BayesXmlParser) BayesIm(edu.cmu.tetrad.bayes.BayesIm) ParsingException(nu.xom.ParsingException) File(java.io.File)

Example 15 with BayesIm

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