SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
EmpiricalDist.java
1/*
2 * Class: EmpiricalDist
3 * Description: empirical discrete distribution function
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
9 * @since
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.probdist;
26
27import java.util.Formatter;
28import java.util.Locale;
29import java.util.Arrays;
30import java.io.IOException;
31import java.io.Reader;
32import java.io.BufferedReader;
33import umontreal.ssj.util.Misc;
34import umontreal.ssj.util.PrintfFormat;
35
47public class EmpiricalDist extends DiscreteDistribution {
48 private int n = 0;
49 private double sampleMean;
50 private double sampleVariance;
51 private double sampleStandardDeviation;
52
60 public EmpiricalDist(double[] obs) {
61 if (obs.length <= 1)
62 throw new IllegalArgumentException("Two or more observations are needed");
63 nVal = n = obs.length;
64 sortedVal = new double[n];
65 System.arraycopy(obs, 0, sortedVal, 0, n);
66 init();
67 }
68
77 public EmpiricalDist(Reader in) throws IOException {
78 BufferedReader inb = new BufferedReader(in);
79 double[] data = new double[5];
80 n = 0;
81 String li;
82 while ((li = inb.readLine()) != null) {
83 li = li.trim();
84
85 // look for the first non-digit character on the read line
86 int index = 0;
87 while (index < li.length() && (li.charAt(index) == '+' || li.charAt(index) == '-' || li.charAt(index) == 'e'
88 || li.charAt(index) == 'E' || li.charAt(index) == '.' || Character.isDigit(li.charAt(index))))
89 ++index;
90
91 // truncate the line
92 li = li.substring(0, index);
93 if (!li.equals("")) {
94 try {
95 data[n++] = Double.parseDouble(li);
96 if (n >= data.length) {
97 double[] newData = new double[2 * n];
98 System.arraycopy(data, 0, newData, 0, data.length);
99 data = newData;
100 }
101 } catch (NumberFormatException nfe) {
102 }
103 }
104 }
105 sortedVal = new double[n];
106 System.arraycopy(data, 0, sortedVal, 0, n);
107 nVal = n;
108 init();
109 }
110
111 public double prob(int i) {
112 if (i >= 0 && i < n)
113 return 1.0 / n;
114 throw new IllegalStateException();
115 }
116
117 public double cdf(double x) {
118 if (x < sortedVal[0])
119 return 0;
120 if (x >= sortedVal[n - 1])
121 return 1;
122 for (int i = 0; i < (n - 1); i++) {
123 if (x >= sortedVal[i] && x < sortedVal[i + 1])
124 return (double) (i + 1) / n;
125 }
126 throw new IllegalStateException();
127 }
128
129 public double barF(double x) {
130 if (x <= sortedVal[0])
131 return 1;
132 if (x > sortedVal[n - 1])
133 return 0;
134 for (int i = 0; i < (n - 1); i++) {
135 if (x > sortedVal[i] && x <= sortedVal[i + 1])
136 return ((double) n - 1 - i) / n;
137 }
138 throw new IllegalStateException();
139 }
140
141 public double inverseF(double u) {
142 if (u < 0 || u > 1)
143 throw new IllegalArgumentException("u is not in [0,1]");
144 if (u == 1.0)
145 return sortedVal[n - 1];
146 int i = (int) Math.floor((double) n * u);
147 return sortedVal[i];
148 }
149
150 private void init() {
151 // Arrays.sort (sortedVal);
152 double sum = 0.0;
153 for (int i = 0; i < sortedVal.length; i++) {
154 sum += sortedVal[i];
155 }
156 sampleMean = sum / n;
157 sum = 0.0;
158 for (int i = 0; i < n; i++) {
159 double coeff = (sortedVal[i] - sampleMean);
160 sum += coeff * coeff;
161 }
162 sampleVariance = sum / (n - 1);
163 sampleStandardDeviation = Math.sqrt(sampleVariance);
164 supportA = sortedVal[0];
165 supportB = sortedVal[n - 1];
166 xmin = 0;
167 xmax = n - 1;
168 }
169
170 public double getMean() {
171 return sampleMean;
172 }
173
174 public double getStandardDeviation() {
175 return sampleStandardDeviation;
176 }
177
178 public double getVariance() {
179 return sampleVariance;
180 }
181
189 public double getMedian() {
190 if ((n % 2) == 0)
191 return ((sortedVal[n / 2 - 1] + sortedVal[n / 2]) / 2.0);
192 else
193 return sortedVal[(n - 1) / 2];
194 }
195
207 public static double getMedian(double obs[], int n) {
208 return Misc.getMedian(obs, n);
209 }
210
214 public int getN() {
215 return n;
216 }
217
221 public double getObs(int i) {
222 return sortedVal[i];
223 }
224
228 public double getSampleMean() {
229 return sampleMean;
230 }
231
235 public double getSampleVariance() {
236 return sampleVariance;
237 }
238
243 return sampleStandardDeviation;
244 }
245
250 public double getInterQuartileRange() {
251 int j = n / 2;
252 double lowerqrt = 0, upperqrt = 0;
253 if (j % 2 == 1) {
254 lowerqrt = sortedVal[(j + 1) / 2 - 1];
255 upperqrt = sortedVal[n - (j + 1) / 2];
256 } else {
257 lowerqrt = 0.5 * (sortedVal[j / 2 - 1] + sortedVal[j / 2 + 1 - 1]);
258 upperqrt = 0.5 * (sortedVal[n - j / 2] + sortedVal[n - j / 2 - 1]);
259 }
260 double h = upperqrt - lowerqrt;
261 if (h < 0)
262 throw new IllegalStateException("Observations MUST be sorted");
263 return h;
264 }
265
269 public double[] getParams() {
270 double[] retour = new double[n];
271 System.arraycopy(sortedVal, 0, retour, 0, n);
272 return retour;
273 }
274
278 public String toString() {
279 StringBuilder sb = new StringBuilder();
280 Formatter formatter = new Formatter(sb, Locale.US);
281 formatter.format(getClass().getSimpleName() + PrintfFormat.NEWLINE);
282 for (int i = 0; i < n; i++) {
283 formatter.format("%f%n", sortedVal[i]);
284 }
285 return sb.toString();
286 }
287
288}
double[] getParams()
Return a table containing parameters of the current distribution.
double prob(int i)
Returns , the probability of the -th value, for.
double getMedian()
Returns the median.
String toString()
Returns a String containing information about the current distribution.
EmpiricalDist(double[] obs)
Constructs a new empirical distribution using all the observations stored in obs, and which are assum...
EmpiricalDist(Reader in)
Constructs a new empirical distribution using the observations read from the reader in.
double getSampleVariance()
Returns the sample variance of the observations.
double getMean()
Computes the mean of the distribution.
double getSampleMean()
Returns the sample mean of the observations.
double getSampleStandardDeviation()
Returns the sample standard deviation of the observations.
double getInterQuartileRange()
Returns the interquartile range of the observations, defined as the difference between the third and ...
static double getMedian(double obs[], int n)
Returns the median.
double getVariance()
Computes the variance of the distribution.
double getStandardDeviation()
Computes the standard deviation of the distribution.
double getObs(int i)
Returns the value of , for .
int getN()
Returns , the number of observations.
This class provides miscellaneous functions that are hard to classify.
Definition Misc.java:33
static double getMedian(double[] A, int n)
Returns the median of the first elements of array .
Definition Misc.java:116
This class acts like a StringBuffer which defines new types of append methods.
static final String NEWLINE
End-of-line symbol or line separator.