1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.fitting;
23
24 import org.hipparchus.exception.MathIllegalArgumentException;
25 import org.hipparchus.exception.MathIllegalStateException;
26 import org.junit.Assert;
27 import org.junit.Test;
28
29
30
31
32
33 public class GaussianCurveFitterTest {
34
35 protected static final double[][] DATASET1 = new double[][] {
36 {4.0254623, 531026.0},
37 {4.02804905, 664002.0},
38 {4.02934242, 787079.0},
39 {4.03128248, 984167.0},
40 {4.03386923, 1294546.0},
41 {4.03580929, 1560230.0},
42 {4.03839603, 1887233.0},
43 {4.0396894, 2113240.0},
44 {4.04162946, 2375211.0},
45 {4.04421621, 2687152.0},
46 {4.04550958, 2862644.0},
47 {4.04744964, 3078898.0},
48 {4.05003639, 3327238.0},
49 {4.05132976, 3461228.0},
50 {4.05326982, 3580526.0},
51 {4.05585657, 3576946.0},
52 {4.05779662, 3439750.0},
53 {4.06038337, 3220296.0},
54 {4.06167674, 3070073.0},
55 {4.0636168, 2877648.0},
56 {4.06620355, 2595848.0},
57 {4.06749692, 2390157.0},
58 {4.06943698, 2175960.0},
59 {4.07202373, 1895104.0},
60 {4.0733171, 1687576.0},
61 {4.07525716, 1447024.0},
62 {4.0778439, 1130879.0},
63 {4.07978396, 904900.0},
64 {4.08237071, 717104.0},
65 {4.08366408, 620014.0}
66 };
67
68 protected static final double[][] DATASET2 = new double[][] {
69 {-20.15, 1523.0},
70 {-19.65, 1566.0},
71 {-19.15, 1592.0},
72 {-18.65, 1927.0},
73 {-18.15, 3089.0},
74 {-17.65, 6068.0},
75 {-17.15, 14239.0},
76 {-16.65, 34124.0},
77 {-16.15, 64097.0},
78 {-15.65, 110352.0},
79 {-15.15, 164742.0},
80 {-14.65, 209499.0},
81 {-14.15, 267274.0},
82 {-13.65, 283290.0},
83 {-13.15, 275363.0},
84 {-12.65, 258014.0},
85 {-12.15, 225000.0},
86 {-11.65, 200000.0},
87 {-11.15, 190000.0},
88 {-10.65, 185000.0},
89 {-10.15, 180000.0},
90 { -9.65, 179000.0},
91 { -9.15, 178000.0},
92 { -8.65, 177000.0},
93 { -8.15, 176000.0},
94 { -7.65, 175000.0},
95 { -7.15, 174000.0},
96 { -6.65, 173000.0},
97 { -6.15, 172000.0},
98 { -5.65, 171000.0},
99 { -5.15, 170000.0}
100 };
101
102 protected static final double[][] DATASET3 = new double[][] {
103 {-90.15, 1513.0},
104 {-80.15, 1514.0},
105 {-70.15, 1513.0},
106 {-60.15, 1514.0},
107 {-50.15, 1513.0},
108 {-40.15, 1514.0},
109 {-30.15, 1513.0},
110 {-20.15, 1523.0},
111 {-19.65, 1566.0},
112 {-19.15, 1592.0},
113 {-18.65, 1927.0},
114 {-18.15, 3089.0},
115 {-17.65, 6068.0},
116 {-17.15, 14239.0},
117 {-16.65, 34124.0},
118 {-16.15, 64097.0},
119 {-15.65, 110352.0},
120 {-15.15, 164742.0},
121 {-14.65, 209499.0},
122 {-14.15, 267274.0},
123 {-13.65, 283290.0},
124 {-13.15, 275363.0},
125 {-12.65, 258014.0},
126 {-12.15, 214073.0},
127 {-11.65, 182244.0},
128 {-11.15, 136419.0},
129 {-10.65, 97823.0},
130 {-10.15, 58930.0},
131 { -9.65, 35404.0},
132 { -9.15, 16120.0},
133 { -8.65, 9823.0},
134 { -8.15, 5064.0},
135 { -7.65, 2575.0},
136 { -7.15, 1642.0},
137 { -6.65, 1101.0},
138 { -6.15, 812.0},
139 { -5.65, 690.0},
140 { -5.15, 565.0},
141 { 5.15, 564.0},
142 { 15.15, 565.0},
143 { 25.15, 564.0},
144 { 35.15, 565.0},
145 { 45.15, 564.0},
146 { 55.15, 565.0},
147 { 65.15, 564.0},
148 { 75.15, 565.0}
149 };
150
151 protected static final double[][] DATASET4 = new double[][] {
152 {-20.15, 1523.0},
153 {-19.65, 1566.0},
154 {-19.15, 1592.0},
155 {-18.65, 1927.0},
156 {-18.15, 3089.0},
157 {-17.65, 6068.0},
158 {-17.15, 14239.0},
159 {-16.65, 34124.0},
160 {-16.15, 64097.0},
161 {-15.65, 110352.0},
162 {-15.15, 164742.0},
163 {-14.65, 209499.0},
164 {-14.15, 267274.0},
165 {-13.65, 283290.0}
166 };
167
168 protected static final double[][] DATASET5 = new double[][] {
169 {4.0254623, 531026.0},
170 {4.03128248, 984167.0},
171 {4.03839603, 1887233.0},
172 {4.04421621, 2687152.0},
173 {4.05132976, 3461228.0},
174 {4.05326982, 3580526.0},
175 {4.05779662, 3439750.0},
176 {4.0636168, 2877648.0},
177 {4.06943698, 2175960.0},
178 {4.07525716, 1447024.0},
179 {4.08237071, 717104.0},
180 {4.08366408, 620014.0}
181 };
182
183
184
185
186 @Test
187 public void testFit01() {
188 GaussianCurveFitter fitter = GaussianCurveFitter.create();
189 double[] parameters = fitter.fit(createDataset(DATASET1).toList());
190
191 Assert.assertEquals(3496978.1837704973, parameters[0], 1e-4);
192 Assert.assertEquals(4.054933085999146, parameters[1], 1e-4);
193 Assert.assertEquals(0.015039355620304326, parameters[2], 1e-4);
194 }
195
196 @Test
197 public void testWithMaxIterations1() {
198 final int maxIter = 20;
199 final double[] init = { 3.5e6, 4.2, 0.1 };
200
201 GaussianCurveFitter fitter = GaussianCurveFitter.create();
202 double[] parameters = fitter
203 .withMaxIterations(maxIter)
204 .withStartPoint(init)
205 .fit(createDataset(DATASET1).toList());
206
207 Assert.assertEquals(3496978.1837704973, parameters[0], 1e-2);
208 Assert.assertEquals(4.054933085999146, parameters[1], 1e-4);
209 Assert.assertEquals(0.015039355620304326, parameters[2], 1e-4);
210 }
211
212 @Test(expected=MathIllegalStateException.class)
213 public void testWithMaxIterations2() {
214 final int maxIter = 1;
215 final double[] init = { 3.5e6, 4.2, 0.1 };
216
217 GaussianCurveFitter fitter = GaussianCurveFitter.create();
218 fitter.withMaxIterations(maxIter)
219 .withStartPoint(init)
220 .fit(createDataset(DATASET1).toList());
221 }
222
223 @Test
224 public void testWithStartPoint() {
225 final double[] init = { 3.5e6, 4.2, 0.1 };
226
227 GaussianCurveFitter fitter = GaussianCurveFitter.create();
228 double[] parameters = fitter
229 .withStartPoint(init)
230 .fit(createDataset(DATASET1).toList());
231
232 Assert.assertEquals(3496978.1837704973, parameters[0], 1e-2);
233 Assert.assertEquals(4.054933085999146, parameters[1], 1e-4);
234 Assert.assertEquals(0.015039355620304326, parameters[2], 1e-4);
235 }
236
237
238
239
240 @Test(expected=MathIllegalArgumentException.class)
241 public void testFit02() {
242 GaussianCurveFitter.create().fit(new WeightedObservedPoints().toList());
243 }
244
245
246
247
248 @Test(expected=MathIllegalArgumentException.class)
249 public void testFit03() {
250 GaussianCurveFitter fitter = GaussianCurveFitter.create();
251 fitter.fit(createDataset(new double[][] {
252 {4.0254623, 531026.0},
253 {4.02804905, 664002.0}
254 }).toList());
255 }
256
257
258
259
260 @Test
261 public void testFit04() {
262 GaussianCurveFitter fitter = GaussianCurveFitter.create();
263 double[] parameters = fitter.fit(createDataset(DATASET2).toList());
264
265 Assert.assertEquals(233003.2967252038, parameters[0], 1e-4);
266 Assert.assertEquals(-10.654887521095983, parameters[1], 1e-4);
267 Assert.assertEquals(4.335937353196641, parameters[2], 1e-4);
268 }
269
270
271
272
273 @Test
274 public void testFit05() {
275 GaussianCurveFitter fitter = GaussianCurveFitter.create();
276 double[] parameters = fitter.fit(createDataset(DATASET3).toList());
277
278 Assert.assertEquals(283863.81929180305, parameters[0], 1e-4);
279 Assert.assertEquals(-13.29641995105174, parameters[1], 1e-4);
280 Assert.assertEquals(1.7297330293549908, parameters[2], 1e-4);
281 }
282
283
284
285
286 @Test
287 public void testFit06() {
288 GaussianCurveFitter fitter = GaussianCurveFitter.create();
289 double[] parameters = fitter.fit(createDataset(DATASET4).toList());
290
291 Assert.assertEquals(285250.66754309234, parameters[0], 1e-4);
292 Assert.assertEquals(-13.528375695228455, parameters[1], 1e-4);
293 Assert.assertEquals(1.5204344894331614, parameters[2], 1e-4);
294 }
295
296
297
298
299 @Test
300 public void testFit07() {
301 GaussianCurveFitter fitter = GaussianCurveFitter.create();
302 double[] parameters = fitter.fit(createDataset(DATASET5).toList());
303
304 Assert.assertEquals(3514384.729342235, parameters[0], 1e-4);
305 Assert.assertEquals(4.054970307455625, parameters[1], 1e-4);
306 Assert.assertEquals(0.015029412832160017, parameters[2], 1e-4);
307 }
308
309 @Test
310 public void testMath519() {
311
312
313
314 final double[] data = {
315 1.1143831578403364E-29,
316 4.95281403484594E-28,
317 1.1171347211930288E-26,
318 1.7044813962636277E-25,
319 1.9784716574832164E-24,
320 1.8630236407866774E-23,
321 1.4820532905097742E-22,
322 1.0241963854632831E-21,
323 6.275077366673128E-21,
324 3.461808994532493E-20,
325 1.7407124684715706E-19,
326 8.056687953553974E-19,
327 3.460193945992071E-18,
328 1.3883326374011525E-17,
329 5.233894983671116E-17,
330 1.8630791465263745E-16,
331 6.288759227922111E-16,
332 2.0204433920597856E-15,
333 6.198768938576155E-15,
334 1.821419346860626E-14,
335 5.139176445538471E-14,
336 1.3956427429045787E-13,
337 3.655705706448139E-13,
338 9.253753324779779E-13,
339 2.267636001476696E-12,
340 5.3880460095836855E-12,
341 1.2431632654852931E-11
342 };
343
344 final WeightedObservedPoints obs = new WeightedObservedPoints();
345 for (int i = 0; i < data.length; i++) {
346 obs.add(i, data[i]);
347 }
348 final double[] p = GaussianCurveFitter.create().fit(obs.toList());
349
350 Assert.assertEquals(53.1572792, p[1], 1e-7);
351 Assert.assertEquals(5.75214622, p[2], 1e-8);
352 }
353
354 @Test
355 public void testMath798() {
356
357
358
359
360
361 final WeightedObservedPoints obs = new WeightedObservedPoints();
362
363 obs.add(0.23, 395.0);
364
365 obs.add(1.14, 376.0);
366
367 obs.add(2.05, 163.0);
368
369 obs.add(2.95, 49.0);
370
371 obs.add(3.86, 16.0);
372
373 obs.add(4.77, 1.0);
374
375 final double[] p = GaussianCurveFitter.create().fit(obs.toList());
376
377
378 Assert.assertEquals(420.8397296167364, p[0], 1e-12);
379 Assert.assertEquals(0.603770729862231, p[1], 1e-15);
380 Assert.assertEquals(1.0786447936766612, p[2], 1e-14);
381 }
382
383
384
385
386
387
388
389
390
391
392
393 private static WeightedObservedPoints createDataset(double[][] points) {
394 final WeightedObservedPoints obs = new WeightedObservedPoints();
395 for (int i = 0; i < points.length; i++) {
396 obs.add(points[i][0], points[i][1]);
397 }
398 return obs;
399 }
400 }