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  /*
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.fitting;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Random;
27  
28  import org.hipparchus.analysis.function.HarmonicOscillator;
29  import org.hipparchus.exception.MathIllegalArgumentException;
30  import org.hipparchus.exception.MathIllegalStateException;
31  import org.hipparchus.util.FastMath;
32  import org.hipparchus.util.MathUtils;
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  public class HarmonicCurveFitterTest {
37      /**
38       * Zero points is not enough observed points.
39       */
40      @Test(expected=MathIllegalArgumentException.class)
41      public void testPreconditions1() {
42          HarmonicCurveFitter.create().fit(new WeightedObservedPoints().toList());
43      }
44  
45      @Test
46      public void testNoError() {
47          final double a = 0.2;
48          final double w = 3.4;
49          final double p = 4.1;
50          final HarmonicOscillator f = new HarmonicOscillator(a, w, p);
51  
52          final WeightedObservedPoints points = new WeightedObservedPoints();
53          for (double x = 0.0; x < 1.3; x += 0.01) {
54              points.add(1, x, f.value(x));
55          }
56  
57          final HarmonicCurveFitter fitter = HarmonicCurveFitter.create();
58          final double[] fitted = fitter.fit(points.toList());
59          Assert.assertEquals(a, fitted[0], 1.0e-13);
60          Assert.assertEquals(w, fitted[1], 1.0e-13);
61          Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);
62  
63          final HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);
64          for (double x = -1.0; x < 1.0; x += 0.01) {
65              Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
66          }
67      }
68  
69      @Test
70      public void test1PercentError() {
71          final Random randomizer = new Random(64925784252L);
72          final double a = 0.2;
73          final double w = 3.4;
74          final double p = 4.1;
75          final HarmonicOscillator f = new HarmonicOscillator(a, w, p);
76  
77          final WeightedObservedPoints points = new WeightedObservedPoints();
78          for (double x = 0.0; x < 10.0; x += 0.1) {
79              points.add(1, x, f.value(x) + 0.01 * randomizer.nextGaussian());
80          }
81  
82          final HarmonicCurveFitter fitter = HarmonicCurveFitter.create();
83          final double[] fitted = fitter.fit(points.toList());
84          Assert.assertEquals(a, fitted[0], 7.6e-4);
85          Assert.assertEquals(w, fitted[1], 2.7e-3);
86          Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2);
87      }
88  
89      @Test
90      public void testTinyVariationsData() {
91          final Random randomizer = new Random(64925784252L);
92  
93          final WeightedObservedPoints points = new WeightedObservedPoints();
94          for (double x = 0.0; x < 10.0; x += 0.1) {
95              points.add(1, x, 1e-7 * randomizer.nextGaussian());
96          }
97  
98          final HarmonicCurveFitter fitter = HarmonicCurveFitter.create();
99          fitter.fit(points.toList());
100 
101         // This test serves to cover the part of the code of "guessAOmega"
102         // when the algorithm using integrals fails.
103     }
104 
105     @Test
106     public void testInitialGuess() {
107         final Random randomizer = new Random(45314242L);
108         final double a = 0.2;
109         final double w = 3.4;
110         final double p = 4.1;
111         final HarmonicOscillator f = new HarmonicOscillator(a, w, p);
112 
113         final WeightedObservedPoints points = new WeightedObservedPoints();
114         for (double x = 0.0; x < 10.0; x += 0.1) {
115             points.add(1, x, f.value(x) + 0.01 * randomizer.nextGaussian());
116         }
117 
118         final HarmonicCurveFitter fitter = HarmonicCurveFitter.create()
119             .withStartPoint(new double[] { 0.15, 3.6, 4.5 });
120         final double[] fitted = fitter.fit(points.toList());
121         Assert.assertEquals(a, fitted[0], 1.2e-3);
122         Assert.assertEquals(w, fitted[1], 3.3e-3);
123         Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2);
124     }
125 
126     @Test
127     public void testUnsorted() {
128         Random randomizer = new Random(64925784252L);
129         final double a = 0.2;
130         final double w = 3.4;
131         final double p = 4.1;
132         final HarmonicOscillator f = new HarmonicOscillator(a, w, p);
133 
134         // Build a regularly spaced array of measurements.
135         final int size = 100;
136         final double[] xTab = new double[size];
137         final double[] yTab = new double[size];
138         for (int i = 0; i < size; i++) {
139             xTab[i] = 0.1 * i;
140             yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
141         }
142 
143         // shake it
144         for (int i = 0; i < size; i++) {
145             int i1 = randomizer.nextInt(size);
146             int i2 = randomizer.nextInt(size);
147             double xTmp = xTab[i1];
148             double yTmp = yTab[i1];
149             xTab[i1] = xTab[i2];
150             yTab[i1] = yTab[i2];
151             xTab[i2] = xTmp;
152             yTab[i2] = yTmp;
153         }
154 
155         // Pass it to the fitter.
156         final WeightedObservedPoints points = new WeightedObservedPoints();
157         for (int i = 0; i < size; ++i) {
158             points.add(1, xTab[i], yTab[i]);
159         }
160 
161         final HarmonicCurveFitter fitter = HarmonicCurveFitter.create();
162         final double[] fitted = fitter.fit(points.toList());
163         Assert.assertEquals(a, fitted[0], 7.6e-4);
164         Assert.assertEquals(w, fitted[1], 3.5e-3);
165         Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
166     }
167 
168     @Test(expected=MathIllegalStateException.class)
169     public void testMath844() {
170         final double[] y = { 0, 1, 2, 3, 2, 1,
171                              0, -1, -2, -3, -2, -1,
172                              0, 1, 2, 3, 2, 1,
173                              0, -1, -2, -3, -2, -1,
174                              0, 1, 2, 3, 2, 1, 0 };
175         final List<WeightedObservedPoint> points = new ArrayList<WeightedObservedPoint>();
176         for (int i = 0; i < y.length; i++) {
177             points.add(new WeightedObservedPoint(1, i, y[i]));
178         }
179 
180         // The guesser fails because the function is far from an harmonic
181         // function: It is a triangular periodic function with amplitude 3
182         // and period 12, and all sample points are taken at integer abscissae
183         // so function values all belong to the integer subset {-3, -2, -1, 0,
184         // 1, 2, 3}.
185         new HarmonicCurveFitter.ParameterGuesser(points);
186     }
187 }