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 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22 package org.hipparchus.optim.nonlinear.vector.leastsquares;
23
24 import org.hipparchus.linear.RealMatrix;
25 import org.hipparchus.linear.RealVector;
26 import org.hipparchus.optim.OptimizationProblem;
27
28 /**
29 * The data necessary to define a non-linear least squares problem.
30 * <p>
31 * Includes the observed values, computed model function, and
32 * convergence/divergence criteria. Weights are implicit in {@link
33 * Evaluation#getResiduals()} and {@link Evaluation#getJacobian()}.
34 * </p>
35 * <p>
36 * Instances are typically either created progressively using a {@link
37 * LeastSquaresBuilder builder} or created at once using a {@link LeastSquaresFactory
38 * factory}.
39 * </p>
40 * @see LeastSquaresBuilder
41 * @see LeastSquaresFactory
42 * @see LeastSquaresAdapter
43 *
44 */
45 public interface LeastSquaresProblem extends OptimizationProblem<LeastSquaresProblem.Evaluation> {
46
47 /**
48 * Gets the initial guess.
49 *
50 * @return the initial guess values.
51 */
52 RealVector getStart();
53
54 /**
55 * Get the number of observations (rows in the Jacobian) in this problem.
56 *
57 * @return the number of scalar observations
58 */
59 int getObservationSize();
60
61 /**
62 * Get the number of parameters (columns in the Jacobian) in this problem.
63 *
64 * @return the number of scalar parameters
65 */
66 int getParameterSize();
67
68 /**
69 * Evaluate the model at the specified point.
70 *
71 *
72 * @param point the parameter values.
73 * @return the model's value and derivative at the given point.
74 * @throws org.hipparchus.exception.MathIllegalStateException
75 * if the maximal number of evaluations (of the model vector function) is
76 * exceeded.
77 */
78 Evaluation evaluate(RealVector point);
79
80 /**
81 * An evaluation of a {@link LeastSquaresProblem} at a particular point. This class
82 * also computes several quantities derived from the value and its Jacobian.
83 */
84 interface Evaluation {
85
86 /**
87 * Get the covariance matrix of the optimized parameters. <br> Note that this
88 * operation involves the inversion of the <code>J<sup>T</sup>J</code> matrix,
89 * where {@code J} is the Jacobian matrix. The {@code threshold} parameter is a
90 * way for the caller to specify that the result of this computation should be
91 * considered meaningless, and thus trigger an exception.
92 *
93 * @param threshold Singularity threshold.
94 * @return the covariance matrix.
95 * @throws org.hipparchus.exception.MathIllegalArgumentException
96 * if the covariance matrix cannot be computed (singular problem).
97 */
98 RealMatrix getCovariances(double threshold);
99
100 /**
101 * Get an estimate of the standard deviation of the parameters. The returned
102 * values are the square root of the diagonal coefficients of the covariance
103 * matrix, {@code sd(a[i]) ~= sqrt(C[i][i])}, where {@code a[i]} is the optimized
104 * value of the {@code i}-th parameter, and {@code C} is the covariance matrix.
105 *
106 * @param covarianceSingularityThreshold Singularity threshold (see {@link
107 * #getCovariances(double) computeCovariances}).
108 * @return an estimate of the standard deviation of the optimized parameters
109 * @throws org.hipparchus.exception.MathIllegalArgumentException
110 * if the covariance matrix cannot be computed.
111 */
112 RealVector getSigma(double covarianceSingularityThreshold);
113
114 /**
115 * Get the normalized cost. It is the square-root of the sum of squared of
116 * the residuals, divided by the number of measurements.
117 *
118 * @return the cost.
119 */
120 double getRMS();
121
122 /**
123 * Get the weighted Jacobian matrix.
124 *
125 * @return the weighted Jacobian: W<sup>1/2</sup> J.
126 * @throws org.hipparchus.exception.MathIllegalArgumentException
127 * if the Jacobian dimension does not match problem dimension.
128 */
129 RealMatrix getJacobian();
130
131 /**
132 * Get the cost.
133 * It is the square-root of the {@link #getChiSquare() objective function}.
134 *
135 * @return the cost.
136 * @see #getResiduals()
137 * @see #getChiSquare()
138 */
139 double getCost();
140
141 /**
142 * Get the sum of the squares of the residuals.
143 *
144 * @return the cost.
145 * @see #getResiduals()
146 * @see #getCost()
147 */
148 double getChiSquare();
149
150 /**
151 * Get the reduced chi-square.
152 *
153 * @param n Number of fitted parameters.
154 * @return the sum of the squares of the residuals divided by the number
155 * of degrees of freedom.
156 */
157 double getReducedChiSquare(int n);
158
159 /**
160 * Get the weighted residuals. The residual is the difference between the
161 * observed (target) values and the model (objective function) value. There is one
162 * residual for each element of the vector-valued function. The raw residuals are
163 * then multiplied by the square root of the weight matrix.
164 *
165 * @return the weighted residuals: W<sup>1/2</sup> K.
166 * @throws org.hipparchus.exception.MathIllegalArgumentException
167 * if the residuals have the wrong length.
168 */
169 RealVector getResiduals();
170
171 /**
172 * Get the abscissa (independent variables) of this evaluation.
173 *
174 * @return the point provided to {@link #evaluate(RealVector)}.
175 */
176 RealVector getPoint();
177 }
178 }