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.optim.nonlinear.vector.leastsquares;
23
24 import org.hipparchus.analysis.MultivariateMatrixFunction;
25 import org.hipparchus.analysis.MultivariateVectorFunction;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.hipparchus.exception.MathIllegalStateException;
28 import org.hipparchus.geometry.euclidean.twod.Vector2D;
29 import org.hipparchus.linear.Array2DRowRealMatrix;
30 import org.hipparchus.linear.ArrayRealVector;
31 import org.hipparchus.linear.BlockRealMatrix;
32 import org.hipparchus.linear.DiagonalMatrix;
33 import org.hipparchus.linear.RealMatrix;
34 import org.hipparchus.linear.RealVector;
35 import org.hipparchus.optim.ConvergenceChecker;
36 import org.hipparchus.optim.SimpleVectorValueChecker;
37 import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresOptimizer.Optimum;
38 import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem.Evaluation;
39 import org.hipparchus.util.FastMath;
40 import org.hipparchus.util.Pair;
41 import org.junit.jupiter.api.Assertions;
42 import org.junit.jupiter.api.Test;
43
44 import java.io.IOException;
45 import java.util.Arrays;
46
47 import static org.hamcrest.CoreMatchers.is;
48 import static org.hamcrest.CoreMatchers.not;
49 import static org.hamcrest.CoreMatchers.sameInstance;
50 import static org.hamcrest.MatcherAssert.assertThat;
51 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
52 import static org.junit.jupiter.api.Assertions.assertEquals;
53 import static org.junit.jupiter.api.Assertions.assertTrue;
54
55
56
57
58
59
60
61
62
63
64 public abstract class AbstractLeastSquaresOptimizerAbstractTest {
65
66
67 public static final double TOl = 1e-10;
68
69 public LeastSquaresBuilder base() {
70 return new LeastSquaresBuilder()
71 .checkerPair(new SimpleVectorValueChecker(1e-6, 1e-6))
72 .maxEvaluations(100)
73 .maxIterations(getMaxIterations());
74 }
75
76 public LeastSquaresBuilder builder(CircleVectorial c) {
77 final double[] weights = new double[c.getN()];
78 Arrays.fill(weights, 1.0);
79 return base()
80 .model(c.getModelFunction(), c.getModelFunctionJacobian())
81 .target(new double[c.getN()])
82 .weight(new DiagonalMatrix(weights));
83 }
84
85 public LeastSquaresBuilder builder(StatisticalReferenceDataset dataset) {
86 StatisticalReferenceDataset.LeastSquaresProblem problem
87 = dataset.getLeastSquaresProblem();
88 final double[] weights = new double[dataset.getNumObservations()];
89 Arrays.fill(weights, 1.0);
90 return base()
91 .model(problem.getModelFunction(), problem.getModelFunctionJacobian())
92 .target(dataset.getData()[1])
93 .weight(new DiagonalMatrix(weights))
94 .start(dataset.getStartingPoint(0));
95 }
96
97 public void customFail(LeastSquaresOptimizer optimizer) {
98 Assertions.fail("Expected Exception from: " + optimizer.toString());
99 }
100
101
102
103
104
105
106
107 public void customAssertEquals(double tolerance, RealVector actual, double... expected){
108 for (int i = 0; i < expected.length; i++) {
109 assertEquals(expected[i], actual.getEntry(i), tolerance);
110 }
111 assertEquals(expected.length, actual.getDimension());
112 }
113
114
115
116
117
118 public abstract int getMaxIterations();
119
120
121
122
123
124
125 public abstract LeastSquaresOptimizer getOptimizer();
126
127
128
129
130 public final LeastSquaresOptimizer optimizer = this.getOptimizer();
131
132 @Test
133 public void testGetIterations() {
134 LeastSquaresProblem lsp = base()
135 .target(new double[]{1})
136 .weight(new DiagonalMatrix(new double[]{1}))
137 .start(new double[]{3})
138 .model(new MultivariateJacobianFunction() {
139 public Pair<RealVector, RealMatrix> value(final RealVector point) {
140 return new Pair<RealVector, RealMatrix>(
141 new ArrayRealVector(
142 new double[]{
143 FastMath.pow(point.getEntry(0), 4)
144 },
145 false),
146 new Array2DRowRealMatrix(
147 new double[][]{
148 {0.25 * FastMath.pow(point.getEntry(0), 3)}
149 },
150 false)
151 );
152 }
153 })
154 .build();
155
156 Optimum optimum = optimizer.optimize(lsp);
157
158
159 assertTrue(optimum.getIterations() > 0);
160 }
161
162 @Test
163 public void testTrivial() {
164 LinearProblem problem
165 = new LinearProblem(new double[][]{{2}},
166 new double[]{3});
167 LeastSquaresProblem ls = problem.getBuilder().build();
168
169 Optimum optimum = optimizer.optimize(ls);
170
171 assertEquals(0, optimum.getRMS(), TOl);
172 customAssertEquals(TOl, optimum.getPoint(), 1.5);
173 assertEquals(0.0, optimum.getResiduals().getEntry(0), TOl);
174 }
175
176 @Test
177 public void testQRColumnsPermutation() {
178 LinearProblem problem
179 = new LinearProblem(new double[][]{{1, -1}, {0, 2}, {1, -2}},
180 new double[]{4, 6, 1});
181
182 Optimum optimum = optimizer.optimize(problem.getBuilder().build());
183
184 assertEquals(0, optimum.getRMS(), TOl);
185 customAssertEquals(TOl, optimum.getPoint(), 7, 3);
186 customAssertEquals(TOl, optimum.getResiduals(), 0, 0, 0);
187 }
188
189 @Test
190 public void testNoDependency() {
191 LinearProblem problem = new LinearProblem(new double[][]{
192 {2, 0, 0, 0, 0, 0},
193 {0, 2, 0, 0, 0, 0},
194 {0, 0, 2, 0, 0, 0},
195 {0, 0, 0, 2, 0, 0},
196 {0, 0, 0, 0, 2, 0},
197 {0, 0, 0, 0, 0, 2}
198 }, new double[]{0, 1.1, 2.2, 3.3, 4.4, 5.5});
199
200 Optimum optimum = optimizer.optimize(problem.getBuilder().build());
201
202 assertEquals(0, optimum.getRMS(), TOl);
203 for (int i = 0; i < problem.target.length; ++i) {
204 assertEquals(0.55 * i, optimum.getPoint().getEntry(i), TOl);
205 }
206 }
207
208 @Test
209 public void testOneSet() {
210 LinearProblem problem = new LinearProblem(new double[][]{
211 {1, 0, 0},
212 {-1, 1, 0},
213 {0, -1, 1}
214 }, new double[]{1, 1, 1});
215
216 Optimum optimum = optimizer.optimize(problem.getBuilder().build());
217
218 assertEquals(0, optimum.getRMS(), TOl);
219 customAssertEquals(TOl, optimum.getPoint(), 1, 2, 3);
220 }
221
222 @Test
223 public void testTwoSets() {
224 double epsilon = 1e-7;
225 LinearProblem problem = new LinearProblem(new double[][]{
226 {2, 1, 0, 4, 0, 0},
227 {-4, -2, 3, -7, 0, 0},
228 {4, 1, -2, 8, 0, 0},
229 {0, -3, -12, -1, 0, 0},
230 {0, 0, 0, 0, epsilon, 1},
231 {0, 0, 0, 0, 1, 1}
232 }, new double[]{2, -9, 2, 2, 1 + epsilon * epsilon, 2});
233
234 Optimum optimum = optimizer.optimize(problem.getBuilder().build());
235
236 assertEquals(0, optimum.getRMS(), TOl);
237 customAssertEquals(TOl, optimum.getPoint(), 3, 4, -1, -2, 1 + epsilon, 1 - epsilon);
238 }
239
240 @Test
241 public void testNonInvertible() throws Exception {
242 try {
243 LinearProblem problem = new LinearProblem(new double[][]{
244 {1, 2, -3},
245 {2, 1, 3},
246 {-3, 0, -9}
247 }, new double[]{1, 1, 1});
248
249 optimizer.optimize(problem.getBuilder().build());
250
251 customFail(optimizer);
252 } catch (MathIllegalArgumentException miae) {
253
254 } catch (MathIllegalStateException mise) {
255
256 }
257 }
258
259 @Test
260 public void testIllConditioned() {
261 LinearProblem problem1 = new LinearProblem(new double[][]{
262 {10, 7, 8, 7},
263 {7, 5, 6, 5},
264 {8, 6, 10, 9},
265 {7, 5, 9, 10}
266 }, new double[]{32, 23, 33, 31});
267 final double[] start = {0, 1, 2, 3};
268
269 Optimum optimum = optimizer
270 .optimize(problem1.getBuilder().start(start).build());
271
272 assertEquals(0, optimum.getRMS(), TOl);
273 customAssertEquals(TOl, optimum.getPoint(), 1, 1, 1, 1);
274
275 LinearProblem problem2 = new LinearProblem(new double[][]{
276 {10.00, 7.00, 8.10, 7.20},
277 {7.08, 5.04, 6.00, 5.00},
278 {8.00, 5.98, 9.89, 9.00},
279 {6.99, 4.99, 9.00, 9.98}
280 }, new double[]{32, 23, 33, 31});
281
282 optimum = optimizer.optimize(problem2.getBuilder().start(start).build());
283
284 assertEquals(0, optimum.getRMS(), TOl);
285 customAssertEquals(1e-8, optimum.getPoint(), -81, 137, -34, 22);
286 }
287
288 @Test
289 public void testMoreEstimatedParametersSimple() {
290 LinearProblem problem = new LinearProblem(new double[][]{
291 {3, 2, 0, 0},
292 {0, 1, -1, 1},
293 {2, 0, 1, 0}
294 }, new double[]{7, 3, 5});
295
296 Optimum optimum = optimizer
297 .optimize(problem.getBuilder().start(new double[]{7, 6, 5, 4}).build());
298
299 assertEquals(0, optimum.getRMS(), TOl);
300 }
301
302 @Test
303 public void testMoreEstimatedParametersUnsorted() {
304 LinearProblem problem = new LinearProblem(new double[][]{
305 {1, 1, 0, 0, 0, 0},
306 {0, 0, 1, 1, 1, 0},
307 {0, 0, 0, 0, 1, -1},
308 {0, 0, -1, 1, 0, 1},
309 {0, 0, 0, -1, 1, 0}
310 }, new double[]{3, 12, -1, 7, 1});
311
312 Optimum optimum = optimizer.optimize(
313 problem.getBuilder().start(new double[]{2, 2, 2, 2, 2, 2}).build());
314
315 assertEquals(0, optimum.getRMS(), TOl);
316 RealVector point = optimum.getPoint();
317
318
319 assertEquals(3, point.getEntry(0) + point.getEntry(1), TOl);
320
321 customAssertEquals(TOl, point.getSubVector(2, 4), 3, 4, 5, 6);
322 }
323
324 @Test
325 public void testRedundantEquations() {
326 LinearProblem problem = new LinearProblem(new double[][]{
327 {1, 1},
328 {1, -1},
329 {1, 3}
330 }, new double[]{3, 1, 5});
331
332 Optimum optimum = optimizer
333 .optimize(problem.getBuilder().start(new double[]{1, 1}).build());
334
335 assertEquals(0, optimum.getRMS(), TOl);
336 customAssertEquals(TOl, optimum.getPoint(), 2, 1);
337 }
338
339 @Test
340 public void testInconsistentEquations() {
341 LinearProblem problem = new LinearProblem(new double[][]{
342 {1, 1},
343 {1, -1},
344 {1, 3}
345 }, new double[]{3, 1, 4});
346
347 Optimum optimum = optimizer
348 .optimize(problem.getBuilder().start(new double[]{1, 1}).build());
349
350
351 assertTrue(optimum.getRMS() > 0.1);
352 }
353
354 @Test
355 public void testInconsistentSizes1() {
356 try {
357 LinearProblem problem
358 = new LinearProblem(new double[][]{{1, 0},
359 {0, 1}},
360 new double[]{-1, 1});
361
362
363 Optimum optimum = optimizer.optimize(problem.getBuilder().build());
364
365 assertEquals(0, optimum.getRMS(), TOl);
366 customAssertEquals(TOl, optimum.getPoint(), -1, 1);
367
368
369 optimizer.optimize(
370 problem.getBuilder().weight(new DiagonalMatrix(new double[]{1})).build());
371
372 customFail(optimizer);
373 } catch (MathIllegalArgumentException e) {
374
375 }
376 }
377
378 @Test
379 public void testInconsistentSizes2() {
380 try {
381 LinearProblem problem
382 = new LinearProblem(new double[][]{{1, 0}, {0, 1}},
383 new double[]{-1, 1});
384
385 Optimum optimum = optimizer.optimize(problem.getBuilder().build());
386
387 assertEquals(0, optimum.getRMS(), TOl);
388 customAssertEquals(TOl, optimum.getPoint(), -1, 1);
389
390
391 optimizer.optimize(
392 problem.getBuilder()
393 .target(new double[]{1})
394 .weight(new DiagonalMatrix(new double[]{1}))
395 .build()
396 );
397
398 customFail(optimizer);
399 } catch (MathIllegalArgumentException e) {
400
401 }
402 }
403
404 @Test
405 public void testCircleFitting() {
406 CircleVectorial circle = new CircleVectorial();
407 circle.addPoint(30, 68);
408 circle.addPoint(50, -6);
409 circle.addPoint(110, -20);
410 circle.addPoint(35, 15);
411 circle.addPoint(45, 97);
412 final double[] start = {98.680, 47.345};
413
414 Optimum optimum = optimizer.optimize(builder(circle).start(start).build());
415
416 assertTrue(optimum.getEvaluations() < 10);
417
418 double rms = optimum.getRMS();
419 assertEquals(1.768262623567235, FastMath.sqrt(circle.getN()) * rms, TOl);
420
421 Vector2D center = new Vector2D(optimum.getPoint().getEntry(0), optimum.getPoint().getEntry(1));
422 assertEquals(69.96016176931406, circle.getRadius(center), 1e-6);
423 assertEquals(96.07590211815305, center.getX(), 1e-6);
424 assertEquals(48.13516790438953, center.getY(), 1e-6);
425
426 double[][] cov = optimum.getCovariances(1e-14).getData();
427 assertEquals(1.839, cov[0][0], 0.001);
428 assertEquals(0.731, cov[0][1], 0.001);
429 assertEquals(cov[0][1], cov[1][0], 1e-14);
430 assertEquals(0.786, cov[1][1], 0.001);
431
432
433 double r = circle.getRadius(center);
434 for (double d = 0; d < 2 * FastMath.PI; d += 0.01) {
435 circle.addPoint(center.getX() + r * FastMath.cos(d), center.getY() + r * FastMath.sin(d));
436 }
437
438 double[] weights = new double[circle.getN()];
439 Arrays.fill(weights, 2);
440
441 optimum = optimizer.optimize(
442 builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());
443
444 cov = optimum.getCovariances(1e-14).getData();
445 assertEquals(0.0016, cov[0][0], 0.001);
446 assertEquals(3.2e-7, cov[0][1], 1e-9);
447 assertEquals(cov[0][1], cov[1][0], 1e-14);
448 assertEquals(0.0016, cov[1][1], 0.001);
449 }
450
451 @Test
452 public void testCircleFittingBadInit() {
453 CircleVectorial circle = new CircleVectorial();
454 double[][] points = circlePoints;
455 double[] weights = new double[points.length];
456 final double[] start = {-12, -12};
457 Arrays.fill(weights, 2);
458 for (int i = 0; i < points.length; ++i) {
459 circle.addPoint(points[i][0], points[i][1]);
460 }
461
462 Optimum optimum = optimizer.optimize(builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());
463
464 Vector2D center = new Vector2D(optimum.getPoint().getEntry(0), optimum.getPoint().getEntry(1));
465 assertTrue(optimum.getEvaluations() < 25);
466 assertEquals(0.043, optimum.getRMS(), 1e-3);
467 assertEquals(0.292235, circle.getRadius(center), 1e-6);
468 assertEquals(-0.151738, center.getX(), 1e-6);
469 assertEquals(0.2075001, center.getY(), 1e-6);
470 }
471
472 @Test
473 public void testCircleFittingGoodInit() {
474 CircleVectorial circle = new CircleVectorial();
475 double[][] points = circlePoints;
476 double[] weights = new double[points.length];
477 Arrays.fill(weights, 2);
478 for (int i = 0; i < points.length; ++i) {
479 circle.addPoint(points[i][0], points[i][1]);
480 }
481 final double[] start = {0, 0};
482
483 Optimum optimum = optimizer.optimize(
484 builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());
485
486 customAssertEquals(1e-6, optimum.getPoint(), -0.1517383071957963, 0.2074999736353867);
487 assertEquals(0.04268731682389561, optimum.getRMS(), 1e-8);
488 }
489
490 private final double[][] circlePoints = new double[][]{
491 {-0.312967, 0.072366}, {-0.339248, 0.132965}, {-0.379780, 0.202724},
492 {-0.390426, 0.260487}, {-0.361212, 0.328325}, {-0.346039, 0.392619},
493 {-0.280579, 0.444306}, {-0.216035, 0.470009}, {-0.149127, 0.493832},
494 {-0.075133, 0.483271}, {-0.007759, 0.452680}, {0.060071, 0.410235},
495 {0.103037, 0.341076}, {0.118438, 0.273884}, {0.131293, 0.192201},
496 {0.115869, 0.129797}, {0.072223, 0.058396}, {0.022884, 0.000718},
497 {-0.053355, -0.020405}, {-0.123584, -0.032451}, {-0.216248, -0.032862},
498 {-0.278592, -0.005008}, {-0.337655, 0.056658}, {-0.385899, 0.112526},
499 {-0.405517, 0.186957}, {-0.415374, 0.262071}, {-0.387482, 0.343398},
500 {-0.347322, 0.397943}, {-0.287623, 0.458425}, {-0.223502, 0.475513},
501 {-0.135352, 0.478186}, {-0.061221, 0.483371}, {0.003711, 0.422737},
502 {0.065054, 0.375830}, {0.108108, 0.297099}, {0.123882, 0.222850},
503 {0.117729, 0.134382}, {0.085195, 0.056820}, {0.029800, -0.019138},
504 {-0.027520, -0.072374}, {-0.102268, -0.091555}, {-0.200299, -0.106578},
505 {-0.292731, -0.091473}, {-0.356288, -0.051108}, {-0.420561, 0.014926},
506 {-0.471036, 0.074716}, {-0.488638, 0.182508}, {-0.485990, 0.254068},
507 {-0.463943, 0.338438}, {-0.406453, 0.404704}, {-0.334287, 0.466119},
508 {-0.254244, 0.503188}, {-0.161548, 0.495769}, {-0.075733, 0.495560},
509 {0.001375, 0.434937}, {0.082787, 0.385806}, {0.115490, 0.323807},
510 {0.141089, 0.223450}, {0.138693, 0.131703}, {0.126415, 0.049174},
511 {0.066518, -0.010217}, {-0.005184, -0.070647}, {-0.080985, -0.103635},
512 {-0.177377, -0.116887}, {-0.260628, -0.100258}, {-0.335756, -0.056251},
513 {-0.405195, -0.000895}, {-0.444937, 0.085456}, {-0.484357, 0.175597},
514 {-0.472453, 0.248681}, {-0.438580, 0.347463}, {-0.402304, 0.422428},
515 {-0.326777, 0.479438}, {-0.247797, 0.505581}, {-0.152676, 0.519380},
516 {-0.071754, 0.516264}, {0.015942, 0.472802}, {0.076608, 0.419077},
517 {0.127673, 0.330264}, {0.159951, 0.262150}, {0.153530, 0.172681},
518 {0.140653, 0.089229}, {0.078666, 0.024981}, {0.023807, -0.037022},
519 {-0.048837, -0.077056}, {-0.127729, -0.075338}, {-0.221271, -0.067526}
520 };
521
522 public void doTestStRD(final StatisticalReferenceDataset dataset,
523 final LeastSquaresOptimizer optimizer,
524 final double errParams,
525 final double errParamsSd) {
526
527 final Optimum optimum = optimizer.optimize(builder(dataset).build());
528
529 final RealVector actual = optimum.getPoint();
530 for (int i = 0; i < actual.getDimension(); i++) {
531 double expected = dataset.getParameter(i);
532 double delta = FastMath.abs(errParams * expected);
533 assertEquals(expected, actual.getEntry(i), delta, dataset.getName() + ", param #" + i);
534 }
535 }
536
537 @Test
538 public void testKirby2() throws IOException {
539 doTestStRD(StatisticalReferenceDatasetFactory.createKirby2(), optimizer, 1E-7, 1E-7);
540 }
541
542 @Test
543 public void testHahn1() throws IOException {
544 doTestStRD(StatisticalReferenceDatasetFactory.createHahn1(), optimizer, 1E-7, 1E-4);
545 }
546
547 @Test
548 public void testPointCopy() {
549 LinearProblem problem = new LinearProblem(new double[][]{
550 {1, 0, 0},
551 {-1, 1, 0},
552 {0, -1, 1}
553 }, new double[]{1, 1, 1});
554
555 final boolean[] checked = {false};
556
557 final LeastSquaresBuilder builder = problem.getBuilder()
558 .checker(new ConvergenceChecker<Evaluation>() {
559 public boolean converged(int iteration, Evaluation previous, Evaluation current) {
560 assertThat(
561 previous.getPoint(),
562 not(sameInstance(current.getPoint())));
563 assertArrayEquals(new double[3], previous.getPoint().toArray(), 0);
564 assertArrayEquals(new double[] {1, 2, 3}, current.getPoint().toArray(), TOl);
565 checked[0] = true;
566 return true;
567 }
568 });
569 optimizer.optimize(builder.build());
570
571 assertThat(checked[0], is(true));
572 }
573
574 class LinearProblem {
575 private final RealMatrix factors;
576 private final double[] target;
577
578 public LinearProblem(double[][] factors, double[] target) {
579 this.factors = new BlockRealMatrix(factors);
580 this.target = target;
581 }
582
583 public double[] getTarget() {
584 return target;
585 }
586
587 public MultivariateVectorFunction getModelFunction() {
588 return new MultivariateVectorFunction() {
589 public double[] value(double[] params) {
590 return factors.operate(params);
591 }
592 };
593 }
594
595 public MultivariateMatrixFunction getModelFunctionJacobian() {
596 return new MultivariateMatrixFunction() {
597 public double[][] value(double[] params) {
598 return factors.getData();
599 }
600 };
601 }
602
603 public LeastSquaresBuilder getBuilder() {
604 final double[] weights = new double[target.length];
605 Arrays.fill(weights, 1.0);
606 return base()
607 .model(getModelFunction(), getModelFunctionJacobian())
608 .target(target)
609 .weight(new DiagonalMatrix(weights))
610 .start(new double[factors.getColumnDimension()]);
611 }
612 }
613 }