View Javadoc
1   /*
2    * Copyright (C) 2015 Alberto Irurueta Carro (alberto@irurueta.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.irurueta.numerical.fitting;
17  
18  import com.irurueta.algebra.Matrix;
19  
20  import java.util.Arrays;
21  
22  /**
23   * Base class to fit a single dimension function y = f(x) by using provided
24   * data (x, y)
25   */
26  public abstract class SingleDimensionFitter extends Fitter {
27      /**
28       * Input points x where function f(x) is evaluated.
29       */
30      protected double[] x;
31  
32      /**
33       * Result of evaluation of linear single dimensional function f(x) at
34       * provided x points. This is provided as input data along with x array.
35       */
36      protected double[] y;
37  
38      /**
39       * Standard deviations of each pair of points (x,y).
40       */
41      protected double[] sig;
42  
43      /**
44       * Number of samples (x, y) in provided input data.
45       */
46      protected int ndat;
47  
48      /**
49       * Estimated parameters of single dimensional function.
50       */
51      protected double[] a;
52  
53      /**
54       * Covariance of estimated parameters of single dimensional function.
55       */
56      protected Matrix covar;
57  
58      /**
59       * Estimated chi square value of input data.
60       */
61      protected double chisq;
62  
63      /**
64       * Constructor.
65       */
66      protected SingleDimensionFitter() {
67      }
68  
69      /**
70       * Constructor.
71       *
72       * @param x   input points x where function f(x) is evaluated.
73       * @param y   result of evaluation of linear single dimensional function f(x)
74       *            at provided x points.
75       * @param sig standard deviations of each pair of points (x, y).
76       * @throws IllegalArgumentException if provided arrays don't have the same
77       *                                  length.
78       */
79      protected SingleDimensionFitter(final double[] x, final double[] y, final double[] sig) {
80          setInputData(x, y, sig);
81      }
82  
83      /**
84       * Constructor.
85       *
86       * @param x   input points x where function f(x) is evaluated.
87       * @param y   result of evaluation of linear single dimensional function f(x)
88       *            at provided x points.
89       * @param sig standard deviation of all pair of points assuming that
90       *            standard deviations are constant.
91       * @throws IllegalArgumentException if provided arrays don't have the same
92       *                                  length.
93       */
94      protected SingleDimensionFitter(final double[] x, final double[] y, final double sig) {
95          setInputData(x, y, sig);
96      }
97  
98      /**
99       * Returns input points x where function f(x) is evaluated.
100      *
101      * @return input points x.
102      */
103     public double[] getX() {
104         return x;
105     }
106 
107     /**
108      * Returns result of evaluation of linear single dimensional function f(x)
109      * at provided x points. This is provided as input data along with x array.
110      *
111      * @return sampled function evaluations.
112      */
113     public double[] getY() {
114         return y;
115     }
116 
117     /**
118      * Returns standard deviations of each pair of points (x,y).
119      *
120      * @return standard deviations of each pair of points (x,y).
121      */
122     public double[] getSig() {
123         return sig;
124     }
125 
126     /**
127      * Sets required input data to start function fitting.
128      *
129      * @param x   input points x where a linear single dimensional function f(x) =
130      *            a * f0(x) + b * f1(x) + ...
131      * @param y   result of evaluation of linear single dimensional function f(x)
132      *            at provided x points. This is provided as input data along with x array.
133      * @param sig standard deviations of each pair of points (x,y).
134      * @throws IllegalArgumentException if provided arrays don't have the same
135      *                                  size.
136      */
137     public final void setInputData(final double[] x, final double[] y, final double[] sig) {
138         if (x.length != y.length || sig.length != x.length) {
139             throw new IllegalArgumentException();
140         }
141 
142         this.x = x;
143         this.y = y;
144         this.sig = sig;
145 
146         ndat = x.length;
147     }
148 
149     /**
150      * Sets required input data to start function fitting and assuming constant
151      * standard deviation errors in input data.
152      *
153      * @param x   input points x where a linear single dimensional function f(x) =
154      *            a * f0(x) + b * f1(x) + ...
155      * @param y   result of evaluation of linear single dimensional function f(x)
156      *            at provided x points.
157      * @param sig standard deviation of all pair of points assuming that
158      *            standard deviations are constant.
159      * @throws IllegalArgumentException if provided arrays don't have the same
160      *                                  size.
161      */
162     public final void setInputData(final double[] x, final double[] y, final double sig) {
163         if (x.length != y.length) {
164             throw new IllegalArgumentException();
165         }
166 
167         this.x = x;
168         this.y = y;
169 
170         this.sig = new double[x.length];
171         Arrays.fill(this.sig, sig);
172 
173         ndat = x.length;
174     }
175 
176     /**
177      * Returns estimated parameters of linear single dimensional function.
178      *
179      * @return estimated parameters.
180      */
181     public double[] getA() {
182         return a;
183     }
184 
185     /**
186      * Returns covariance of estimated parameters of linear single dimensional
187      * function.
188      *
189      * @return covariance of estimated parameters.
190      */
191     public Matrix getCovar() {
192         return covar;
193     }
194 
195     /**
196      * Returns estimated chi square value of input data.
197      *
198      * @return estimated chi square value of input data.
199      */
200     public double getChisq() {
201         return chisq;
202     }
203 }