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