25package umontreal.ssj.probdist;
27import java.util.Formatter;
28import java.util.Locale;
29import umontreal.ssj.util.Num;
30import umontreal.ssj.util.PrintfFormat;
31import java.util.Arrays;
32import java.io.IOException;
34import java.io.BufferedReader;
59 private double[] sortedObs;
60 private double[] diffObs;
62 private double sampleMean;
63 private double sampleVariance;
64 private double sampleStandardDeviation;
73 throw new IllegalArgumentException(
"Two or more observations are needed");
76 sortedObs =
new double[n];
77 System.arraycopy(obs, 0, sortedObs, 0, n);
89 BufferedReader inb =
new BufferedReader(in);
90 double[] data =
new double[5];
92 while ((li = inb.readLine()) !=
null) {
95 while (index < li.length() && (li.charAt(index) ==
'+' || li.charAt(index) ==
'-' || li.charAt(index) ==
'e'
96 || li.charAt(index) ==
'E' || li.charAt(index) ==
'.' || Character.isDigit(li.charAt(index))))
100 li = li.substring(0, index);
101 if (!li.equals(
"")) {
103 data[n++] = Double.parseDouble(li);
104 if (n >= data.length) {
105 double[] newData =
new double[2 * n];
106 System.arraycopy(data, 0, newData, 0, data.length);
109 }
catch (NumberFormatException nfe) {
113 sortedObs =
new double[n];
114 System.arraycopy(data, 0, sortedObs, 0, n);
120 if (x < sortedObs[0] || x >= sortedObs[n - 1])
122 for (
int i = 0; i < (n - 1); i++) {
123 if (x >= sortedObs[i] && x < sortedObs[i + 1])
124 return 1.0 / ((n - 1) * diffObs[i]);
126 throw new IllegalStateException();
129 public double cdf(
double x) {
131 if (x <= sortedObs[0])
133 if (x >= sortedObs[n - 1])
135 for (
int i = 0; i < (n - 1); i++) {
136 if (x >= sortedObs[i] && x < sortedObs[i + 1])
137 return i / (n - 1.0) + (x - sortedObs[i]) / ((n - 1.0) * diffObs[i]);
139 throw new IllegalStateException();
144 if (x <= sortedObs[0])
146 if (x >= sortedObs[n - 1])
148 for (
int i = 0; i < (n - 1); i++) {
149 if (x >= sortedObs[i] && x < sortedObs[i + 1])
150 return (n - 1.0 - i) / (n - 1.0) - (x - sortedObs[i]) / ((n - 1.0) * diffObs[i]);
152 throw new IllegalStateException();
157 throw new IllegalArgumentException(
"u is not in [0,1]");
161 return sortedObs[n - 1];
162 double p = (n - 1) * u;
163 int i = (int) Math.floor(p);
165 return sortedObs[n - 1];
167 return sortedObs[i] + (p - i) * diffObs[i];
175 return sampleVariance;
179 return sampleStandardDeviation;
182 private void init() {
183 Arrays.sort(sortedObs);
185 diffObs =
new double[sortedObs.length];
187 for (
int i = 0; i < diffObs.length - 1; i++) {
188 diffObs[i] = sortedObs[i + 1] - sortedObs[i];
191 diffObs[n - 1] = 0.0;
192 sum += sortedObs[n - 1];
193 sampleMean = sum / n;
195 for (
int i = 0; i < n; i++) {
196 double coeff = (sortedObs[i] - sampleMean);
197 sum += coeff * coeff;
199 sampleVariance = sum / (n - 1);
200 sampleStandardDeviation = Math.sqrt(sampleVariance);
201 supportA = sortedObs[0] * (1.0 - Num.DBL_EPSILON);
202 supportB = sortedObs[n - 1] * (1.0 + Num.DBL_EPSILON);
230 return sampleVariance;
237 return sampleStandardDeviation;
244 double[] retour =
new double[n];
245 System.arraycopy(sortedObs, 0, retour, 0, n);
253 StringBuilder sb =
new StringBuilder();
254 Formatter formatter =
new Formatter(sb, Locale.US);
256 for (
int i = 0; i < n; i++) {
257 formatter.format(
"%f%n", sortedObs[i]);
259 return sb.toString();
Classes implementing continuous distributions should inherit from this base class.
double getSampleVariance()
Returns the sample variance of the observations.
double density(double x)
Returns , the density evaluated at .
double[] getParams()
Return a table containing parameters of the current distribution.
double getVariance()
Returns the variance.
double cdf(double x)
Returns the distribution function .
int getN()
Returns , the number of observations.
double getSampleMean()
Returns the sample mean of the observations.
double getStandardDeviation()
Returns the standard deviation.
PiecewiseLinearEmpiricalDist(Reader in)
Constructs a new empirical distribution using the observations read from the reader in.
double getMean()
Returns the mean.
double getSampleStandardDeviation()
Returns the sample standard deviation of the observations.
String toString()
Returns a String containing information about the current distribution.
PiecewiseLinearEmpiricalDist(double[] obs)
Constructs a new piecewise-linear distribution using all the observations stored in obs.
double barF(double x)
Returns the complementary distribution function.
double inverseF(double u)
Returns the inverse distribution function .
double getObs(int i)
Returns the value of .