QtiPlot  0.9.8.2
Fit.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : Fit.h
3  Project : QtiPlot
4  --------------------------------------------------------------------
5  Copyright : (C) 2006 by Ion Vasilief
6  Email (use @ for *) : ion_vasilief*yahoo.fr
7  Description : Fit base class
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  * This program is distributed in the hope that it will be useful, *
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21  * GNU General Public License for more details. *
22  * *
23  * You should have received a copy of the GNU General Public License *
24  * along with this program; if not, write to the Free Software *
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26  * Boston, MA 02110-1301 USA *
27  * *
28  ***************************************************************************/
29 #ifndef FIT_H
30 #define FIT_H
31 
32 #include <QObject>
33 
34 #include <ApplicationWindow.h>
35 #include "Filter.h"
36 
37 #include <gsl/gsl_multifit_nlin.h>
38 #include <gsl/gsl_multimin.h>
39 
40 class Table;
41 class Matrix;
42 
44 class Fit : public Filter
45 {
46  Q_OBJECT
47 
48  public:
49 
50  typedef double (*fit_function_simplex)(const gsl_vector *, void *);
51  typedef int (*fit_function)(const gsl_vector *, void *, gsl_vector *);
52  typedef int (*fit_function_df)(const gsl_vector *, void *, gsl_matrix *);
53  typedef int (*fit_function_fdf)(const gsl_vector *, void *, gsl_vector *, gsl_matrix *);
54 
57  enum FitType{BuiltIn = 0, Plugin = 1, User = 2};
58 
59  Fit(ApplicationWindow *parent, QwtPlotCurve *c);
60  Fit(ApplicationWindow *parent, Graph *g = 0, const QString& name = QString());
61  Fit(ApplicationWindow *parent, Table *t, const QString& name = QString());
62  ~Fit();
63 
65  virtual void fit();
66  virtual bool run(){fit(); return true;};
67 
69  bool setWeightingData(WeightingMethod w, const QString& colName = QString::null);
70 
71  void setDataCurve(QwtPlotCurve *curve, double start, double end);
72  bool setDataFromTable(Table *t, const QString& xColName, const QString& yColName, int from = 1, int to = -1, bool sort = false);
73 
74  QString resultFormula(){return d_result_formula;};
75  QString formula(){return d_formula;};
76  virtual bool setFormula(const QString&, bool = true){return true;};
77 
78  int numParameters(){return d_p;};
79  QStringList parameterNames(){return d_param_names;};
80  virtual bool setParametersList(const QStringList&){return true;};
81  void setParameterExplanations(const QStringList& lst){d_param_explain = lst;};
82 
83  double initialGuess(int parIndex){return gsl_vector_get(d_param_init, parIndex);};
84  void setInitialGuess(int parIndex, double val){gsl_vector_set(d_param_init, parIndex, val);};
85  void setInitialGuesses(double *x_init);
86 
87  virtual void guessInitialValues(){};
88 
89  void setParameterRange(int parIndex, double left, double right);
91 
93  void generateFunction(bool yes, int points = 100);
94 
96  virtual QString legendInfo();
97 
99  double* results(){return d_results;};
100 
102  double* residuals();
103 
105  QwtPlotCurve* showResiduals();
106 
107  void showPredictionLimits(double confidenceLevel);
108  void showConfidenceLimits(double confidenceLevel);
110  double lcl(int parIndex, double confidenceLevel);
112  double ucl(int parIndex, double confidenceLevel);
113 
115  double* errors();
116 
118  double chiSquare() {return chi_2;};
119 
121  double rSquare();
122 
125 
127  double rss(){return d_rss;};
128 
130  double rmse(){return sqrt(d_rss/(d_n - d_p));};
131 
133  void scaleErrors(bool yes = true){d_scale_errors = yes;};
134 
135  Table* parametersTable(const QString& tableName);
136  void writeParametersToTable(Table *t, bool append = false);
137 
138  Matrix* covarianceMatrix(const QString& matrixName);
139 
140  bool save(const QString& fileName);
141  bool load(const QString& fileName);
142 
143  FitType type(){return d_fit_type;};
144  void setType(FitType t){d_fit_type = t;};
145 
146  QString fileName(){return d_file_name;};
147  void setFileName(const QString& fn){d_file_name = fn;};
148 
150  virtual double eval(double *, double){return 0.0;};
151 
152  private:
153  void init();
154 
156  gsl_multimin_fminimizer * fitSimplex(gsl_multimin_function f, int &iterations, int &status);
157 
159  gsl_multifit_fdfsolver * fitGSL(gsl_multifit_function_fdf f, int &iterations, int &status);
160 
162  virtual void customizeFitResults(){};
163 
165  virtual bool removeDataSingularities(){return true;};
166 
167  protected:
169  void initWorkspace(int par);
171  void freeWorkspace();
173  virtual void freeMemory();
175  virtual FunctionCurve * insertFitFunctionCurve(const QString& name, int penWidth = 1, bool updateData = true);
176 
178  virtual void generateFitCurve();
179 
181  virtual void calculateFitCurveData(double *X, double *Y) {Q_UNUSED(X) Q_UNUSED(Y)};
182 
184  virtual QString logFitInfo(int iterations, int status);
185 
190 
192  int d_p;
193 
195  gsl_vector *d_param_init;
196 
201 
203  double *d_w;
204 
206  QStringList d_param_names;
207 
209  QStringList d_param_explain;
210 
213 
216 
218  QString d_formula;
219 
222 
224  gsl_matrix *covar;
225 
228 
231 
233  double *d_results;
234 
236  double *d_errors;
237 
239  double *d_residuals;
240 
242  double chi_2;
243 
245  double d_rss;
246 
249 
252 
254  QPointer <Table> d_param_table;
255 
258 
260 
262  QString d_file_name;
263 
266 
269 };
270 
271 #endif
Fit(ApplicationWindow *parent, QwtPlotCurve *c)
Definition: Fit.cpp:49
double * residuals()
Returns a vector with the fit residuals.
Definition: Fit.cpp:634
Table * parametersTable(const QString &tableName)
Definition: Fit.cpp:550
void showConfidenceLimits(double confidenceLevel)
Definition: Fit.cpp:685
double chi_2
The sum of squares of the residuals from the best-fit line.
Definition: Fit.h:242
Definition: Fit.h:57
~Fit()
Definition: Fit.cpp:1180
bool setDataFromTable(Table *t, const QString &xColName, const QString &yColName, int from=1, int to=-1, bool sort=false)
Definition: Fit.cpp:228
virtual QString logFitInfo(int iterations, int status)
Output string added to the result log.
Definition: Fit.cpp:292
double * d_results
Stores the result parameters.
Definition: Fit.h:233
double * d_residuals
Stores fit residuals.
Definition: Fit.h:239
gsl_matrix * covar
Covariance matrix.
Definition: Fit.h:224
void setAlgorithm(Algorithm s)
Definition: Fit.h:90
Definition: Fit.h:56
void setParameterExplanations(const QStringList &lst)
Definition: Fit.h:81
fit_function_simplex d_fsimplex
Definition: Fit.h:189
A 2D-plotting widget.
Definition: Graph.h:123
virtual bool setFormula(const QString &, bool=true)
Definition: Fit.h:76
gsl_multimin_fminimizer * fitSimplex(gsl_multimin_function f, int &iterations, int &status)
Pointer to the GSL multifit minimizer (for simplex algorithm)
Definition: Fit.cpp:164
Definition: Fit.h:55
virtual void fit()
Actually does the fit. Should be reimplemented in derived classes.
Definition: Fit.cpp:903
Matrix * covarianceMatrix(const QString &matrixName)
Definition: Fit.cpp:602
void setInitialGuesses(double *x_init)
Definition: Fit.cpp:279
QString fileName()
Definition: Fit.h:146
void setParameterRange(int parIndex, double left, double right)
Definition: Fit.cpp:1102
double ucl(int parIndex, double confidenceLevel)
Upper Confidence Limit.
Definition: Fit.cpp:795
virtual void generateFitCurve()
Adds the result curve to the plot.
Definition: Fit.cpp:979
void writeParametersToTable(Table *t, bool append=false)
Definition: Fit.cpp:568
virtual bool setParametersList(const QStringList &)
Definition: Fit.h:80
Abstract base class for data analysis operations.
Definition: Filter.h:43
double initialGuess(int parIndex)
Definition: Fit.h:83
Definition: Fit.h:55
double adjustedRSquare()
Returns adjusted R^2.
Definition: Fit.h:124
QString weighting_dataset
The name of the weighting dataset.
Definition: Fit.h:230
double lcl(int parIndex, double confidenceLevel)
Lower Confidence Limit.
Definition: Fit.cpp:786
void setFileName(const QString &fn)
Definition: Fit.h:147
void showPredictionLimits(double confidenceLevel)
Definition: Fit.cpp:804
Algorithm d_solver
Algorithm type.
Definition: Fit.h:215
virtual bool removeDataSingularities()
Removes any data singularities before fitting.
Definition: Fit.h:165
QStringList d_param_names
Names of the fit parameters.
Definition: Fit.h:206
QString d_result_formula
The result fit formula, where the fit parameters are replaced with the calculated values...
Definition: Fit.h:221
bool setWeightingData(WeightingMethod w, const QString &colName=QString::null)
Sets the data set to be used for weighting.
Definition: Fit.cpp:433
QwtPlotCurve * showResiduals()
Plot residuals and display data values in a column.
Definition: Fit.cpp:647
QStringList parameterNames()
Definition: Fit.h:79
void initWorkspace(int par)
Allocates the memory for the fit workspace.
Definition: Fit.cpp:1111
double * d_param_range_right
Stores the right limits of the research interval for the result parameters.
Definition: Fit.h:268
double * d_w
weighting data set used for the fit
Definition: Fit.h:203
FitType
Definition: Fit.h:57
double d_adjusted_r_square
Adjusted R^2.
Definition: Fit.h:248
Fit base class.
Definition: Fit.h:44
QString formula()
Definition: Fit.h:75
double chiSquare()
Returns the sum of squares of the residuals from the best-fit line.
Definition: Fit.h:118
Definition: Fit.h:56
int d_n
Size of the data arrays.
Definition: Filter.h:159
virtual bool run()
Actually does the job. Should be reimplemented in derived classes.
Definition: Fit.h:66
bool is_non_linear
Tells whether the fitter uses non-linear/simplex fitting with an initial parameters set...
Definition: Fit.h:200
bool load(const QString &fileName)
Definition: Fit.cpp:1078
double rmse()
Returns the Root Mean Squared Error.
Definition: Fit.h:130
double * errors()
Returns a vector with the standard deviations of the results.
Definition: Fit.cpp:619
void setType(FitType t)
Definition: Fit.h:144
double * d_param_range_left
Stores the left limits of the research interval for the result parameters.
Definition: Fit.h:265
QtiPlot's main window.
Definition: ApplicationWindow.h:133
fit_function_fdf d_fdf
Definition: Fit.h:188
Definition: FunctionCurve.h:37
QString d_file_name
Path of the XML file where the user stores the fit model.
Definition: Fit.h:262
Definition: Fit.h:56
Matrix worksheet class.
Definition: Matrix.h:57
double d_rss
Residual sum of squares.
Definition: Fit.h:245
double(* fit_function_simplex)(const gsl_vector *, void *)
Definition: Fit.h:50
FitType d_fit_type
Definition: Fit.h:259
WeightingMethod d_weighting
The kind of weighting to be performed on the data.
Definition: Fit.h:227
int(* fit_function)(const gsl_vector *, void *, gsl_vector *)
Definition: Fit.h:51
gsl_multifit_fdfsolver * fitGSL(gsl_multifit_function_fdf f, int &iterations, int &status)
Pointer to the GSL multifit solver.
Definition: Fit.cpp:106
void setDataCurve(QwtPlotCurve *curve, double start, double end)
Definition: Fit.cpp:247
void freeWorkspace()
Frees the memory allocated for the fit workspace.
Definition: Fit.cpp:1133
double rSquare()
Returns R^2.
Definition: Fit.cpp:379
QString resultFormula()
Definition: Fit.h:74
double * d_errors
Stores standard deviations of the result parameters.
Definition: Fit.h:236
Definition: Fit.h:57
void init()
Definition: Fit.cpp:67
Definition: Fit.h:57
Matrix * d_cov_matrix
Matrix window used for the output of covariance matrix.
Definition: Fit.h:257
int numParameters()
Definition: Fit.h:78
bool d_gen_function
Specifies weather the result curve is a FunctionCurve or a normal curve with the same x values as the...
Definition: Fit.h:212
void scaleErrors(bool yes=true)
Specifies wheather the errors must be scaled with sqrt(chi_2/dof)
Definition: Fit.h:133
virtual void guessInitialValues()
Definition: Fit.h:87
void generateFunction(bool yes, int points=100)
Specifies weather the result of the fit is a function curve.
Definition: Fit.cpp:285
int d_p
Number of fit parameters.
Definition: Fit.h:192
fit_function d_f
Definition: Fit.h:186
virtual void calculateFitCurveData(double *X, double *Y)
Calculates the data for the output fit curve and store itin the X an Y vectors.
Definition: Fit.h:181
double * results()
Returns a vector with the fit results.
Definition: Fit.h:99
QPointer< Table > d_param_table
Table window used for the output of fit parameters.
Definition: Fit.h:254
virtual FunctionCurve * insertFitFunctionCurve(const QString &name, int penWidth=1, bool updateData=true)
Adds the result curve as a FunctionCurve to the plot, if d_gen_function = true.
Definition: Fit.cpp:1020
QString d_formula
The fit formula given on input.
Definition: Fit.h:218
MDI window providing a spreadsheet table with column logic.
Definition: Table.h:57
virtual double eval(double *, double)
Calculates the data for the output fit curve.
Definition: Fit.h:150
virtual void freeMemory()
Frees all the arrays with size d_n. Used when changing the source data sets.
Definition: Fit.cpp:1166
bool d_scale_errors
Specifies wheather the errors must be scaled with sqrt(chi_2/dof)
Definition: Fit.h:251
fit_function_df d_df
Definition: Fit.h:187
WeightingMethod
Definition: Fit.h:56
double rss()
Returns the Residual Sum of Squares.
Definition: Fit.h:127
virtual QString legendInfo()
Output string added to the plot as a new legend.
Definition: Fit.cpp:402
Algorithm
Definition: Fit.h:55
QStringList d_param_explain
Stores a list of short explanations for the significance of the fit parameters.
Definition: Fit.h:209
gsl_vector * d_param_init
Initial guesses for the fit parameters.
Definition: Fit.h:195
void setInitialGuess(int parIndex, double val)
Definition: Fit.h:84
FitType type()
Definition: Fit.h:143
bool save(const QString &fileName)
Definition: Fit.cpp:1043
Definition: Fit.h:56
int(* fit_function_fdf)(const gsl_vector *, void *, gsl_vector *, gsl_matrix *)
Definition: Fit.h:53
Definition: Fit.h:56
virtual void customizeFitResults()
Customs and stores the fit results according to the derived class specifications. Used by exponential...
Definition: Fit.h:162
int(* fit_function_df)(const gsl_vector *, void *, gsl_matrix *)
Definition: Fit.h:52