1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.analysis.function;
19
20 import java.util.Arrays;
21
22 import org.apache.commons.math4.legacy.analysis.ParametricUnivariateFunction;
23 import org.apache.commons.math4.legacy.analysis.differentiation.DerivativeStructure;
24 import org.apache.commons.math4.legacy.analysis.differentiation.UnivariateDifferentiableFunction;
25 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
26 import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
27 import org.apache.commons.math4.legacy.exception.NullArgumentException;
28 import org.apache.commons.math4.core.jdkmath.JdkMath;
29 import org.apache.commons.numbers.core.Precision;
30
31
32
33
34
35
36
37 public class Gaussian implements UnivariateDifferentiableFunction {
38
39 private final double mean;
40
41 private final double is;
42
43 private final double i2s2;
44
45 private final double norm;
46
47
48
49
50
51
52
53
54
55 public Gaussian(double norm,
56 double mean,
57 double sigma)
58 throws NotStrictlyPositiveException {
59 if (sigma <= 0) {
60 throw new NotStrictlyPositiveException(sigma);
61 }
62
63 this.norm = norm;
64 this.mean = mean;
65 this.is = 1 / sigma;
66 this.i2s2 = 0.5 * is * is;
67 }
68
69
70
71
72
73
74
75
76 public Gaussian(double mean,
77 double sigma)
78 throws NotStrictlyPositiveException {
79 this(1 / (sigma * JdkMath.sqrt(2 * Math.PI)), mean, sigma);
80 }
81
82
83
84
85 public Gaussian() {
86 this(0, 1);
87 }
88
89
90 @Override
91 public double value(double x) {
92 return value(x - mean, norm, i2s2);
93 }
94
95
96
97
98
99
100
101
102
103
104 public static class Parametric implements ParametricUnivariateFunction {
105
106
107
108
109
110
111
112
113
114
115
116 @Override
117 public double value(double x, double ... param)
118 throws NullArgumentException,
119 DimensionMismatchException,
120 NotStrictlyPositiveException {
121 validateParameters(param);
122
123 final double diff = x - param[1];
124 final double i2s2 = 1 / (2 * param[2] * param[2]);
125 return Gaussian.value(diff, param[0], i2s2);
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 @Override
143 public double[] gradient(double x, double ... param)
144 throws NullArgumentException,
145 DimensionMismatchException,
146 NotStrictlyPositiveException {
147 validateParameters(param);
148
149 final double norm = param[0];
150 final double diff = x - param[1];
151 final double sigma = param[2];
152 final double i2s2 = 1 / (2 * sigma * sigma);
153
154 final double n = Gaussian.value(diff, 1, i2s2);
155 final double m = norm * n * 2 * i2s2 * diff;
156 final double s = m * diff / sigma;
157
158 return new double[] { n, m, s };
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172 private void validateParameters(double[] param)
173 throws NullArgumentException,
174 DimensionMismatchException,
175 NotStrictlyPositiveException {
176 if (param == null) {
177 throw new NullArgumentException();
178 }
179 if (param.length != 3) {
180 throw new DimensionMismatchException(param.length, 3);
181 }
182 if (param[2] <= 0) {
183 throw new NotStrictlyPositiveException(param[2]);
184 }
185 }
186 }
187
188
189
190
191
192
193
194 private static double value(double xMinusMean,
195 double norm,
196 double i2s2) {
197 return norm * JdkMath.exp(-xMinusMean * xMinusMean * i2s2);
198 }
199
200
201
202
203 @Override
204 public DerivativeStructure value(final DerivativeStructure t)
205 throws DimensionMismatchException {
206
207 final double u = is * (t.getValue() - mean);
208 double[] f = new double[t.getOrder() + 1];
209
210
211
212
213
214
215
216
217 final double[] p = new double[f.length];
218 p[0] = 1;
219 final double u2 = u * u;
220 double coeff = norm * JdkMath.exp(-0.5 * u2);
221 if (coeff <= Precision.SAFE_MIN) {
222 Arrays.fill(f, 0.0);
223 } else {
224 f[0] = coeff;
225 for (int n = 1; n < f.length; ++n) {
226
227
228 double v = 0;
229 p[n] = -p[n - 1];
230 for (int k = n; k >= 0; k -= 2) {
231 v = v * u2 + p[k];
232 if (k > 2) {
233 p[k - 2] = (k - 1) * p[k - 1] - p[k - 3];
234 } else if (k == 2) {
235 p[0] = p[1];
236 }
237 }
238 if ((n & 0x1) == 1) {
239 v *= u;
240 }
241
242 coeff *= is;
243 f[n] = coeff * v;
244 }
245 }
246
247 return t.compose(f);
248 }
249 }