1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.analysis.interpolation;
18
19 import java.io.Serializable;
20 import java.util.Arrays;
21
22 import org.apache.commons.math.MathException;
23 import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class LoessInterpolator
42 implements UnivariateRealInterpolator, Serializable {
43
44
45 private static final long serialVersionUID = 5204927143605193821L;
46
47
48
49
50 public static final double DEFAULT_BANDWIDTH = 0.3;
51
52
53
54 public static final int DEFAULT_ROBUSTNESS_ITERS = 2;
55
56
57
58
59
60
61
62
63
64 private final double bandwidth;
65
66
67
68
69
70
71
72
73 private final int robustnessIters;
74
75
76
77
78
79
80
81
82 public LoessInterpolator() {
83 this.bandwidth = DEFAULT_BANDWIDTH;
84 this.robustnessIters = DEFAULT_ROBUSTNESS_ITERS;
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 public LoessInterpolator(double bandwidth, int robustnessIters) throws MathException {
105 if (bandwidth < 0 || bandwidth > 1) {
106 throw new MathException("bandwidth must be in the interval [0,1], but got {0}",
107 bandwidth);
108 }
109 this.bandwidth = bandwidth;
110 if (robustnessIters < 0) {
111 throw new MathException("the number of robustness iterations must " +
112 "be non-negative, but got {0}",
113 robustnessIters);
114 }
115 this.robustnessIters = robustnessIters;
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 public final PolynomialSplineFunction interpolate(
136 final double[] xval, final double[] yval) throws MathException {
137 return new SplineInterpolator().interpolate(xval, smooth(xval, yval));
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public final double[] smooth(final double[] xval, final double[] yval)
154 throws MathException {
155 if (xval.length != yval.length) {
156 throw new MathException(
157 "Loess expects the abscissa and ordinate arrays " +
158 "to be of the same size, " +
159 "but got {0} abscisssae and {1} ordinatae",
160 xval.length, yval.length);
161 }
162
163 final int n = xval.length;
164
165 if (n == 0) {
166 throw new MathException("Loess expects at least 1 point");
167 }
168
169 checkAllFiniteReal(xval, true);
170 checkAllFiniteReal(yval, false);
171
172 checkStrictlyIncreasing(xval);
173
174 if (n == 1) {
175 return new double[]{yval[0]};
176 }
177
178 if (n == 2) {
179 return new double[]{yval[0], yval[1]};
180 }
181
182 int bandwidthInPoints = (int) (bandwidth * n);
183
184 if (bandwidthInPoints < 2) {
185 throw new MathException(
186 "the bandwidth must be large enough to " +
187 "accomodate at least 2 points. There are {0} " +
188 " data points, and bandwidth must be at least {1} " +
189 " but it is only {2}",
190 n, 2.0 / n, bandwidth);
191 }
192
193 final double[] res = new double[n];
194
195 final double[] residuals = new double[n];
196 final double[] sortedResiduals = new double[n];
197
198 final double[] robustnessWeights = new double[n];
199
200
201
202
203 Arrays.fill(robustnessWeights, 1);
204
205 for (int iter = 0; iter <= robustnessIters; ++iter) {
206 final int[] bandwidthInterval = {0, bandwidthInPoints - 1};
207
208 for (int i = 0; i < n; ++i) {
209 final double x = xval[i];
210
211
212
213 if (i > 0) {
214 updateBandwidthInterval(xval, i, bandwidthInterval);
215 }
216
217 final int ileft = bandwidthInterval[0];
218 final int iright = bandwidthInterval[1];
219
220
221
222 final int edge;
223 if (xval[i] - xval[ileft] > xval[iright] - xval[i]) {
224 edge = ileft;
225 } else {
226 edge = iright;
227 }
228
229
230
231
232
233
234
235
236 double sumWeights = 0;
237 double sumX = 0, sumXSquared = 0, sumY = 0, sumXY = 0;
238 double denom = Math.abs(1.0 / (xval[edge] - x));
239 for (int k = ileft; k <= iright; ++k) {
240 final double xk = xval[k];
241 final double yk = yval[k];
242 double dist;
243 if (k < i) {
244 dist = (x - xk);
245 } else {
246 dist = (xk - x);
247 }
248 final double w = tricube(dist * denom) * robustnessWeights[k];
249 final double xkw = xk * w;
250 sumWeights += w;
251 sumX += xkw;
252 sumXSquared += xk * xkw;
253 sumY += yk * w;
254 sumXY += yk * xkw;
255 }
256
257 final double meanX = sumX / sumWeights;
258 final double meanY = sumY / sumWeights;
259 final double meanXY = sumXY / sumWeights;
260 final double meanXSquared = sumXSquared / sumWeights;
261
262 final double beta;
263 if (meanXSquared == meanX * meanX) {
264 beta = 0;
265 } else {
266 beta = (meanXY - meanX * meanY) / (meanXSquared - meanX * meanX);
267 }
268
269 final double alpha = meanY - beta * meanX;
270
271 res[i] = beta * x + alpha;
272 residuals[i] = Math.abs(yval[i] - res[i]);
273 }
274
275
276
277 if (iter == robustnessIters) {
278 break;
279 }
280
281
282
283
284
285
286 System.arraycopy(residuals, 0, sortedResiduals, 0, n);
287 Arrays.sort(sortedResiduals);
288 final double medianResidual = sortedResiduals[n / 2];
289
290 if (medianResidual == 0) {
291 break;
292 }
293
294 for (int i = 0; i < n; ++i) {
295 final double arg = residuals[i] / (6 * medianResidual);
296 robustnessWeights[i] = (arg >= 1) ? 0 : Math.pow(1 - arg * arg, 2);
297 }
298 }
299
300 return res;
301 }
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 private static void updateBandwidthInterval(final double[] xval, final int i,
317 final int[] bandwidthInterval) {
318 final int left = bandwidthInterval[0];
319 final int right = bandwidthInterval[1];
320
321
322
323 if (right < xval.length - 1 &&
324 xval[right+1] - xval[i] < xval[i] - xval[left]) {
325 bandwidthInterval[0]++;
326 bandwidthInterval[1]++;
327 }
328 }
329
330
331
332
333
334
335
336
337
338 private static double tricube(final double x) {
339 final double tmp = 1 - x * x * x;
340 return tmp * tmp * tmp;
341 }
342
343
344
345
346
347
348
349
350
351 private static void checkAllFiniteReal(final double[] values, final boolean isAbscissae)
352 throws MathException {
353 for (int i = 0; i < values.length; i++) {
354 final double x = values[i];
355 if (Double.isInfinite(x) || Double.isNaN(x)) {
356 final String pattern = isAbscissae ?
357 "all abscissae must be finite real numbers, but {0}-th is {1}" :
358 "all ordinatae must be finite real numbers, but {0}-th is {1}";
359 throw new MathException(pattern, i, x);
360 }
361 }
362 }
363
364
365
366
367
368
369
370
371
372 private static void checkStrictlyIncreasing(final double[] xval)
373 throws MathException {
374 for (int i = 0; i < xval.length; ++i) {
375 if (i >= 1 && xval[i - 1] >= xval[i]) {
376 throw new MathException(
377 "the abscissae array must be sorted in a strictly " +
378 "increasing order, but the {0}-th element is {1} " +
379 "whereas {2}-th is {3}",
380 i - 1, xval[i - 1], i, xval[i]);
381 }
382 }
383 }
384 }