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.math3.fitting.leastsquares;
018
019import org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem.Evaluation;
020import org.apache.commons.math3.linear.ArrayRealVector;
021import org.apache.commons.math3.linear.DecompositionSolver;
022import org.apache.commons.math3.linear.QRDecomposition;
023import org.apache.commons.math3.linear.RealMatrix;
024import org.apache.commons.math3.linear.RealVector;
025import org.apache.commons.math3.util.FastMath;
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 observation. Needed for {@link
044     *                        #getRMS()}.
045     */
046    AbstractEvaluation(final int observationSize) {
047        this.observationSize = observationSize;
048    }
049
050    /** {@inheritDoc} */
051    public RealMatrix getCovariances(double threshold) {
052        // Set up the Jacobian.
053        final RealMatrix j = this.getJacobian();
054
055        // Compute transpose(J)J.
056        final RealMatrix jTj = j.transpose().multiply(j);
057
058        // Compute the covariances matrix.
059        final DecompositionSolver solver
060                = new QRDecomposition(jTj, threshold).getSolver();
061        return solver.getInverse();
062    }
063
064    /** {@inheritDoc} */
065    public RealVector getSigma(double covarianceSingularityThreshold) {
066        final RealMatrix cov = this.getCovariances(covarianceSingularityThreshold);
067        final int nC = cov.getColumnDimension();
068        final RealVector sig = new ArrayRealVector(nC);
069        for (int i = 0; i < nC; ++i) {
070            sig.setEntry(i, FastMath.sqrt(cov.getEntry(i,i)));
071        }
072        return sig;
073    }
074
075    /** {@inheritDoc} */
076    public double getRMS() {
077        final double cost = this.getCost();
078        return FastMath.sqrt(cost * cost / this.observationSize);
079    }
080
081    /** {@inheritDoc} */
082    public double getCost() {
083        final ArrayRealVector r = new ArrayRealVector(this.getResiduals());
084        return FastMath.sqrt(r.dotProduct(r));
085    }
086
087}