1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.jstats.distributions;
11
12 import java.awt.Dimension;
13
14 import javax.swing.JFrame;
15 import javax.swing.JTabbedPane;
16
17 import nl.tudelft.simulation.event.Event;
18 import nl.tudelft.simulation.event.EventType;
19 import nl.tudelft.simulation.jstats.charts.histogram.Histogram;
20 import nl.tudelft.simulation.jstats.charts.histogram.HistogramSeries;
21 import nl.tudelft.simulation.jstats.streams.MersenneTwister;
22 import nl.tudelft.simulation.jstats.streams.StreamInterface;
23
24 /***
25 * The DistributionsGUIInspector provides graphical insight in the randomness of
26 * different streams.
27 * <p>
28 * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
29 * University of Technology </a>, the Netherlands. <br>
30 * See for project information <a
31 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
32 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
33 * License (GPL) </a>, no warranty <br>
34 *
35 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
36 * Jacobs </a>
37 * @version 1.0, 2004-03-18
38 * @since 1.2
39 */
40 public class DistributionsGUIInspector extends JTabbedPane
41 {
42 /***
43 * constructs a new DistributionsGUIInspector
44 */
45 public DistributionsGUIInspector()
46 {
47 this.setPreferredSize(new Dimension(500, 500));
48 StreamInterface stream = new MersenneTwister();
49
50 DistContinuous distribution = new DistUniform(stream, 0, 1.0);
51 this.add(distribution.toString(), this.createHistogram(distribution,
52 new double[]{0, 1}).getSwingPanel());
53
54 distribution = new DistConstant(stream, 10.0);
55 this.add(distribution.toString(), this.createHistogram(distribution,
56 new double[]{0, 10}).getSwingPanel());
57
58
59 distribution = new DistExponential(stream, 1.0);
60 this.add(distribution.toString(), this.createHistogram(distribution,
61 new double[]{0, 6}).getSwingPanel());
62
63 distribution = new DistGamma(stream, 2.0, 1.0);
64 this.add(distribution.toString(), this.createHistogram(distribution,
65 new double[]{0, 9}).getSwingPanel());
66
67 distribution = new DistWeibull(stream, 2.0, 1.0);
68 this.add(distribution.toString(), this.createHistogram(distribution,
69 new double[]{0, 4}).getSwingPanel());
70
71 distribution = new DistNormal(stream, 0.0, 1.0);
72 this.add(distribution.toString(), this.createHistogram(distribution,
73 new double[]{-3, 3}).getSwingPanel());
74
75 distribution = new DistLogNormal(stream, 0.0, 1);
76 this.add(distribution.toString(), this.createHistogram(distribution,
77 new double[]{0, 5}).getSwingPanel());
78
79 distribution = new DistBeta(stream, 1.5, 5);
80 this.add(distribution.toString(), this.createHistogram(distribution,
81 new double[]{0, 1}).getSwingPanel());
82
83 distribution = new DistBeta(stream, 5, 1.5);
84 this.add(distribution.toString(), this.createHistogram(distribution,
85 new double[]{0, 1}).getSwingPanel());
86
87 distribution = new DistPearson5(stream, 1, 1);
88 this.add(distribution.toString(), this.createHistogram(distribution,
89 new double[]{0, 5}).getSwingPanel());
90
91 distribution = new DistPearson6(stream, 1, 2, 4);
92 this.add(distribution.toString(), this.createHistogram(distribution,
93 new double[]{0, 5}).getSwingPanel());
94
95 distribution = new DistTriangular(stream, 1, 1.5, 7);
96 this.add(distribution.toString(), this.createHistogram(distribution,
97 new double[]{0, 7}).getSwingPanel());
98 }
99
100 /***
101 * creates a histogram of a discrete distribution
102 *
103 * @param distribution the distribution to plot
104 * @param domain the domain to show on the histogram
105 * @return Histogram the histogram.
106 */
107 private Histogram createHistogram(final Dist distribution,
108 final double[] domain)
109 {
110 Histogram histogram = new Histogram(distribution.toString(), domain, 20);
111 HistogramSeries series = new HistogramSeries(distribution.toString(),
112 domain, null, 20);
113 EventType accept = new EventType("DISTRIBUTION_VALUE");
114 for (int i = 0; i < 10000; i++)
115 {
116 if (distribution instanceof DistContinuous)
117 {
118 Double value = new Double(((DistContinuous) distribution)
119 .draw());
120 series.notify(new Event(accept, this, value));
121 } else
122 {
123 series.notify(new Event(accept, this, new Long(
124 ((DistDiscrete) distribution).draw())));
125 }
126 }
127 histogram.getDataset().addSeries(series);
128 return histogram;
129 }
130
131 /***
132 * executes the main program
133 *
134 * @param args the commandline arguments
135 */
136 public static void main(final String[] args)
137 {
138 JFrame app = new JFrame("Distributions gui tester");
139 app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
140
141 app.getContentPane().add(new DistributionsGUIInspector());
142 app.pack();
143 app.setVisible(true);
144 }
145 }