25package umontreal.ssj.probdist;
27import java.util.Formatter;
28import java.util.Locale;
29import java.util.Arrays;
30import java.io.IOException;
32import java.io.BufferedReader;
33import umontreal.ssj.util.Misc;
34import umontreal.ssj.util.PrintfFormat;
49 private double sampleMean;
50 private double sampleVariance;
51 private double sampleStandardDeviation;
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);
78 BufferedReader inb =
new BufferedReader(in);
79 double[] data =
new double[5];
82 while ((li = inb.readLine()) !=
null) {
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))))
92 li = li.substring(0, index);
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);
101 }
catch (NumberFormatException nfe) {
105 sortedVal =
new double[n];
106 System.arraycopy(data, 0, sortedVal, 0, n);
114 throw new IllegalStateException();
117 public double cdf(
double x) {
118 if (x < sortedVal[0])
120 if (x >= sortedVal[n - 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;
126 throw new IllegalStateException();
130 if (x <= sortedVal[0])
132 if (x > sortedVal[n - 1])
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;
138 throw new IllegalStateException();
143 throw new IllegalArgumentException(
"u is not in [0,1]");
145 return sortedVal[n - 1];
146 int i = (int) Math.floor((
double) n * u);
150 private void init() {
153 for (
int i = 0; i < sortedVal.length; i++) {
156 sampleMean = sum / n;
158 for (
int i = 0; i < n; i++) {
159 double coeff = (sortedVal[i] - sampleMean);
160 sum += coeff * coeff;
162 sampleVariance = sum / (n - 1);
163 sampleStandardDeviation = Math.sqrt(sampleVariance);
164 supportA = sortedVal[0];
165 supportB = sortedVal[n - 1];
175 return sampleStandardDeviation;
179 return sampleVariance;
191 return ((sortedVal[n / 2 - 1] + sortedVal[n / 2]) / 2.0);
193 return sortedVal[(n - 1) / 2];
236 return sampleVariance;
243 return sampleStandardDeviation;
252 double lowerqrt = 0, upperqrt = 0;
254 lowerqrt = sortedVal[(j + 1) / 2 - 1];
255 upperqrt = sortedVal[n - (j + 1) / 2];
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]);
260 double h = upperqrt - lowerqrt;
262 throw new IllegalStateException(
"Observations MUST be sorted");
270 double[] retour =
new double[n];
271 System.arraycopy(sortedVal, 0, retour, 0, n);
279 StringBuilder sb =
new StringBuilder();
280 Formatter formatter =
new Formatter(sb, Locale.US);
282 for (
int i = 0; i < n; i++) {
283 formatter.format(
"%f%n", sortedVal[i]);
285 return sb.toString();
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 inverseF(double u)
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.
static double getMedian(double[] A, int n)
Returns the median of the first elements of array .