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     */
017    
018    package org.apache.commons.math.optimization.fitting;
019    
020    import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer;
021    import org.apache.commons.math.analysis.function.HarmonicOscillator;
022    import org.apache.commons.math.exception.ZeroException;
023    import org.apache.commons.math.exception.NumberIsTooSmallException;
024    import org.apache.commons.math.exception.util.LocalizedFormats;
025    import org.apache.commons.math.util.FastMath;
026    
027    /**
028     * Class that implements a curve fitting specialized for sinusoids.
029     *
030     * Harmonic fitting is a very simple case of curve fitting. The
031     * estimated coefficients are the amplitude a, the pulsation ω and
032     * the phase &phi;: <code>f (t) = a cos (&omega; t + &phi;)</code>. They are
033     * searched by a least square estimator initialized with a rough guess
034     * based on integrals.
035     *
036     * @version $Id: HarmonicFitter.java 1131229 2011-06-03 20:49:25Z luc $
037     * @since 2.0
038     */
039    public class HarmonicFitter extends CurveFitter {
040        /**
041         * Simple constructor.
042         * @param optimizer Optimizer to use for the fitting.
043         */
044        public HarmonicFitter(final DifferentiableMultivariateVectorialOptimizer optimizer) {
045            super(optimizer);
046        }
047    
048        /**
049         * Fit an harmonic function to the observed points.
050         *
051         * @param initialGuess First guess values in the following order:
052         * <ul>
053         *  <li>Amplitude</li>
054         *  <li>Angular frequency</li>
055         *  <li>Phase</li>
056         * </ul>
057         * @return the parameters of the harmonic function that best fits the
058         * observed points (in the same order as above).
059         */
060        public double[] fit(double[] initialGuess) {
061            return fit(new HarmonicOscillator.Parametric(), initialGuess);
062        }
063    
064        /**
065         * Fit an harmonic function to the observed points.
066         * An initial guess will be automatically computed.
067         *
068         * @return the parameters of the harmonic function that best fits the
069         * observed points (see the other {@link #fit(double[]) fit} method.
070         * @throws NumberIsTooSmallException if the sample is too short for the
071         * the first guess to be computed.
072         * @throws ZeroException if the first guess cannot be computed because
073         * the abscissa range is zero.
074         */
075        public double[] fit() {
076            return fit((new ParameterGuesser(getObservations())).guess());
077        }
078    
079        /**
080         * This class guesses harmonic coefficients from a sample.
081         * <p>The algorithm used to guess the coefficients is as follows:</p>
082         *
083         * <p>We know f (t) at some sampling points t<sub>i</sub> and want to find a,
084         * &omega; and &phi; such that f (t) = a cos (&omega; t + &phi;).
085         * </p>
086         *
087         * <p>From the analytical expression, we can compute two primitives :
088         * <pre>
089         *     If2  (t) = &int; f<sup>2</sup>  = a<sup>2</sup> &times; [t + S (t)] / 2
090         *     If'2 (t) = &int; f'<sup>2</sup> = a<sup>2</sup> &omega;<sup>2</sup> &times; [t - S (t)] / 2
091         *     where S (t) = sin (2 (&omega; t + &phi;)) / (2 &omega;)
092         * </pre>
093         * </p>
094         *
095         * <p>We can remove S between these expressions :
096         * <pre>
097         *     If'2 (t) = a<sup>2</sup> &omega;<sup>2</sup> t - &omega;<sup>2</sup> If2 (t)
098         * </pre>
099         * </p>
100         *
101         * <p>The preceding expression shows that If'2 (t) is a linear
102         * combination of both t and If2 (t): If'2 (t) = A &times; t + B &times; If2 (t)
103         * </p>
104         *
105         * <p>From the primitive, we can deduce the same form for definite
106         * integrals between t<sub>1</sub> and t<sub>i</sub> for each t<sub>i</sub> :
107         * <pre>
108         *   If2 (t<sub>i</sub>) - If2 (t<sub>1</sub>) = A &times; (t<sub>i</sub> - t<sub>1</sub>) + B &times; (If2 (t<sub>i</sub>) - If2 (t<sub>1</sub>))
109         * </pre>
110         * </p>
111         *
112         * <p>We can find the coefficients A and B that best fit the sample
113         * to this linear expression by computing the definite integrals for
114         * each sample points.
115         * </p>
116         *
117         * <p>For a bilinear expression z (x<sub>i</sub>, y<sub>i</sub>) = A &times; x<sub>i</sub> + B &times; y<sub>i</sub>, the
118         * coefficients A and B that minimize a least square criterion
119         * &sum; (z<sub>i</sub> - z (x<sub>i</sub>, y<sub>i</sub>))<sup>2</sup> are given by these expressions:</p>
120         * <pre>
121         *
122         *         &sum;y<sub>i</sub>y<sub>i</sub> &sum;x<sub>i</sub>z<sub>i</sub> - &sum;x<sub>i</sub>y<sub>i</sub> &sum;y<sub>i</sub>z<sub>i</sub>
123         *     A = ------------------------
124         *         &sum;x<sub>i</sub>x<sub>i</sub> &sum;y<sub>i</sub>y<sub>i</sub> - &sum;x<sub>i</sub>y<sub>i</sub> &sum;x<sub>i</sub>y<sub>i</sub>
125         *
126         *         &sum;x<sub>i</sub>x<sub>i</sub> &sum;y<sub>i</sub>z<sub>i</sub> - &sum;x<sub>i</sub>y<sub>i</sub> &sum;x<sub>i</sub>z<sub>i</sub>
127         *     B = ------------------------
128         *         &sum;x<sub>i</sub>x<sub>i</sub> &sum;y<sub>i</sub>y<sub>i</sub> - &sum;x<sub>i</sub>y<sub>i</sub> &sum;x<sub>i</sub>y<sub>i</sub>
129         * </pre>
130         * </p>
131         *
132         *
133         * <p>In fact, we can assume both a and &omega; are positive and
134         * compute them directly, knowing that A = a<sup>2</sup> &omega;<sup>2</sup> and that
135         * B = - &omega;<sup>2</sup>. The complete algorithm is therefore:</p>
136         * <pre>
137         *
138         * for each t<sub>i</sub> from t<sub>1</sub> to t<sub>n-1</sub>, compute:
139         *   f  (t<sub>i</sub>)
140         *   f' (t<sub>i</sub>) = (f (t<sub>i+1</sub>) - f(t<sub>i-1</sub>)) / (t<sub>i+1</sub> - t<sub>i-1</sub>)
141         *   x<sub>i</sub> = t<sub>i</sub> - t<sub>1</sub>
142         *   y<sub>i</sub> = &int; f<sup>2</sup> from t<sub>1</sub> to t<sub>i</sub>
143         *   z<sub>i</sub> = &int; f'<sup>2</sup> from t<sub>1</sub> to t<sub>i</sub>
144         *   update the sums &sum;x<sub>i</sub>x<sub>i</sub>, &sum;y<sub>i</sub>y<sub>i</sub>, &sum;x<sub>i</sub>y<sub>i</sub>, &sum;x<sub>i</sub>z<sub>i</sub> and &sum;y<sub>i</sub>z<sub>i</sub>
145         * end for
146         *
147         *            |--------------------------
148         *         \  | &sum;y<sub>i</sub>y<sub>i</sub> &sum;x<sub>i</sub>z<sub>i</sub> - &sum;x<sub>i</sub>y<sub>i</sub> &sum;y<sub>i</sub>z<sub>i</sub>
149         * a     =  \ | ------------------------
150         *           \| &sum;x<sub>i</sub>y<sub>i</sub> &sum;x<sub>i</sub>z<sub>i</sub> - &sum;x<sub>i</sub>x<sub>i</sub> &sum;y<sub>i</sub>z<sub>i</sub>
151         *
152         *
153         *            |--------------------------
154         *         \  | &sum;x<sub>i</sub>y<sub>i</sub> &sum;x<sub>i</sub>z<sub>i</sub> - &sum;x<sub>i</sub>x<sub>i</sub> &sum;y<sub>i</sub>z<sub>i</sub>
155         * &omega;     =  \ | ------------------------
156         *           \| &sum;x<sub>i</sub>x<sub>i</sub> &sum;y<sub>i</sub>y<sub>i</sub> - &sum;x<sub>i</sub>y<sub>i</sub> &sum;x<sub>i</sub>y<sub>i</sub>
157         *
158         * </pre>
159         * </p>
160         *
161         * <p>Once we know &omega;, we can compute:
162         * <pre>
163         *    fc = &omega; f (t) cos (&omega; t) - f' (t) sin (&omega; t)
164         *    fs = &omega; f (t) sin (&omega; t) + f' (t) cos (&omega; t)
165         * </pre>
166         * </p>
167         *
168         * <p>It appears that <code>fc = a &omega; cos (&phi;)</code> and
169         * <code>fs = -a &omega; sin (&phi;)</code>, so we can use these
170         * expressions to compute &phi;. The best estimate over the sample is
171         * given by averaging these expressions.
172         * </p>
173         *
174         * <p>Since integrals and means are involved in the preceding
175         * estimations, these operations run in O(n) time, where n is the
176         * number of measurements.</p>
177         */
178        public static class ParameterGuesser {
179            /** Sampled observations. */
180            private final WeightedObservedPoint[] observations;
181            /** Amplitude. */
182            private double a;
183            /** Angular frequency. */
184            private double omega;
185            /** Phase. */
186            private double phi;
187    
188            /**
189             * Simple constructor.
190             * @param observations sampled observations
191             * @throws NumberIsTooSmallException if the sample is too short or if
192             * the first guess cannot be computed.
193             */
194            public ParameterGuesser(WeightedObservedPoint[] observations) {
195                if (observations.length < 4) {
196                    throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE,
197                                                        observations.length, 4, true);
198                }
199    
200                this.observations = observations.clone();
201            }
202    
203            /**
204             * Estimate a first guess of the coefficients.
205             *
206             * @return the guessed coefficients, in the following order:
207             * <ul>
208             *  <li>Amplitude</li>
209             *  <li>Angular frequency</li>
210             *  <li>Phase</li>
211             * </ul>
212             */
213            public double[] guess() {
214                sortObservations();
215                guessAOmega();
216                guessPhi();
217                return new double[] { a, omega, phi };
218            }
219    
220            /**
221             * Sort the observations with respect to the abscissa.
222             */
223            private void sortObservations() {
224                // Since the samples are almost always already sorted, this
225                // method is implemented as an insertion sort that reorders the
226                // elements in place. Insertion sort is very efficient in this case.
227                WeightedObservedPoint curr = observations[0];
228                for (int j = 1; j < observations.length; ++j) {
229                    WeightedObservedPoint prec = curr;
230                    curr = observations[j];
231                    if (curr.getX() < prec.getX()) {
232                        // the current element should be inserted closer to the beginning
233                        int i = j - 1;
234                        WeightedObservedPoint mI = observations[i];
235                        while ((i >= 0) && (curr.getX() < mI.getX())) {
236                            observations[i + 1] = mI;
237                            if (i-- != 0) {
238                                mI = observations[i];
239                            }
240                        }
241                        observations[i + 1] = curr;
242                        curr = observations[j];
243                    }
244                }
245            }
246    
247            /**
248             * Estimate a first guess of the amplitude and angular frequency.
249             * This method assumes that the {@link #sortObservations()} method
250             * has been called previously.
251             *
252             * @throws ZeroException if the abscissa range is zero.
253             */
254            private void guessAOmega() {
255                // initialize the sums for the linear model between the two integrals
256                double sx2 = 0;
257                double sy2 = 0;
258                double sxy = 0;
259                double sxz = 0;
260                double syz = 0;
261    
262                double currentX = observations[0].getX();
263                double currentY = observations[0].getY();
264                double f2Integral = 0;
265                double fPrime2Integral = 0;
266                final double startX = currentX;
267                for (int i = 1; i < observations.length; ++i) {
268                    // one step forward
269                    final double previousX = currentX;
270                    final double previousY = currentY;
271                    currentX = observations[i].getX();
272                    currentY = observations[i].getY();
273    
274                    // update the integrals of f<sup>2</sup> and f'<sup>2</sup>
275                    // considering a linear model for f (and therefore constant f')
276                    final double dx = currentX - previousX;
277                    final double dy = currentY - previousY;
278                    final double f2StepIntegral =
279                        dx * (previousY * previousY + previousY * currentY + currentY * currentY) / 3;
280                    final double fPrime2StepIntegral = dy * dy / dx;
281    
282                    final double x = currentX - startX;
283                    f2Integral += f2StepIntegral;
284                    fPrime2Integral += fPrime2StepIntegral;
285    
286                    sx2 += x * x;
287                    sy2 += f2Integral * f2Integral;
288                    sxy += x * f2Integral;
289                    sxz += x * fPrime2Integral;
290                    syz += f2Integral * fPrime2Integral;
291                }
292    
293                // compute the amplitude and pulsation coefficients
294                double c1 = sy2 * sxz - sxy * syz;
295                double c2 = sxy * sxz - sx2 * syz;
296                double c3 = sx2 * sy2 - sxy * sxy;
297                if ((c1 / c2 < 0) || (c2 / c3 < 0)) {
298                    final int last = observations.length - 1;
299                    // Range of the observations, assuming that the
300                    // observations are sorted.
301                    final double xRange = observations[last].getX() - observations[0].getX();
302                    if (xRange == 0) {
303                        throw new ZeroException();
304                    }
305                    omega = 2 * Math.PI / xRange;
306    
307                    double yMin = Double.POSITIVE_INFINITY;
308                    double yMax = Double.NEGATIVE_INFINITY;
309                    for (int i = 1; i < observations.length; ++i) {
310                        final double y = observations[i].getY();
311                        if (y < yMin) {
312                            yMin = y;
313                        }
314                        if (y > yMax) {
315                            yMax = y;
316                        }
317                    }
318                    a = 0.5 * (yMax - yMin);
319                } else {
320                    a = FastMath.sqrt(c1 / c2);
321                    omega = FastMath.sqrt(c2 / c3);
322                }
323            }
324    
325            /**
326             * Estimate a first guess of the phase.
327             */
328            private void guessPhi() {
329                // initialize the means
330                double fcMean = 0;
331                double fsMean = 0;
332    
333                double currentX = observations[0].getX();
334                double currentY = observations[0].getY();
335                for (int i = 1; i < observations.length; ++i) {
336                    // one step forward
337                    final double previousX = currentX;
338                    final double previousY = currentY;
339                    currentX = observations[i].getX();
340                    currentY = observations[i].getY();
341                    final double currentYPrime = (currentY - previousY) / (currentX - previousX);
342    
343                    double omegaX = omega * currentX;
344                    double cosine = FastMath.cos(omegaX);
345                    double sine = FastMath.sin(omegaX);
346                    fcMean += omega * currentY * cosine - currentYPrime * sine;
347                    fsMean += omega * currentY * sine + currentYPrime * cosine;
348                }
349    
350                phi = FastMath.atan2(-fsMean, fcMean);
351            }
352        }
353    }