1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.distribution.continuous;
24
25 import org.hipparchus.exception.LocalizedCoreFormats;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.hipparchus.special.Erf;
28 import org.hipparchus.util.FastMath;
29 import org.hipparchus.util.MathUtils;
30
31
32
33
34
35
36
37 public class NormalDistribution extends AbstractRealDistribution {
38
39 private static final long serialVersionUID = 20160320L;
40
41 private static final double SQRT2 = FastMath.sqrt(2.0);
42
43 private final double mean;
44
45 private final double standardDeviation;
46
47 private final double logStandardDeviationPlusHalfLog2Pi;
48
49
50
51
52
53 public NormalDistribution() {
54 this(0, 1);
55 }
56
57
58
59
60
61
62
63
64 public NormalDistribution(double mean, double sd)
65 throws MathIllegalArgumentException {
66 if (sd <= 0) {
67 throw new MathIllegalArgumentException(LocalizedCoreFormats.STANDARD_DEVIATION, sd);
68 }
69
70 this.mean = mean;
71 this.standardDeviation = sd;
72 this.logStandardDeviationPlusHalfLog2Pi =
73 FastMath.log(sd) + 0.5 * FastMath.log(2 * FastMath.PI);
74 }
75
76
77
78
79
80
81 public double getMean() {
82 return mean;
83 }
84
85
86
87
88
89
90 public double getStandardDeviation() {
91 return standardDeviation;
92 }
93
94
95 @Override
96 public double density(double x) {
97 return FastMath.exp(logDensity(x));
98 }
99
100
101 @Override
102 public double logDensity(double x) {
103 final double x0 = x - mean;
104 final double x1 = x0 / standardDeviation;
105 return -0.5 * x1 * x1 - logStandardDeviationPlusHalfLog2Pi;
106 }
107
108
109
110
111
112
113
114
115 @Override
116 public double cumulativeProbability(double x) {
117 final double dev = x - mean;
118 if (FastMath.abs(dev) > 40 * standardDeviation) {
119 return dev < 0 ? 0.0d : 1.0d;
120 }
121 return 0.5 * Erf.erfc(-dev / (standardDeviation * SQRT2));
122 }
123
124
125 @Override
126 public double inverseCumulativeProbability(final double p) throws MathIllegalArgumentException {
127 MathUtils.checkRangeInclusive(p, 0, 1);
128 return mean + standardDeviation * SQRT2 * Erf.erfInv(2 * p - 1);
129 }
130
131
132 @Override
133 public double probability(double x0,
134 double x1)
135 throws MathIllegalArgumentException {
136 if (x0 > x1) {
137 throw new MathIllegalArgumentException(LocalizedCoreFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
138 x0, x1, true);
139 }
140 final double denom = standardDeviation * SQRT2;
141 final double v0 = (x0 - mean) / denom;
142 final double v1 = (x1 - mean) / denom;
143 return 0.5 * Erf.erf(v0, v1);
144 }
145
146
147
148
149
150
151 @Override
152 public double getNumericalMean() {
153 return getMean();
154 }
155
156
157
158
159
160
161 @Override
162 public double getNumericalVariance() {
163 final double s = getStandardDeviation();
164 return s * s;
165 }
166
167
168
169
170
171
172
173
174
175
176 @Override
177 public double getSupportLowerBound() {
178 return Double.NEGATIVE_INFINITY;
179 }
180
181
182
183
184
185
186
187
188
189
190 @Override
191 public double getSupportUpperBound() {
192 return Double.POSITIVE_INFINITY;
193 }
194
195
196
197
198
199
200
201
202 @Override
203 public boolean isSupportConnected() {
204 return true;
205 }
206 }