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 multi variate function [y1, y2, ...] = f([x1, x2, ...])
24   * by using provided data (x, y).
25   */
26  public abstract class MultiVariateFitter extends Fitter {
27      /**
28       * Input points x where a multidimensional function f(x1, x2, ...) is
29       * evaluated where each column of the matrix represents each dimension of
30       * the point and each row is related to each sample corresponding to
31       * provided y pairs of values.
32       */
33      protected Matrix x;
34  
35      /**
36       * Result of evaluation of multi variate function f(x1, x2, ...) at
37       * provided x points.
38       * Each row contains the function evaluation for a given point x, and
39       * each column contains values for each output variable f1, f2, ...
40       */
41      protected Matrix y;
42  
43      /**
44       * Standard deviations of each pair of points (x, y).
45       */
46      protected double[] sig;
47  
48      /**
49       * Number of samples (x, y) in provided input data.
50       */
51      protected int ndat;
52  
53      /**
54       * Estimated parameters of linear single dimensional function.
55       */
56      protected double[] a;
57  
58      /**
59       * Covariance of estimated parameters of linear single dimensional function.
60       */
61      protected Matrix covar;
62  
63      /**
64       * Estimated chi square value of input data.
65       */
66      protected double chisq;
67  
68      /**
69       * Constructor.
70       */
71      protected MultiVariateFitter() {
72      }
73  
74      /**
75       * Constructor.
76       *
77       * @param x   input points x where a multi variate function f(x1, x2, ...) is
78       *            evaluated.
79       * @param y   result of evaluation of multi variate function f(x1, x2, ...) at
80       *            provided x points.
81       * @param sig standard deviations of each pair of points (x, y).
82       * @throws IllegalArgumentException if provided matrix rows and arrays don't
83       *                                  have the same length.
84       */
85      protected MultiVariateFitter(final Matrix x, final Matrix y, final double[] sig) {
86          setInputData(x, y, sig);
87      }
88  
89      /**
90       * Constructor.
91       *
92       * @param x   input points x where a multi variate function f(x1, x2, ...) is
93       *            evaluated.
94       * @param y   result of evaluation of multi variate function f(x1, x2, ...) at
95       *            provided x points.
96       * @param sig standard deviation of all pair of points assuming that
97       *            standard deviations are constant.
98       * @throws IllegalArgumentException if provided matrix rows and arrays don't
99       *                                  have the same length.
100      */
101     protected MultiVariateFitter(final Matrix x, final Matrix y, final double sig) {
102         setInputData(x, y, sig);
103     }
104 
105     /**
106      * Returns input points x where a multi variate function f(x1, x2, ...)
107      * is evaluated and where each column of the matrix represents
108      * each dimension of the point and each row is related to each sample
109      * corresponding to provided y pairs of values.
110      *
111      * @return input point x.
112      */
113     public Matrix getX() {
114         return x;
115     }
116 
117     /**
118      * Returns result of evaluation of multi variate function f(x) at provided
119      * x points. This is provided as input data along with x array.
120      *
121      * @return result of evaluation.
122      */
123     public Matrix getY() {
124         return y;
125     }
126 
127     /**
128      * Returns standard deviations of each pair of points (x,y).
129      *
130      * @return standard deviations of each pair of points (x,y).
131      */
132     public double[] getSig() {
133         return sig;
134     }
135 
136     /**
137      * Sets required input data to start function fitting.
138      *
139      * @param x   input points x where a multi variate function f(x1, x2, ...)
140      *            is evaluated and where each column of the matrix represents each
141      *            dimension of the point and each row is related to each sample
142      *            corresponding to provided y pairs of values.
143      * @param y   result of evaluation of multi variate function
144      *            f(x1, x2, ...) at provided x points. This is provided as input data along
145      *            with x array.
146      * @param sig standard deviations of each pair of points (x,y).
147      * @throws IllegalArgumentException if provided arrays don't have the same
148      *                                  size.
149      */
150     public final void setInputData(final Matrix x, final Matrix y, final double[] sig) {
151         if (x.getRows() != y.getRows() || sig.length != y.getRows()) {
152             throw new IllegalArgumentException();
153         }
154 
155         this.x = x;
156         this.y = y;
157         this.sig = sig;
158 
159         ndat = sig.length;
160     }
161 
162     /**
163      * Sets required input data to start function fitting and assuming constant
164      * standard deviation errors in input data.
165      *
166      * @param x   input points x where a multi variate function f(x1, x2, ...)
167      *            is evaluated and where each column of the matrix represents each
168      *            dimension of the point and each row is related to each sample
169      *            corresponding to provided y pairs of values.
170      * @param y   result of evaluation of multi variate function
171      *            f(x1, x2, ...) at provided x points. This is provided as input data along
172      *            with x array.
173      * @param sig standard deviations of each pair of points (x,y).
174      * @throws IllegalArgumentException if provided arrays don't have the same
175      *                                  size.
176      */
177     public final void setInputData(final Matrix x, final Matrix y, final double sig) {
178         if (x.getRows() != y.getRows()) {
179             throw new IllegalArgumentException();
180         }
181 
182         this.x = x;
183         this.y = y;
184 
185         ndat = y.getRows();
186         this.sig = new double[ndat];
187         Arrays.fill(this.sig, sig);
188     }
189 
190     /**
191      * Returns estimated parameters of linear single dimensional function.
192      *
193      * @return estimated parameters.
194      */
195     public double[] getA() {
196         return a;
197     }
198 
199     /**
200      * Returns covariance of estimated parameters of linear single dimensional
201      * function.
202      *
203      * @return covariance of estimated parameters.
204      */
205     public Matrix getCovar() {
206         return covar;
207     }
208 
209     /**
210      * Returns estimated chi square value of input data.
211      *
212      * @return estimated chi square value of input data.
213      */
214     public double getChisq() {
215         return chisq;
216     }
217 }