001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math4.legacy.fitting.leastsquares;
018
019import org.apache.commons.math4.legacy.fitting.leastsquares.LeastSquaresProblem.Evaluation;
020import org.apache.commons.math4.legacy.linear.ArrayRealVector;
021import org.apache.commons.math4.legacy.linear.DecompositionSolver;
022import org.apache.commons.math4.legacy.linear.QRDecomposition;
023import org.apache.commons.math4.legacy.linear.RealMatrix;
024import org.apache.commons.math4.legacy.linear.RealVector;
025import org.apache.commons.math4.core.jdkmath.JdkMath;
026
027/**
028 * An implementation of {@link Evaluation} that is designed for extension. All of the
029 * methods implemented here use the methods that are left unimplemented.
030 * <p>
031 * TODO cache results?
032 *
033 * @since 3.3
034 */
035public abstract class AbstractEvaluation implements Evaluation {
036
037    /** number of observations. */
038    private final int observationSize;
039
040    /**
041     * Constructor.
042     *
043     * @param observationSize the number of observations.
044     * Needed for {@link #getRMS()} and {@link #getReducedChiSquare(int)}.
045     */
046    AbstractEvaluation(final int observationSize) {
047        this.observationSize = observationSize;
048    }
049
050    /** {@inheritDoc} */
051    @Override
052    public RealMatrix getCovariances(double threshold) {
053        // Set up the Jacobian.
054        final RealMatrix j = this.getJacobian();
055
056        // Compute transpose(J)J.
057        final RealMatrix jTj = j.transpose().multiply(j);
058
059        // Compute the covariances matrix.
060        final DecompositionSolver solver
061                = new QRDecomposition(jTj, threshold).getSolver();
062        return solver.getInverse();
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public RealVector getSigma(double covarianceSingularityThreshold) {
068        final RealMatrix cov = this.getCovariances(covarianceSingularityThreshold);
069        final int nC = cov.getColumnDimension();
070        final RealVector sig = new ArrayRealVector(nC);
071        for (int i = 0; i < nC; ++i) {
072            sig.setEntry(i, JdkMath.sqrt(cov.getEntry(i,i)));
073        }
074        return sig;
075    }
076
077    /** {@inheritDoc} */
078    @Override
079    public double getRMS() {
080        return JdkMath.sqrt(getReducedChiSquare(1));
081    }
082
083    /** {@inheritDoc} */
084    @Override
085    public double getCost() {
086        return JdkMath.sqrt(getChiSquare());
087    }
088
089    /** {@inheritDoc} */
090    @Override
091    public double getChiSquare() {
092        final ArrayRealVector r = new ArrayRealVector(getResiduals());
093        return r.dotProduct(r);
094    }
095
096    /** {@inheritDoc} */
097    @Override
098    public double getReducedChiSquare(int numberOfFittedParameters) {
099        return getChiSquare() / (observationSize - numberOfFittedParameters + 1);
100    }
101}