AbstractEvaluation.java

  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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.optim.nonlinear.vector.leastsquares;

  22. import org.hipparchus.linear.ArrayRealVector;
  23. import org.hipparchus.linear.DecompositionSolver;
  24. import org.hipparchus.linear.QRDecomposition;
  25. import org.hipparchus.linear.RealMatrix;
  26. import org.hipparchus.linear.RealVector;
  27. import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem.Evaluation;
  28. import org.hipparchus.util.FastMath;

  29. /**
  30.  * An implementation of {@link Evaluation} that is designed for extension. All of the
  31.  * methods implemented here use the methods that are left unimplemented.
  32.  */
  33. public abstract class AbstractEvaluation implements Evaluation {

  34.     /** number of observations */
  35.     private final int observationSize;

  36.     /**
  37.      * Constructor.
  38.      *
  39.      * @param observationSize the number of observations.
  40.      * Needed for {@link #getRMS()} and {@link #getReducedChiSquare(int)}.
  41.      */
  42.     protected AbstractEvaluation(final int observationSize) {
  43.         this.observationSize = observationSize;
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public RealMatrix getCovariances(double threshold) {
  48.         // Set up the Jacobian.
  49.         final RealMatrix j = this.getJacobian();

  50.         // Compute transpose(J)J.
  51.         final RealMatrix jTj = j.transposeMultiply(j);

  52.         // Compute the covariances matrix.
  53.         final DecompositionSolver solver
  54.                 = new QRDecomposition(jTj, threshold).getSolver();
  55.         return solver.getInverse();
  56.     }

  57.     /** {@inheritDoc} */
  58.     @Override
  59.     public RealVector getSigma(double covarianceSingularityThreshold) {
  60.         final RealMatrix cov = this.getCovariances(covarianceSingularityThreshold);
  61.         final int nC = cov.getColumnDimension();
  62.         final RealVector sig = new ArrayRealVector(nC);
  63.         for (int i = 0; i < nC; ++i) {
  64.             sig.setEntry(i, FastMath.sqrt(cov.getEntry(i,i)));
  65.         }
  66.         return sig;
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public double getRMS() {
  71.         return FastMath.sqrt(getReducedChiSquare(1));
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public double getCost() {
  76.         return FastMath.sqrt(getChiSquare());
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public double getChiSquare() {
  81.         final ArrayRealVector r = new ArrayRealVector(getResiduals());
  82.         return r.dotProduct(r);
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public double getReducedChiSquare(int numberOfFittedParameters) {
  87.         return getChiSquare() / (observationSize - numberOfFittedParameters + 1);
  88.     }
  89. }