Search in sources :

Example 1 with Histogram

use of edu.cmu.tetrad.data.Histogram in project tetrad by cmu-phil.

the class HistogramAction method createHistogramPanel.

/**
 * Creates a dialog that is showing the histogram for the given node (if null
 * one is selected for you)
 */
private JPanel createHistogramPanel(Node selected) {
    DataSet dataSet = (DataSet) dataEditor.getSelectedDataModel();
    Histogram histogram = new Histogram(dataSet);
    histogram.setTarget(selected == null ? null : selected.getName());
    HistogramView view = new HistogramView(histogram);
    Box box = Box.createHorizontalBox();
    box.add(view);
    box.add(Box.createHorizontalStrut(5));
    box.add(Box.createHorizontalGlue());
    Box vBox = Box.createVerticalBox();
    vBox.add(Box.createVerticalStrut(15));
    vBox.add(box);
    vBox.add(Box.createVerticalStrut(5));
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(vBox, BorderLayout.CENTER);
    return panel;
}
Also used : Histogram(edu.cmu.tetrad.data.Histogram) DataSet(edu.cmu.tetrad.data.DataSet)

Example 2 with Histogram

use of edu.cmu.tetrad.data.Histogram 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)

Aggregations

DataSet (edu.cmu.tetrad.data.DataSet)2 Histogram (edu.cmu.tetrad.data.Histogram)2 BayesIm (edu.cmu.tetrad.bayes.BayesIm)1 BayesPm (edu.cmu.tetrad.bayes.BayesPm)1 MlBayesIm (edu.cmu.tetrad.bayes.MlBayesIm)1 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)1 Dag (edu.cmu.tetrad.graph.Dag)1 Node (edu.cmu.tetrad.graph.Node)1 SemIm (edu.cmu.tetrad.sem.SemIm)1 SemPm (edu.cmu.tetrad.sem.SemPm)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1