SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
DiscreteDistIntChart.java
1/*
2 * Class: DiscreteDistIntChart
3 * Description:
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author Richard Simard
9 * @since May 2008
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.charts;
26
27import umontreal.ssj.probdist.DiscreteDistributionInt;
28
29import javax.swing.JFrame;
30import java.awt.*;
31
39 protected DiscreteDistributionInt dist;
40 protected int a, b;
41 protected XYLineChart cdfChart;
42 protected XYLineChart probChart;
43
51 this.dist = dist;
52 this.a = 0;
53 this.b = 0;
54 }
55
65 public DiscreteDistIntChart(DiscreteDistributionInt dist, int a, int b) {
66 this.dist = dist;
67 if (a >= b)
68 throw new IllegalArgumentException("a is bigger than b");
69 this.a = a;
70 this.b = b;
71 init();
72 }
73
74 private void init() {
75 int m = b - a + 1;
76 double[][] cdf = new double[2][m];
77 double[][] probability = new double[2][m];
78
79 for (int i = 0; i < m; i++) {
80 cdf[0][i] = i + a;
81 cdf[1][i] = dist.cdf(i + a);
82 probability[0][i] = i + a;
83 probability[1][i] = dist.prob(i + a);
84 }
85
86 double[][] cdfFinal = new double[2][2 * (m - 1)];
87 for (int i = 0; i < m - 1; i++) {
88 cdfFinal[0][2 * i] = cdf[0][i];
89 cdfFinal[0][2 * i + 1] = cdf[0][i + 1];
90 cdfFinal[1][2 * i] = cdf[1][i];
91 cdfFinal[1][2 * i + 1] = cdf[1][i];
92 }
93
94 cdfChart = new XYLineChart("cdf: " + dist.toString(), "", "", cdfFinal);
95 probChart = new XYLineChart("probability: " + dist.toString(), "", "", probability);
96 cdfChart.setprobFlag(true);
97 probChart.setprobFlag(true);
98
99 // Only for tikZ
100 XYListSeriesCollection collec = cdfChart.getSeriesCollection();
101 collec.setColor(0, Color.BLUE);
102 collec.setPlotStyle(0, "thick");
103 collec.setMarksType(0, "only marks");
104
105 collec = probChart.getSeriesCollection();
106 collec.setColor(0, Color.ORANGE);
107 collec.setPlotStyle(0, "ycomb");
108 collec.setMarksType(0, "*");
109 collec.setDashPattern(0, "solid");
110 }
111
112 private void testParam() {
113 if (a == 0 && b == 0) {
114 double mean = dist.getMean();
115 double sd = dist.getStandardDeviation();
116 int xa = (int) Math.round(mean - 3.0 * sd);
117 if (xa < dist.getXinf())
118 xa = dist.getXinf();
119 int xb = (int) Math.round(mean + 3.0 * sd);
120 if (xb > dist.getXsup())
121 xb = dist.getXsup();
122 setParam(xa, xb);
123 }
124 }
125
139 public JFrame viewCdf(int width, int height, int a, int b) {
140 setParam(a, b);
141 return cdfChart.view(width, height);
142 }
143
154 public JFrame viewCdf(int width, int height) {
155 testParam();
156 return cdfChart.view(width, height);
157 }
158
172 public JFrame viewProb(int width, int height, int a, int b) {
173 setParam(a, b);
174 return probChart.viewBar(width, height);
175 }
176
187 public JFrame viewProb(int width, int height) {
188 testParam();
189 return probChart.viewBar(width, height);
190 }
191
198 public void setParam(int a, int b) {
199 if (a >= b)
200 throw new IllegalArgumentException("a is bigger than b" + a + " " + b);
201 this.a = a;
202 this.b = b;
203 init();
204 }
205
218 public String toLatexCdf(int width, int height) {
219 testParam();
220 return cdfChart.toLatex(width, height);
221 }
222
230 public String toLatexProb(int width, int height) {
231 testParam();
232 return probChart.toLatex(width, height);
233 }
234
241 return cdfChart;
242 }
243
250 return probChart;
251 }
252
253}
JFrame viewCdf(int width, int height)
Similar to method viewCdf above.
String toLatexProb(int width, int height)
Similar to toLatexCdf, but for the probability instead of the cdf.
DiscreteDistIntChart(DiscreteDistributionInt dist)
Constructor for a new DiscreteDistIntChart instance used to plot the probabilities of the discrete di...
JFrame viewCdf(int width, int height, int a, int b)
Displays a chart of the cumulative distribution function (cdf) over the interval on the screen using...
DiscreteDistIntChart(DiscreteDistributionInt dist, int a, int b)
Constructor for a new DiscreteDistIntChart instance used to plot the probabilities of the discrete di...
XYLineChart getCdf()
Returns the chart of the cdf.
String toLatexCdf(int width, int height)
Exports a chart of the cumulative probability to a LaTeX source code using PGF/TikZ.
void setParam(int a, int b)
Sets the parameters and for this object.
XYLineChart getProb()
Returns the chart of the probability.
JFrame viewProb(int width, int height, int a, int b)
Displays a chart of the probability mass function over the interval.
JFrame viewProb(int width, int height)
Similar to method viewProb above.
void setprobFlag(boolean flag)
Must be set true when plotting probabilities, false otherwise.
Definition XYChart.java:133
Provides tools to create and manage curve plots.
XYListSeriesCollection getSeriesCollection()
Returns the chart’s dataset.
Classes implementing discrete distributions over the integers should inherit from this class.
int getXsup()
Returns the upper limit of the support of the probability mass function.
abstract double prob(int x)
Returns , the probability of .
int getXinf()
Returns the lower limit of the support of the probability mass function.
double getStandardDeviation()
Returns the standard deviation of the distribution function.
double getMean()
Returns the mean of the distribution function.