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;
}
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]);
}
Aggregations