1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.fitting.leastsquares;
18
19 import org.apache.commons.math4.legacy.fitting.leastsquares.LeastSquaresProblem.Evaluation;
20 import org.apache.commons.math4.legacy.linear.ArrayRealVector;
21 import org.apache.commons.math4.legacy.linear.DecompositionSolver;
22 import org.apache.commons.math4.legacy.linear.QRDecomposition;
23 import org.apache.commons.math4.legacy.linear.RealMatrix;
24 import org.apache.commons.math4.legacy.linear.RealVector;
25 import org.apache.commons.math4.core.jdkmath.JdkMath;
26
27
28
29
30
31
32
33
34
35 public abstract class AbstractEvaluation implements Evaluation {
36
37
38 private final int observationSize;
39
40
41
42
43
44
45
46 AbstractEvaluation(final int observationSize) {
47 this.observationSize = observationSize;
48 }
49
50
51 @Override
52 public RealMatrix getCovariances(double threshold) {
53
54 final RealMatrix j = this.getJacobian();
55
56
57 final RealMatrix jTj = j.transpose().multiply(j);
58
59
60 final DecompositionSolver solver
61 = new QRDecomposition(jTj, threshold).getSolver();
62 return solver.getInverse();
63 }
64
65
66 @Override
67 public RealVector getSigma(double covarianceSingularityThreshold) {
68 final RealMatrix cov = this.getCovariances(covarianceSingularityThreshold);
69 final int nC = cov.getColumnDimension();
70 final RealVector sig = new ArrayRealVector(nC);
71 for (int i = 0; i < nC; ++i) {
72 sig.setEntry(i, JdkMath.sqrt(cov.getEntry(i,i)));
73 }
74 return sig;
75 }
76
77
78 @Override
79 public double getRMS() {
80 return JdkMath.sqrt(getReducedChiSquare(1));
81 }
82
83
84 @Override
85 public double getCost() {
86 return JdkMath.sqrt(getChiSquare());
87 }
88
89
90 @Override
91 public double getChiSquare() {
92 final ArrayRealVector r = new ArrayRealVector(getResiduals());
93 return r.dotProduct(r);
94 }
95
96
97 @Override
98 public double getReducedChiSquare(int numberOfFittedParameters) {
99 return getChiSquare() / (observationSize - numberOfFittedParameters + 1);
100 }
101 }