51 protected double sigma;
52 protected static final double RAC2PI = 2.50662827463100050;
54 private static final double[] AbarF = { 6.10143081923200418E-1, -4.34841272712577472E-1, 1.76351193643605501E-1,
55 -6.07107956092494149E-2, 1.77120689956941145E-2, -4.32111938556729382E-3, 8.54216676887098679E-4,
56 -1.27155090609162743E-4, 1.12481672436711895E-5, 3.13063885421820973E-7, -2.70988068537762022E-7,
57 3.07376227014076884E-8, 2.51562038481762294E-9, -1.02892992132031913E-9, 2.99440521199499394E-11,
58 2.60517896872669363E-11, -2.63483992417196939E-12, -6.43404509890636443E-13, 1.12457401801663447E-13,
59 1.7281533389986098E-14, -4.2641016949424E-15, -5.4537197788E-16, 1.5869760776E-16, 2.08998378E-17,
79 double z = (x - mu) / sigma;
80 return Math.exp(-0.5 * z * z) / (RAC2PI * sigma);
83 public double cdf(
double x) {
84 return cdf01((x - mu) / sigma);
87 public double barF(
double x) {
88 return barF01((x - mu) / sigma);
104 return NormalDist.getStandardDeviation(mu, sigma);
111 return Math.exp(-0.5 * x * x) / RAC2PI;
118 public static double density(
double mu,
double sigma,
double x) {
120 throw new IllegalArgumentException(
"sigma <= 0");
121 double z = (x - mu) / sigma;
122 return Math.exp(-0.5 * z * z) / (RAC2PI * sigma);
130 private static final int COEFFMAX = 24;
132 private static final double NORMAL2_A[] = { 6.10143081923200417926465815756e-1, -4.34841272712577471828182820888e-1,
133 1.76351193643605501125840298123e-1, -6.0710795609249414860051215825e-2, 1.7712068995694114486147141191e-2,
134 -4.321119385567293818599864968e-3, 8.54216676887098678819832055e-4, -1.27155090609162742628893940e-4,
135 1.1248167243671189468847072e-5, 3.13063885421820972630152e-7, -2.70988068537762022009086e-7,
136 3.0737622701407688440959e-8, 2.515620384817622937314e-9, -1.028929921320319127590e-9,
137 2.9944052119949939363e-11, 2.6051789687266936290e-11, -2.634839924171969386e-12, -6.43404509890636443e-13,
138 1.12457401801663447e-13, 1.7281533389986098e-14, -4.264101694942375e-15, -5.45371977880191e-16,
139 1.58697607761671e-16, 2.0899837844334e-17, -5.900526869409e-18, -9.41893387554e-19
151 public static double cdf01(
double x) {
167 t = (x - 3.75) / (x + 3.75);
168 r = 1.0 - 0.5 * Math.exp(-x * x) *
Num.
evalCheby(NORMAL2_A, COEFFMAX, t);
170 t = (x - 3.75) / (x + 3.75);
171 r = 0.5 * Math.exp(-x * x) *
Num.
evalCheby(NORMAL2_A, COEFFMAX, t);
181 public static double cdf(
double mu,
double sigma,
double x) {
183 throw new IllegalArgumentException(
"sigma <= 0");
184 return cdf01((x - mu) / sigma);
196 final double KK = 5.30330085889910643300;
212 t = (x - KK) / (x + KK);
214 y = y * Math.exp(-x * x / 2.0) / 2.0;
229 public static double barF(
double mu,
double sigma,
double x) {
231 throw new IllegalArgumentException(
"sigma <= 0");
232 return barF01((x - mu) / sigma);
235 private static final double[] InvP1 = { 0.160304955844066229311E2, -0.90784959262960326650E2,
236 0.18644914861620987391E3, -0.16900142734642382420E3, 0.6545466284794487048E2, -0.864213011587247794E1,
237 0.1760587821390590 };
239 private static final double[] InvQ1 = { 0.147806470715138316110E2, -0.91374167024260313396E2,
240 0.21015790486205317714E3, -0.22210254121855132366E3, 0.10760453916055123830E3, -0.206010730328265443E2,
243 private static final double[] InvP2 = { -0.152389263440726128E-1, 0.3444556924136125216, -0.29344398672542478687E1,
244 0.11763505705217827302E2, -0.22655292823101104193E2, 0.19121334396580330163E2, -0.5478927619598318769E1,
245 0.237516689024448000 };
247 private static final double[] InvQ2 = { -0.108465169602059954E-1, 0.2610628885843078511, -0.24068318104393757995E1,
248 0.10695129973387014469E2, -0.23716715521596581025E2, 0.24640158943917284883E2, -0.10014376349783070835E2,
251 private static final double[] InvP3 = { 0.56451977709864482298E-4, 0.53504147487893013765E-2, 0.12969550099727352403,
252 0.10426158549298266122E1, 0.28302677901754489974E1, 0.26255672879448072726E1, 0.20789742630174917228E1,
253 0.72718806231556811306, 0.66816807711804989575E-1, -0.17791004575111759979E-1, 0.22419563223346345828E-2 };
255 private static final double[] InvQ3 = { 0.56451699862760651514E-4, 0.53505587067930653953E-2, 0.12986615416911646934,
256 0.10542932232626491195E1, 0.30379331173522206237E1, 0.37631168536405028901E1, 0.38782858277042011263E1,
257 0.20372431817412177929E1, 0.1E1 };
275 if (u < 0.0 || u > 1.0)
276 throw new IllegalArgumentException(
"u is not in [0, 1]");
278 return Double.NEGATIVE_INFINITY;
280 return Double.POSITIVE_INFINITY;
294 for (i = 6; i >= 0; i--) {
295 v = v * y + InvP1[i];
296 w = w * y + InvQ1[i];
300 }
else if (x <= 0.9375) {
301 y = x * x - 0.87890625;
303 for (i = 7; i >= 0; i--) {
304 v = v * y + InvP2[i];
305 w = w * y + InvQ2[i];
311 y = 1.0 / Math.sqrt(-Math.log(1.0 - x));
313 y = 1.0 / Math.sqrt(-Math.log(2.0 * u));
315 for (i = 10; i >= 0; i--)
316 v = v * y + InvP3[i];
318 for (i = 8; i >= 0; i--)
319 w = w * y + InvQ3[i];
325 final double RACPI = 1.77245385090551602729;
326 w = Math.exp(-z * z) / RACPI;
331 for (i = 0; i < 6; ++i) {
332 term *= -(2 * i + 1) / y;
336 z -= u / w - 0.5 * v / z;
352 public static double inverseF(
double mu,
double sigma,
double u) {
354 throw new IllegalArgumentException(
"sigma <= 0");
375 public static double[]
getMLE(
double[] x,
int n) {
377 throw new IllegalArgumentException(
"n <= 0");
379 double[] parameters =
new double[2];
381 for (
int i = 0; i < n; i++)
383 parameters[0] = sum / n;
386 for (
int i = 0; i < n; i++)
387 sum = sum + (x[i] - parameters[0]) * (x[i] - parameters[0]);
388 parameters[1] = Math.sqrt(sum / n);
403 double parameters[] =
getMLE(x, n);
404 return new NormalDist(parameters[0], parameters[1]);
413 public static double getMean(
double mu,
double sigma) {
415 throw new IllegalArgumentException(
"sigma <= 0");
429 throw new IllegalArgumentException(
"sigma <= 0");
431 return (sigma * sigma);
463 throw new IllegalArgumentException(
"sigma <= 0");
473 double[] retour = { mu, sigma };
481 return getClass().getSimpleName() +
" : mu = " + mu +
", sigma = " + sigma;