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