1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.estimation;
18
19 import java.io.Serializable;
20 import java.util.Arrays;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 @Deprecated
102 public class LevenbergMarquardtEstimator extends AbstractEstimator implements Serializable {
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public LevenbergMarquardtEstimator() {
117
118
119 setMaxCostEval(1000);
120
121
122 setInitialStepBoundFactor(100.0);
123 setCostRelativeTolerance(1.0e-10);
124 setParRelativeTolerance(1.0e-10);
125 setOrthoTolerance(1.0e-10);
126
127 }
128
129
130
131
132
133
134
135
136
137
138 public void setInitialStepBoundFactor(double initialStepBoundFactor) {
139 this.initialStepBoundFactor = initialStepBoundFactor;
140 }
141
142
143
144
145
146
147
148 public void setCostRelativeTolerance(double costRelativeTolerance) {
149 this.costRelativeTolerance = costRelativeTolerance;
150 }
151
152
153
154
155
156
157
158
159 public void setParRelativeTolerance(double parRelativeTolerance) {
160 this.parRelativeTolerance = parRelativeTolerance;
161 }
162
163
164
165
166
167
168
169
170 public void setOrthoTolerance(double orthoTolerance) {
171 this.orthoTolerance = orthoTolerance;
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 @Override
203 public void estimate(EstimationProblem problem)
204 throws EstimationException {
205
206 initializeEstimate(problem);
207
208
209 solvedCols = Math.min(rows, cols);
210 diagR = new double[cols];
211 jacNorm = new double[cols];
212 beta = new double[cols];
213 permutation = new int[cols];
214 lmDir = new double[cols];
215
216
217 double delta = 0, xNorm = 0;
218 double[] diag = new double[cols];
219 double[] oldX = new double[cols];
220 double[] oldRes = new double[rows];
221 double[] work1 = new double[cols];
222 double[] work2 = new double[cols];
223 double[] work3 = new double[cols];
224
225
226 updateResidualsAndCost();
227
228
229 lmPar = 0;
230 boolean firstIteration = true;
231 while (true) {
232
233
234 updateJacobian();
235 qrDecomposition();
236
237
238 qTy(residuals);
239
240
241
242 for (int k = 0; k < solvedCols; ++k) {
243 int pk = permutation[k];
244 jacobian[k * cols + pk] = diagR[pk];
245 }
246
247 if (firstIteration) {
248
249
250
251 xNorm = 0;
252 for (int k = 0; k < cols; ++k) {
253 double dk = jacNorm[k];
254 if (dk == 0) {
255 dk = 1.0;
256 }
257 double xk = dk * parameters[k].getEstimate();
258 xNorm += xk * xk;
259 diag[k] = dk;
260 }
261 xNorm = Math.sqrt(xNorm);
262
263
264 delta = (xNorm == 0) ? initialStepBoundFactor : (initialStepBoundFactor * xNorm);
265
266 }
267
268
269 double maxCosine = 0;
270 if (cost != 0) {
271 for (int j = 0; j < solvedCols; ++j) {
272 int pj = permutation[j];
273 double s = jacNorm[pj];
274 if (s != 0) {
275 double sum = 0;
276 for (int i = 0, index = pj; i <= j; ++i, index += cols) {
277 sum += jacobian[index] * residuals[i];
278 }
279 maxCosine = Math.max(maxCosine, Math.abs(sum) / (s * cost));
280 }
281 }
282 }
283 if (maxCosine <= orthoTolerance) {
284 return;
285 }
286
287
288 for (int j = 0; j < cols; ++j) {
289 diag[j] = Math.max(diag[j], jacNorm[j]);
290 }
291
292
293 for (double ratio = 0; ratio < 1.0e-4;) {
294
295
296 for (int j = 0; j < solvedCols; ++j) {
297 int pj = permutation[j];
298 oldX[pj] = parameters[pj].getEstimate();
299 }
300 double previousCost = cost;
301 double[] tmpVec = residuals;
302 residuals = oldRes;
303 oldRes = tmpVec;
304
305
306 determineLMParameter(oldRes, delta, diag, work1, work2, work3);
307
308
309 double lmNorm = 0;
310 for (int j = 0; j < solvedCols; ++j) {
311 int pj = permutation[j];
312 lmDir[pj] = -lmDir[pj];
313 parameters[pj].setEstimate(oldX[pj] + lmDir[pj]);
314 double s = diag[pj] * lmDir[pj];
315 lmNorm += s * s;
316 }
317 lmNorm = Math.sqrt(lmNorm);
318
319
320 if (firstIteration) {
321 delta = Math.min(delta, lmNorm);
322 }
323
324
325 updateResidualsAndCost();
326
327
328 double actRed = -1.0;
329 if (0.1 * cost < previousCost) {
330 double r = cost / previousCost;
331 actRed = 1.0 - r * r;
332 }
333
334
335
336 for (int j = 0; j < solvedCols; ++j) {
337 int pj = permutation[j];
338 double dirJ = lmDir[pj];
339 work1[j] = 0;
340 for (int i = 0, index = pj; i <= j; ++i, index += cols) {
341 work1[i] += jacobian[index] * dirJ;
342 }
343 }
344 double coeff1 = 0;
345 for (int j = 0; j < solvedCols; ++j) {
346 coeff1 += work1[j] * work1[j];
347 }
348 double pc2 = previousCost * previousCost;
349 coeff1 = coeff1 / pc2;
350 double coeff2 = lmPar * lmNorm * lmNorm / pc2;
351 double preRed = coeff1 + 2 * coeff2;
352 double dirDer = -(coeff1 + coeff2);
353
354
355 ratio = (preRed == 0) ? 0 : (actRed / preRed);
356
357
358 if (ratio <= 0.25) {
359 double tmp =
360 (actRed < 0) ? (0.5 * dirDer / (dirDer + 0.5 * actRed)) : 0.5;
361 if ((0.1 * cost >= previousCost) || (tmp < 0.1)) {
362 tmp = 0.1;
363 }
364 delta = tmp * Math.min(delta, 10.0 * lmNorm);
365 lmPar /= tmp;
366 } else if ((lmPar == 0) || (ratio >= 0.75)) {
367 delta = 2 * lmNorm;
368 lmPar *= 0.5;
369 }
370
371
372 if (ratio >= 1.0e-4) {
373
374 firstIteration = false;
375 xNorm = 0;
376 for (int k = 0; k < cols; ++k) {
377 double xK = diag[k] * parameters[k].getEstimate();
378 xNorm += xK * xK;
379 }
380 xNorm = Math.sqrt(xNorm);
381 } else {
382
383 cost = previousCost;
384 for (int j = 0; j < solvedCols; ++j) {
385 int pj = permutation[j];
386 parameters[pj].setEstimate(oldX[pj]);
387 }
388 tmpVec = residuals;
389 residuals = oldRes;
390 oldRes = tmpVec;
391 }
392
393
394 if (((Math.abs(actRed) <= costRelativeTolerance) &&
395 (preRed <= costRelativeTolerance) &&
396 (ratio <= 2.0)) ||
397 (delta <= parRelativeTolerance * xNorm)) {
398 return;
399 }
400
401
402
403 if ((Math.abs(actRed) <= 2.2204e-16) && (preRed <= 2.2204e-16) && (ratio <= 2.0)) {
404 throw new EstimationException("cost relative tolerance is too small ({0})," +
405 " no further reduction in the" +
406 " sum of squares is possible",
407 costRelativeTolerance);
408 } else if (delta <= 2.2204e-16 * xNorm) {
409 throw new EstimationException("parameters relative tolerance is too small" +
410 " ({0}), no further improvement in" +
411 " the approximate solution is possible",
412 parRelativeTolerance);
413 } else if (maxCosine <= 2.2204e-16) {
414 throw new EstimationException("orthogonality tolerance is too small ({0})," +
415 " solution is orthogonal to the jacobian",
416 orthoTolerance);
417 }
418
419 }
420
421 }
422
423 }
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447 private void determineLMParameter(double[] qy, double delta, double[] diag,
448 double[] work1, double[] work2, double[] work3) {
449
450
451
452 for (int j = 0; j < rank; ++j) {
453 lmDir[permutation[j]] = qy[j];
454 }
455 for (int j = rank; j < cols; ++j) {
456 lmDir[permutation[j]] = 0;
457 }
458 for (int k = rank - 1; k >= 0; --k) {
459 int pk = permutation[k];
460 double ypk = lmDir[pk] / diagR[pk];
461 for (int i = 0, index = pk; i < k; ++i, index += cols) {
462 lmDir[permutation[i]] -= ypk * jacobian[index];
463 }
464 lmDir[pk] = ypk;
465 }
466
467
468
469 double dxNorm = 0;
470 for (int j = 0; j < solvedCols; ++j) {
471 int pj = permutation[j];
472 double s = diag[pj] * lmDir[pj];
473 work1[pj] = s;
474 dxNorm += s * s;
475 }
476 dxNorm = Math.sqrt(dxNorm);
477 double fp = dxNorm - delta;
478 if (fp <= 0.1 * delta) {
479 lmPar = 0;
480 return;
481 }
482
483
484
485
486 double sum2, parl = 0;
487 if (rank == solvedCols) {
488 for (int j = 0; j < solvedCols; ++j) {
489 int pj = permutation[j];
490 work1[pj] *= diag[pj] / dxNorm;
491 }
492 sum2 = 0;
493 for (int j = 0; j < solvedCols; ++j) {
494 int pj = permutation[j];
495 double sum = 0;
496 for (int i = 0, index = pj; i < j; ++i, index += cols) {
497 sum += jacobian[index] * work1[permutation[i]];
498 }
499 double s = (work1[pj] - sum) / diagR[pj];
500 work1[pj] = s;
501 sum2 += s * s;
502 }
503 parl = fp / (delta * sum2);
504 }
505
506
507 sum2 = 0;
508 for (int j = 0; j < solvedCols; ++j) {
509 int pj = permutation[j];
510 double sum = 0;
511 for (int i = 0, index = pj; i <= j; ++i, index += cols) {
512 sum += jacobian[index] * qy[i];
513 }
514 sum /= diag[pj];
515 sum2 += sum * sum;
516 }
517 double gNorm = Math.sqrt(sum2);
518 double paru = gNorm / delta;
519 if (paru == 0) {
520
521 paru = 2.2251e-308 / Math.min(delta, 0.1);
522 }
523
524
525
526 lmPar = Math.min(paru, Math.max(lmPar, parl));
527 if (lmPar == 0) {
528 lmPar = gNorm / dxNorm;
529 }
530
531 for (int countdown = 10; countdown >= 0; --countdown) {
532
533
534 if (lmPar == 0) {
535 lmPar = Math.max(2.2251e-308, 0.001 * paru);
536 }
537 double sPar = Math.sqrt(lmPar);
538 for (int j = 0; j < solvedCols; ++j) {
539 int pj = permutation[j];
540 work1[pj] = sPar * diag[pj];
541 }
542 determineLMDirection(qy, work1, work2, work3);
543
544 dxNorm = 0;
545 for (int j = 0; j < solvedCols; ++j) {
546 int pj = permutation[j];
547 double s = diag[pj] * lmDir[pj];
548 work3[pj] = s;
549 dxNorm += s * s;
550 }
551 dxNorm = Math.sqrt(dxNorm);
552 double previousFP = fp;
553 fp = dxNorm - delta;
554
555
556
557 if ((Math.abs(fp) <= 0.1 * delta) ||
558 ((parl == 0) && (fp <= previousFP) && (previousFP < 0))) {
559 return;
560 }
561
562
563 for (int j = 0; j < solvedCols; ++j) {
564 int pj = permutation[j];
565 work1[pj] = work3[pj] * diag[pj] / dxNorm;
566 }
567 for (int j = 0; j < solvedCols; ++j) {
568 int pj = permutation[j];
569 work1[pj] /= work2[j];
570 double tmp = work1[pj];
571 for (int i = j + 1; i < solvedCols; ++i) {
572 work1[permutation[i]] -= jacobian[i * cols + pj] * tmp;
573 }
574 }
575 sum2 = 0;
576 for (int j = 0; j < solvedCols; ++j) {
577 double s = work1[permutation[j]];
578 sum2 += s * s;
579 }
580 double correction = fp / (delta * sum2);
581
582
583 if (fp > 0) {
584 parl = Math.max(parl, lmPar);
585 } else if (fp < 0) {
586 paru = Math.min(paru, lmPar);
587 }
588
589
590 lmPar = Math.max(parl, lmPar + correction);
591
592 }
593 }
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615 private void determineLMDirection(double[] qy, double[] diag,
616 double[] lmDiag, double[] work) {
617
618
619
620 for (int j = 0; j < solvedCols; ++j) {
621 int pj = permutation[j];
622 for (int i = j + 1; i < solvedCols; ++i) {
623 jacobian[i * cols + pj] = jacobian[j * cols + permutation[i]];
624 }
625 lmDir[j] = diagR[pj];
626 work[j] = qy[j];
627 }
628
629
630 for (int j = 0; j < solvedCols; ++j) {
631
632
633
634 int pj = permutation[j];
635 double dpj = diag[pj];
636 if (dpj != 0) {
637 Arrays.fill(lmDiag, j + 1, lmDiag.length, 0);
638 }
639 lmDiag[j] = dpj;
640
641
642
643
644 double qtbpj = 0;
645 for (int k = j; k < solvedCols; ++k) {
646 int pk = permutation[k];
647
648
649
650 if (lmDiag[k] != 0) {
651
652 double sin, cos;
653 double rkk = jacobian[k * cols + pk];
654 if (Math.abs(rkk) < Math.abs(lmDiag[k])) {
655 double cotan = rkk / lmDiag[k];
656 sin = 1.0 / Math.sqrt(1.0 + cotan * cotan);
657 cos = sin * cotan;
658 } else {
659 double tan = lmDiag[k] / rkk;
660 cos = 1.0 / Math.sqrt(1.0 + tan * tan);
661 sin = cos * tan;
662 }
663
664
665
666 jacobian[k * cols + pk] = cos * rkk + sin * lmDiag[k];
667 double temp = cos * work[k] + sin * qtbpj;
668 qtbpj = -sin * work[k] + cos * qtbpj;
669 work[k] = temp;
670
671
672 for (int i = k + 1; i < solvedCols; ++i) {
673 double rik = jacobian[i * cols + pk];
674 temp = cos * rik + sin * lmDiag[i];
675 lmDiag[i] = -sin * rik + cos * lmDiag[i];
676 jacobian[i * cols + pk] = temp;
677 }
678
679 }
680 }
681
682
683
684 int index = j * cols + permutation[j];
685 lmDiag[j] = jacobian[index];
686 jacobian[index] = lmDir[j];
687
688 }
689
690
691
692 int nSing = solvedCols;
693 for (int j = 0; j < solvedCols; ++j) {
694 if ((lmDiag[j] == 0) && (nSing == solvedCols)) {
695 nSing = j;
696 }
697 if (nSing < solvedCols) {
698 work[j] = 0;
699 }
700 }
701 if (nSing > 0) {
702 for (int j = nSing - 1; j >= 0; --j) {
703 int pj = permutation[j];
704 double sum = 0;
705 for (int i = j + 1; i < nSing; ++i) {
706 sum += jacobian[i * cols + pj] * work[i];
707 }
708 work[j] = (work[j] - sum) / lmDiag[j];
709 }
710 }
711
712
713 for (int j = 0; j < lmDir.length; ++j) {
714 lmDir[permutation[j]] = work[j];
715 }
716
717 }
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741 private void qrDecomposition() throws EstimationException {
742
743
744 for (int k = 0; k < cols; ++k) {
745 permutation[k] = k;
746 double norm2 = 0;
747 for (int index = k; index < jacobian.length; index += cols) {
748 double akk = jacobian[index];
749 norm2 += akk * akk;
750 }
751 jacNorm[k] = Math.sqrt(norm2);
752 }
753
754
755 for (int k = 0; k < cols; ++k) {
756
757
758 int nextColumn = -1;
759 double ak2 = Double.NEGATIVE_INFINITY;
760 for (int i = k; i < cols; ++i) {
761 double norm2 = 0;
762 int iDiag = k * cols + permutation[i];
763 for (int index = iDiag; index < jacobian.length; index += cols) {
764 double aki = jacobian[index];
765 norm2 += aki * aki;
766 }
767 if (Double.isInfinite(norm2) || Double.isNaN(norm2)) {
768 throw new EstimationException(
769 "unable to perform Q.R decomposition on the {0}x{1} jacobian matrix",
770 rows, cols);
771 }
772 if (norm2 > ak2) {
773 nextColumn = i;
774 ak2 = norm2;
775 }
776 }
777 if (ak2 == 0) {
778 rank = k;
779 return;
780 }
781 int pk = permutation[nextColumn];
782 permutation[nextColumn] = permutation[k];
783 permutation[k] = pk;
784
785
786 int kDiag = k * cols + pk;
787 double akk = jacobian[kDiag];
788 double alpha = (akk > 0) ? -Math.sqrt(ak2) : Math.sqrt(ak2);
789 double betak = 1.0 / (ak2 - akk * alpha);
790 beta[pk] = betak;
791
792
793 diagR[pk] = alpha;
794 jacobian[kDiag] -= alpha;
795
796
797 for (int dk = cols - 1 - k; dk > 0; --dk) {
798 int dkp = permutation[k + dk] - pk;
799 double gamma = 0;
800 for (int index = kDiag; index < jacobian.length; index += cols) {
801 gamma += jacobian[index] * jacobian[index + dkp];
802 }
803 gamma *= betak;
804 for (int index = kDiag; index < jacobian.length; index += cols) {
805 jacobian[index + dkp] -= gamma * jacobian[index];
806 }
807 }
808
809 }
810
811 rank = solvedCols;
812
813 }
814
815
816
817
818
819
820 private void qTy(double[] y) {
821 for (int k = 0; k < cols; ++k) {
822 int pk = permutation[k];
823 int kDiag = k * cols + pk;
824 double gamma = 0;
825 for (int i = k, index = kDiag; i < rows; ++i, index += cols) {
826 gamma += jacobian[index] * y[i];
827 }
828 gamma *= beta[pk];
829 for (int i = k, index = kDiag; i < rows; ++i, index += cols) {
830 y[i] -= gamma * jacobian[index];
831 }
832 }
833 }
834
835
836 private int solvedCols;
837
838
839 private double[] diagR;
840
841
842 private double[] jacNorm;
843
844
845 private double[] beta;
846
847
848 private int[] permutation;
849
850
851 private int rank;
852
853
854 private double lmPar;
855
856
857 private double[] lmDir;
858
859
860 private double initialStepBoundFactor;
861
862
863 private double costRelativeTolerance;
864
865
866 private double parRelativeTolerance;
867
868
869
870 private double orthoTolerance;
871
872
873 private static final long serialVersionUID = -5705952631533171019L;
874
875 }