1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 | package agents.anac.y2019.harddealer.math3.fitting.leastsquares;
|
---|
18 |
|
---|
19 | import agents.anac.y2019.harddealer.math3.fitting.leastsquares.LeastSquaresProblem.Evaluation;
|
---|
20 | import agents.anac.y2019.harddealer.math3.linear.ArrayRealVector;
|
---|
21 | import agents.anac.y2019.harddealer.math3.linear.DecompositionSolver;
|
---|
22 | import agents.anac.y2019.harddealer.math3.linear.QRDecomposition;
|
---|
23 | import agents.anac.y2019.harddealer.math3.linear.RealMatrix;
|
---|
24 | import agents.anac.y2019.harddealer.math3.linear.RealVector;
|
---|
25 | import agents.anac.y2019.harddealer.math3.util.FastMath;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * An implementation of {@link Evaluation} that is designed for extension. All of the
|
---|
29 | * methods implemented here use the methods that are left unimplemented.
|
---|
30 | * <p/>
|
---|
31 | * TODO cache results?
|
---|
32 | *
|
---|
33 | * @since 3.3
|
---|
34 | */
|
---|
35 | public abstract class AbstractEvaluation implements Evaluation {
|
---|
36 |
|
---|
37 | /** number of observations */
|
---|
38 | private final int observationSize;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Constructor.
|
---|
42 | *
|
---|
43 | * @param observationSize the number of observation. Needed for {@link
|
---|
44 | * #getRMS()}.
|
---|
45 | */
|
---|
46 | AbstractEvaluation(final int observationSize) {
|
---|
47 | this.observationSize = observationSize;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /** {@inheritDoc} */
|
---|
51 | public RealMatrix getCovariances(double threshold) {
|
---|
52 | // Set up the Jacobian.
|
---|
53 | final RealMatrix j = this.getJacobian();
|
---|
54 |
|
---|
55 | // Compute transpose(J)J.
|
---|
56 | final RealMatrix jTj = j.transpose().multiply(j);
|
---|
57 |
|
---|
58 | // Compute the covariances matrix.
|
---|
59 | final DecompositionSolver solver
|
---|
60 | = new QRDecomposition(jTj, threshold).getSolver();
|
---|
61 | return solver.getInverse();
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** {@inheritDoc} */
|
---|
65 | public RealVector getSigma(double covarianceSingularityThreshold) {
|
---|
66 | final RealMatrix cov = this.getCovariances(covarianceSingularityThreshold);
|
---|
67 | final int nC = cov.getColumnDimension();
|
---|
68 | final RealVector sig = new ArrayRealVector(nC);
|
---|
69 | for (int i = 0; i < nC; ++i) {
|
---|
70 | sig.setEntry(i, FastMath.sqrt(cov.getEntry(i,i)));
|
---|
71 | }
|
---|
72 | return sig;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** {@inheritDoc} */
|
---|
76 | public double getRMS() {
|
---|
77 | final double cost = this.getCost();
|
---|
78 | return FastMath.sqrt(cost * cost / this.observationSize);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** {@inheritDoc} */
|
---|
82 | public double getCost() {
|
---|
83 | final ArrayRealVector r = new ArrayRealVector(this.getResiduals());
|
---|
84 | return FastMath.sqrt(r.dotProduct(r));
|
---|
85 | }
|
---|
86 |
|
---|
87 | }
|
---|