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.linear.ArrayRealVector;
25 import org.hipparchus.linear.DecompositionSolver;
26 import org.hipparchus.linear.QRDecomposition;
27 import org.hipparchus.linear.RealMatrix;
28 import org.hipparchus.linear.RealVector;
29 import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem.Evaluation;
30 import org.hipparchus.util.FastMath;
31
32
33
34
35
36 public abstract class AbstractEvaluation implements Evaluation {
37
38
39 private final int observationSize;
40
41
42
43
44
45
46
47 public AbstractEvaluation(final int observationSize) {
48 this.observationSize = observationSize;
49 }
50
51
52 @Override
53 public RealMatrix getCovariances(double threshold) {
54
55 final RealMatrix j = this.getJacobian();
56
57
58 final RealMatrix jTj = j.transposeMultiply(j);
59
60
61 final DecompositionSolver solver
62 = new QRDecomposition(jTj, threshold).getSolver();
63 return solver.getInverse();
64 }
65
66
67 @Override
68 public RealVector getSigma(double covarianceSingularityThreshold) {
69 final RealMatrix cov = this.getCovariances(covarianceSingularityThreshold);
70 final int nC = cov.getColumnDimension();
71 final RealVector sig = new ArrayRealVector(nC);
72 for (int i = 0; i < nC; ++i) {
73 sig.setEntry(i, FastMath.sqrt(cov.getEntry(i,i)));
74 }
75 return sig;
76 }
77
78
79 @Override
80 public double getRMS() {
81 return FastMath.sqrt(getReducedChiSquare(1));
82 }
83
84
85 @Override
86 public double getCost() {
87 return FastMath.sqrt(getChiSquare());
88 }
89
90
91 @Override
92 public double getChiSquare() {
93 final ArrayRealVector r = new ArrayRealVector(getResiduals());
94 return r.dotProduct(r);
95 }
96
97
98 @Override
99 public double getReducedChiSquare(int numberOfFittedParameters) {
100 return getChiSquare() / (observationSize - numberOfFittedParameters + 1);
101 }
102 }