summaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/math/optimization/fitting/GaussianParametersGuesser.java
blob: 3fb3698aaf29b6983ae7408006f4f1f918bde77d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.math.optimization.fitting;

import java.util.Arrays;
import java.util.Comparator;

import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.ZeroException;
import org.apache.commons.math.exception.NullArgumentException;

/**
 * Guesses the parameters ({@code a}, {@code b}, {@code c}, and {@code d})
 * of a {@link ParametricGaussianFunction} based on the specified observed
 * points.
 *
 * @since 2.2
 * @version $Revision: 983921 $ $Date: 2010-08-10 12:46:06 +0200 (mar. 10 août 2010) $
 */
public class GaussianParametersGuesser {

    /** Observed points. */
    private final WeightedObservedPoint[] observations;

    /** Resulting guessed parameters. */
    private double[] parameters;

    /**
     * Constructs instance with the specified observed points.
     *
     * @param observations observed points upon which should base guess
     */
    public GaussianParametersGuesser(WeightedObservedPoint[] observations) {
        if (observations == null) {
            throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
        }
        if (observations.length < 3) {
            throw new NumberIsTooSmallException(observations.length, 3, true);
        }
        this.observations = observations.clone();
    }

    /**
     * Guesses the parameters based on the observed points.
     *
     * @return guessed parameters array <code>{a, b, c, d}</code>
     */
    public double[] guess() {
        if (parameters == null) {
            parameters = basicGuess(observations);
        }
        return parameters.clone();
    }

    /**
     * Guesses the parameters based on the specified observed points.
     *
     * @param points observed points upon which should base guess
     *
     * @return guessed parameters array <code>{a, b, c, d}</code>
     */
    private double[] basicGuess(WeightedObservedPoint[] points) {
        Arrays.sort(points, createWeightedObservedPointComparator());
        double[] params = new double[4];

        int minYIdx = findMinY(points);
        params[0] = points[minYIdx].getY();

        int maxYIdx = findMaxY(points);
        params[1] = points[maxYIdx].getY();
        params[2] = points[maxYIdx].getX();

        double fwhmApprox;
        try {
            double halfY = params[0] + ((params[1] - params[0]) / 2.0);
            double fwhmX1 = interpolateXAtY(points, maxYIdx, -1, halfY);
            double fwhmX2 = interpolateXAtY(points, maxYIdx, +1, halfY);
            fwhmApprox = fwhmX2 - fwhmX1;
        } catch (OutOfRangeException e) {
            fwhmApprox = points[points.length - 1].getX() - points[0].getX();
        }
        params[3] = fwhmApprox / (2.0 * Math.sqrt(2.0 * Math.log(2.0)));

        return params;
    }

    /**
     * Finds index of point in specified points with the smallest Y.
     *
     * @param points points to search
     *
     * @return index in specified points array
     */
    private int findMinY(WeightedObservedPoint[] points) {
        int minYIdx = 0;
        for (int i = 1; i < points.length; i++) {
            if (points[i].getY() < points[minYIdx].getY()) {
                minYIdx = i;
            }
        }
        return minYIdx;
    }

    /**
     * Finds index of point in specified points with the largest Y.
     *
     * @param points points to search
     *
     * @return index in specified points array
     */
    private int findMaxY(WeightedObservedPoint[] points) {
        int maxYIdx = 0;
        for (int i = 1; i < points.length; i++) {
            if (points[i].getY() > points[maxYIdx].getY()) {
                maxYIdx = i;
            }
        }
        return maxYIdx;
    }

    /**
     * Interpolates using the specified points to determine X at the specified
     * Y.
     *
     * @param points points to use for interpolation
     * @param startIdx index within points from which to start search for
     *        interpolation bounds points
     * @param idxStep index step for search for interpolation bounds points
     * @param y Y value for which X should be determined
     *
     * @return value of X at the specified Y
     *
     * @throws IllegalArgumentException if idxStep is 0
     * @throws OutOfRangeException if specified <code>y</code> is not within the
     *         range of the specified <code>points</code>
     */
    private double interpolateXAtY(WeightedObservedPoint[] points,
                                   int startIdx, int idxStep, double y) throws OutOfRangeException {
        if (idxStep == 0) {
            throw new ZeroException();
        }
        WeightedObservedPoint[] twoPoints = getInterpolationPointsForY(points, startIdx, idxStep, y);
        WeightedObservedPoint pointA = twoPoints[0];
        WeightedObservedPoint pointB = twoPoints[1];
        if (pointA.getY() == y) {
            return pointA.getX();
        }
        if (pointB.getY() == y) {
            return pointB.getX();
        }
        return pointA.getX() +
               (((y - pointA.getY()) * (pointB.getX() - pointA.getX())) / (pointB.getY() - pointA.getY()));
    }

    /**
     * Gets the two bounding interpolation points from the specified points
     * suitable for determining X at the specified Y.
     *
     * @param points points to use for interpolation
     * @param startIdx index within points from which to start search for
     *        interpolation bounds points
     * @param idxStep index step for search for interpolation bounds points
     * @param y Y value for which X should be determined
     *
     * @return array containing two points suitable for determining X at the
     *         specified Y
     *
     * @throws IllegalArgumentException if idxStep is 0
     * @throws OutOfRangeException if specified <code>y</code> is not within the
     *         range of the specified <code>points</code>
     */
    private WeightedObservedPoint[] getInterpolationPointsForY(WeightedObservedPoint[] points,
                                                               int startIdx, int idxStep, double y)
        throws OutOfRangeException {
        if (idxStep == 0) {
            throw new ZeroException();
        }
        for (int i = startIdx;
             (idxStep < 0) ? (i + idxStep >= 0) : (i + idxStep < points.length);
             i += idxStep) {
            if (isBetween(y, points[i].getY(), points[i + idxStep].getY())) {
                return (idxStep < 0) ?
                       new WeightedObservedPoint[] { points[i + idxStep], points[i] } :
                       new WeightedObservedPoint[] { points[i], points[i + idxStep] };
            }
        }

        double minY = Double.POSITIVE_INFINITY;
        double maxY = Double.NEGATIVE_INFINITY;
        for (final WeightedObservedPoint point : points) {
            minY = Math.min(minY, point.getY());
            maxY = Math.max(maxY, point.getY());
        }
        throw new OutOfRangeException(y, minY, maxY);

    }

    /**
     * Determines whether a value is between two other values.
     *
     * @param value value to determine whether is between <code>boundary1</code>
     *        and <code>boundary2</code>
     * @param boundary1 one end of the range
     * @param boundary2 other end of the range
     *
     * @return true if <code>value</code> is between <code>boundary1</code> and
     *         <code>boundary2</code> (inclusive); false otherwise
     */
    private boolean isBetween(double value, double boundary1, double boundary2) {
        return (value >= boundary1 && value <= boundary2) ||
               (value >= boundary2 && value <= boundary1);
    }

    /**
     * Factory method creating <code>Comparator</code> for comparing
     * <code>WeightedObservedPoint</code> instances.
     *
     * @return new <code>Comparator</code> instance
     */
    private Comparator<WeightedObservedPoint> createWeightedObservedPointComparator() {
        return new Comparator<WeightedObservedPoint>() {
            public int compare(WeightedObservedPoint p1, WeightedObservedPoint p2) {
                if (p1 == null && p2 == null) {
                    return 0;
                }
                if (p1 == null) {
                    return -1;
                }
                if (p2 == null) {
                    return 1;
                }
                if (p1.getX() < p2.getX()) {
                    return -1;
                }
                if (p1.getX() > p2.getX()) {
                    return 1;
                }
                if (p1.getY() < p2.getY()) {
                    return -1;
                }
                if (p1.getY() > p2.getY()) {
                    return 1;
                }
                if (p1.getWeight() < p2.getWeight()) {
                    return -1;
                }
                if (p1.getWeight() > p2.getWeight()) {
                    return 1;
                }
                return 0;
            }
        };
    }
}