View Javadoc

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  
18  package org.apache.commons.math3.optimization;
19  
20  import org.apache.commons.math3.analysis.MultivariateFunction;
21  import org.apache.commons.math3.analysis.MultivariateVectorFunction;
22  import org.apache.commons.math3.exception.DimensionMismatchException;
23  import org.apache.commons.math3.linear.RealMatrix;
24  
25  /** This class converts {@link MultivariateVectorFunction vectorial
26   * objective functions} to {@link MultivariateFunction scalar objective functions}
27   * when the goal is to minimize them.
28   * <p>
29   * This class is mostly used when the vectorial objective function represents
30   * a theoretical result computed from a point set applied to a model and
31   * the models point must be adjusted to fit the theoretical result to some
32   * reference observations. The observations may be obtained for example from
33   * physical measurements whether the model is built from theoretical
34   * considerations.
35   * </p>
36   * <p>
37   * This class computes a possibly weighted squared sum of the residuals, which is
38   * a scalar value. The residuals are the difference between the theoretical model
39   * (i.e. the output of the vectorial objective function) and the observations. The
40   * class implements the {@link MultivariateFunction} interface and can therefore be
41   * minimized by any optimizer supporting scalar objectives functions.This is one way
42   * to perform a least square estimation. There are other ways to do this without using
43   * this converter, as some optimization algorithms directly support vectorial objective
44   * functions.
45   * </p>
46   * <p>
47   * This class support combination of residuals with or without weights and correlations.
48   * </p>
49    *
50   * @see MultivariateFunction
51   * @see MultivariateVectorFunction
52   * @version $Id: LeastSquaresConverter.java 1422230 2012-12-15 12:11:13Z erans $
53   * @deprecated As of 3.1 (to be removed in 4.0).
54   * @since 2.0
55   */
56  
57  @Deprecated
58  public class LeastSquaresConverter implements MultivariateFunction {
59  
60      /** Underlying vectorial function. */
61      private final MultivariateVectorFunction function;
62  
63      /** Observations to be compared to objective function to compute residuals. */
64      private final double[] observations;
65  
66      /** Optional weights for the residuals. */
67      private final double[] weights;
68  
69      /** Optional scaling matrix (weight and correlations) for the residuals. */
70      private final RealMatrix scale;
71  
72      /** Build a simple converter for uncorrelated residuals with the same weight.
73       * @param function vectorial residuals function to wrap
74       * @param observations observations to be compared to objective function to compute residuals
75       */
76      public LeastSquaresConverter(final MultivariateVectorFunction function,
77                                   final double[] observations) {
78          this.function     = function;
79          this.observations = observations.clone();
80          this.weights      = null;
81          this.scale        = null;
82      }
83  
84      /** Build a simple converter for uncorrelated residuals with the specific weights.
85       * <p>
86       * The scalar objective function value is computed as:
87       * <pre>
88       * objective = &sum;weight<sub>i</sub>(observation<sub>i</sub>-objective<sub>i</sub>)<sup>2</sup>
89       * </pre>
90       * </p>
91       * <p>
92       * Weights can be used for example to combine residuals with different standard
93       * deviations. As an example, consider a residuals array in which even elements
94       * are angular measurements in degrees with a 0.01&deg; standard deviation and
95       * odd elements are distance measurements in meters with a 15m standard deviation.
96       * In this case, the weights array should be initialized with value
97       * 1.0/(0.01<sup>2</sup>) in the even elements and 1.0/(15.0<sup>2</sup>) in the
98       * odd elements (i.e. reciprocals of variances).
99       * </p>
100      * <p>
101      * The array computed by the objective function, the observations array and the
102      * weights array must have consistent sizes or a {@link DimensionMismatchException}
103      * will be triggered while computing the scalar objective.
104      * </p>
105      * @param function vectorial residuals function to wrap
106      * @param observations observations to be compared to objective function to compute residuals
107      * @param weights weights to apply to the residuals
108      * @exception DimensionMismatchException if the observations vector and the weights
109      * vector dimensions do not match (objective function dimension is checked only when
110      * the {@link #value(double[])} method is called)
111      */
112     public LeastSquaresConverter(final MultivariateVectorFunction function,
113                                  final double[] observations, final double[] weights) {
114         if (observations.length != weights.length) {
115             throw new DimensionMismatchException(observations.length, weights.length);
116         }
117         this.function     = function;
118         this.observations = observations.clone();
119         this.weights      = weights.clone();
120         this.scale        = null;
121     }
122 
123     /** Build a simple converter for correlated residuals with the specific weights.
124      * <p>
125      * The scalar objective function value is computed as:
126      * <pre>
127      * objective = y<sup>T</sup>y with y = scale&times;(observation-objective)
128      * </pre>
129      * </p>
130      * <p>
131      * The array computed by the objective function, the observations array and the
132      * the scaling matrix must have consistent sizes or a {@link DimensionMismatchException}
133      * will be triggered while computing the scalar objective.
134      * </p>
135      * @param function vectorial residuals function to wrap
136      * @param observations observations to be compared to objective function to compute residuals
137      * @param scale scaling matrix
138      * @throws DimensionMismatchException if the observations vector and the scale
139      * matrix dimensions do not match (objective function dimension is checked only when
140      * the {@link #value(double[])} method is called)
141      */
142     public LeastSquaresConverter(final MultivariateVectorFunction function,
143                                  final double[] observations, final RealMatrix scale) {
144         if (observations.length != scale.getColumnDimension()) {
145             throw new DimensionMismatchException(observations.length, scale.getColumnDimension());
146         }
147         this.function     = function;
148         this.observations = observations.clone();
149         this.weights      = null;
150         this.scale        = scale.copy();
151     }
152 
153     /** {@inheritDoc} */
154     public double value(final double[] point) {
155         // compute residuals
156         final double[] residuals = function.value(point);
157         if (residuals.length != observations.length) {
158             throw new DimensionMismatchException(residuals.length, observations.length);
159         }
160         for (int i = 0; i < residuals.length; ++i) {
161             residuals[i] -= observations[i];
162         }
163 
164         // compute sum of squares
165         double sumSquares = 0;
166         if (weights != null) {
167             for (int i = 0; i < residuals.length; ++i) {
168                 final double ri = residuals[i];
169                 sumSquares +=  weights[i] * ri * ri;
170             }
171         } else if (scale != null) {
172             for (final double yi : scale.operate(residuals)) {
173                 sumSquares += yi * yi;
174             }
175         } else {
176             for (final double ri : residuals) {
177                 sumSquares += ri * ri;
178             }
179         }
180 
181         return sumSquares;
182     }
183 }