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 | package agents.anac.y2019.harddealer.math3.fitting;
|
---|
18 |
|
---|
19 | import java.util.Collection;
|
---|
20 |
|
---|
21 | import agents.anac.y2019.harddealer.math3.analysis.polynomials.PolynomialFunction;
|
---|
22 | import agents.anac.y2019.harddealer.math3.exception.MathInternalError;
|
---|
23 | import agents.anac.y2019.harddealer.math3.fitting.leastsquares.LeastSquaresBuilder;
|
---|
24 | import agents.anac.y2019.harddealer.math3.fitting.leastsquares.LeastSquaresProblem;
|
---|
25 | import agents.anac.y2019.harddealer.math3.linear.DiagonalMatrix;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Fits points to a {@link
|
---|
29 | * agents.anac.y2019.harddealer.math3.analysis.polynomials.PolynomialFunction.Parametric polynomial}
|
---|
30 | * function.
|
---|
31 | * <br/>
|
---|
32 | * The size of the {@link #withStartPoint(double[]) initial guess} array defines the
|
---|
33 | * degree of the polynomial to be fitted.
|
---|
34 | * They must be sorted in increasing order of the polynomial's degree.
|
---|
35 | * The optimal values of the coefficients will be returned in the same order.
|
---|
36 | *
|
---|
37 | * @since 3.3
|
---|
38 | */
|
---|
39 | public class PolynomialCurveFitter extends AbstractCurveFitter {
|
---|
40 | /** Parametric function to be fitted. */
|
---|
41 | private static final PolynomialFunction.Parametric FUNCTION = new PolynomialFunction.Parametric();
|
---|
42 | /** Initial guess. */
|
---|
43 | private final double[] initialGuess;
|
---|
44 | /** Maximum number of iterations of the optimization algorithm. */
|
---|
45 | private final int maxIter;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Contructor used by the factory methods.
|
---|
49 | *
|
---|
50 | * @param initialGuess Initial guess.
|
---|
51 | * @param maxIter Maximum number of iterations of the optimization algorithm.
|
---|
52 | * @throws MathInternalError if {@code initialGuess} is {@code null}.
|
---|
53 | */
|
---|
54 | private PolynomialCurveFitter(double[] initialGuess,
|
---|
55 | int maxIter) {
|
---|
56 | this.initialGuess = initialGuess;
|
---|
57 | this.maxIter = maxIter;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Creates a default curve fitter.
|
---|
62 | * Zero will be used as initial guess for the coefficients, and the maximum
|
---|
63 | * number of iterations of the optimization algorithm is set to
|
---|
64 | * {@link Integer#MAX_VALUE}.
|
---|
65 | *
|
---|
66 | * @param degree Degree of the polynomial to be fitted.
|
---|
67 | * @return a curve fitter.
|
---|
68 | *
|
---|
69 | * @see #withStartPoint(double[])
|
---|
70 | * @see #withMaxIterations(int)
|
---|
71 | */
|
---|
72 | public static PolynomialCurveFitter create(int degree) {
|
---|
73 | return new PolynomialCurveFitter(new double[degree + 1], Integer.MAX_VALUE);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Configure the start point (initial guess).
|
---|
78 | * @param newStart new start point (initial guess)
|
---|
79 | * @return a new instance.
|
---|
80 | */
|
---|
81 | public PolynomialCurveFitter withStartPoint(double[] newStart) {
|
---|
82 | return new PolynomialCurveFitter(newStart.clone(),
|
---|
83 | maxIter);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Configure the maximum number of iterations.
|
---|
88 | * @param newMaxIter maximum number of iterations
|
---|
89 | * @return a new instance.
|
---|
90 | */
|
---|
91 | public PolynomialCurveFitter withMaxIterations(int newMaxIter) {
|
---|
92 | return new PolynomialCurveFitter(initialGuess,
|
---|
93 | newMaxIter);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** {@inheritDoc} */
|
---|
97 | @Override
|
---|
98 | protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> observations) {
|
---|
99 | // Prepare least-squares problem.
|
---|
100 | final int len = observations.size();
|
---|
101 | final double[] target = new double[len];
|
---|
102 | final double[] weights = new double[len];
|
---|
103 |
|
---|
104 | int i = 0;
|
---|
105 | for (WeightedObservedPoint obs : observations) {
|
---|
106 | target[i] = obs.getY();
|
---|
107 | weights[i] = obs.getWeight();
|
---|
108 | ++i;
|
---|
109 | }
|
---|
110 |
|
---|
111 | final AbstractCurveFitter.TheoreticalValuesFunction model =
|
---|
112 | new AbstractCurveFitter.TheoreticalValuesFunction(FUNCTION, observations);
|
---|
113 |
|
---|
114 | if (initialGuess == null) {
|
---|
115 | throw new MathInternalError();
|
---|
116 | }
|
---|
117 |
|
---|
118 | // Return a new least squares problem set up to fit a polynomial curve to the
|
---|
119 | // observed points.
|
---|
120 | return new LeastSquaresBuilder().
|
---|
121 | maxEvaluations(Integer.MAX_VALUE).
|
---|
122 | maxIterations(maxIter).
|
---|
123 | start(initialGuess).
|
---|
124 | target(target).
|
---|
125 | weight(new DiagonalMatrix(weights)).
|
---|
126 | model(model.getModelFunction(), model.getModelFunctionJacobian()).
|
---|
127 | build();
|
---|
128 |
|
---|
129 | }
|
---|
130 |
|
---|
131 | }
|
---|