QtiPlot 0.9.7.3

Fit.h

Go to the documentation of this file.
00001 /***************************************************************************
00002     File                 : Fit.h
00003     Project              : QtiPlot
00004     --------------------------------------------------------------------
00005     Copyright            : (C) 2006 by Ion Vasilief
00006     Email (use @ for *)  : ion_vasilief*yahoo.fr
00007     Description          : Fit base class
00008 
00009  ***************************************************************************/
00010 
00011 /***************************************************************************
00012  *                                                                         *
00013  *  This program is free software; you can redistribute it and/or modify   *
00014  *  it under the terms of the GNU General Public License as published by   *
00015  *  the Free Software Foundation; either version 2 of the License, or      *
00016  *  (at your option) any later version.                                    *
00017  *                                                                         *
00018  *  This program is distributed in the hope that it will be useful,        *
00019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00021  *  GNU General Public License for more details.                           *
00022  *                                                                         *
00023  *   You should have received a copy of the GNU General Public License     *
00024  *   along with this program; if not, write to the Free Software           *
00025  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
00026  *   Boston, MA  02110-1301  USA                                           *
00027  *                                                                         *
00028  ***************************************************************************/
00029 #ifndef FIT_H
00030 #define FIT_H
00031 
00032 #include <QObject>
00033 
00034 #include <ApplicationWindow.h>
00035 #include "Filter.h"
00036 
00037 #include <gsl/gsl_multifit_nlin.h>
00038 #include <gsl/gsl_multimin.h>
00039 
00040 class Table;
00041 class Matrix;
00042 
00044 class Fit : public Filter
00045 {
00046     Q_OBJECT
00047 
00048     public:
00049 
00050         typedef double (*fit_function_simplex)(const gsl_vector *, void *);
00051         typedef int (*fit_function)(const gsl_vector *, void *, gsl_vector *);
00052         typedef int (*fit_function_df)(const gsl_vector *, void *, gsl_matrix *);
00053         typedef int (*fit_function_fdf)(const gsl_vector *, void *, gsl_vector *, gsl_matrix *);
00054 
00055         enum Algorithm{ScaledLevenbergMarquardt, UnscaledLevenbergMarquardt, NelderMeadSimplex};
00056         enum WeightingMethod{NoWeighting, Instrumental, Statistical, Dataset, Direct};
00057         enum FitType{BuiltIn = 0, Plugin = 1, User = 2};
00058 
00059         Fit(ApplicationWindow *parent, QwtPlotCurve *c);
00060         Fit(ApplicationWindow *parent, Graph *g = 0, const QString& name = QString());
00061         Fit(ApplicationWindow *parent, Table *t, const QString& name = QString());
00062         ~Fit();
00063 
00065         virtual void fit();
00066         virtual bool run(){fit(); return true;};
00067 
00069         bool setWeightingData(WeightingMethod w, const QString& colName = QString::null);
00070 
00071         void setDataCurve(QwtPlotCurve *curve, double start, double end);
00072         bool setDataFromTable(Table *t, const QString& xColName, const QString& yColName, int from = 1, int to = -1);
00073 
00074         QString resultFormula(){return d_result_formula;};
00075         QString formula(){return d_formula;};
00076         virtual bool setFormula(const QString&, bool = true){return true;};
00077 
00078         int numParameters(){return d_p;};
00079         QStringList parameterNames(){return d_param_names;};
00080         virtual bool setParametersList(const QStringList&){return true;};
00081         void setParameterExplanations(const QStringList& lst){d_param_explain = lst;};
00082 
00083         double initialGuess(int parIndex){return gsl_vector_get(d_param_init, parIndex);};
00084         void setInitialGuess(int parIndex, double val){gsl_vector_set(d_param_init, parIndex, val);};
00085         void setInitialGuesses(double *x_init);
00086 
00087         virtual void guessInitialValues(){};
00088 
00089         void setParameterRange(int parIndex, double left, double right);
00090         void setAlgorithm(Algorithm s){d_solver = s;};
00091 
00093         void generateFunction(bool yes, int points = 100);
00094 
00096         virtual QString legendInfo();
00097 
00099         double* results(){return d_results;};
00100 
00102         double* residuals();
00103 
00105         QwtPlotCurve* showResiduals();
00106 
00107         void showPredictionLimits(double confidenceLevel);
00108         void showConfidenceLimits(double confidenceLevel);
00110         double lcl(int parIndex, double confidenceLevel);
00112         double ucl(int parIndex, double confidenceLevel);
00113 
00115         double* errors();
00116 
00118         double chiSquare() {return chi_2;};
00119 
00121         double rSquare();
00122 
00124         double adjustedRSquare(){return d_adjusted_r_square;};
00125 
00127         double rss(){return d_rss;};
00128 
00130         double rmse(){return sqrt(d_rss/(d_n - d_p));};
00131 
00133         void scaleErrors(bool yes = true){d_scale_errors = yes;};
00134 
00135         Table* parametersTable(const QString& tableName);
00136         void writeParametersToTable(Table *t, bool append = false);
00137 
00138         Matrix* covarianceMatrix(const QString& matrixName);
00139 
00140         bool save(const QString& fileName);
00141         bool load(const QString& fileName);
00142 
00143         FitType type(){return d_fit_type;};
00144         void setType(FitType t){d_fit_type = t;};
00145 
00146         QString fileName(){return d_file_name;};
00147         void setFileName(const QString& fn){d_file_name = fn;};
00148 
00150         virtual double eval(double *, double){return 0.0;};
00151 
00152     private:
00153         void init();
00154 
00156         gsl_multimin_fminimizer * fitSimplex(gsl_multimin_function f, int &iterations, int &status);
00157 
00159         gsl_multifit_fdfsolver * fitGSL(gsl_multifit_function_fdf f, int &iterations, int &status);
00160 
00162         virtual void customizeFitResults(){};
00163 
00165         virtual bool removeDataSingularities(){return true;};
00166 
00167     protected:
00169         void initWorkspace(int par);
00171         void freeWorkspace();
00173         virtual void freeMemory();
00175         virtual FunctionCurve * insertFitFunctionCurve(const QString& name, int penWidth = 1, bool updateData = true);
00176 
00178         virtual void generateFitCurve();
00179 
00181         virtual void calculateFitCurveData(double *X, double *Y) {Q_UNUSED(X) Q_UNUSED(Y)};
00182 
00184         virtual QString logFitInfo(int iterations, int status);
00185 
00186         fit_function d_f;
00187         fit_function_df d_df;
00188         fit_function_fdf d_fdf;
00189         fit_function_simplex d_fsimplex;
00190 
00192         int d_p;
00193 
00195         gsl_vector *d_param_init;
00196 
00200         bool is_non_linear;
00201 
00203         double *d_w;
00204 
00206         QStringList d_param_names;
00207 
00209         QStringList d_param_explain;
00210 
00212         bool d_gen_function;
00213 
00215         Algorithm d_solver;
00216 
00218         QString d_formula;
00219 
00221         QString d_result_formula;
00222 
00224         gsl_matrix *covar;
00225 
00227         WeightingMethod d_weighting;
00228 
00230         QString weighting_dataset;
00231 
00233         double *d_results;
00234 
00236         double *d_errors;
00237 
00239         double *d_residuals;
00240 
00242         double chi_2;
00243 
00245         double d_rss;
00246 
00248         double d_adjusted_r_square;
00249 
00251         bool d_scale_errors;
00252 
00254         QPointer <Table> d_param_table;
00255 
00257         Matrix *d_cov_matrix;
00258 
00259         FitType d_fit_type;
00260 
00262         QString d_file_name;
00263 
00265         double *d_param_range_left;
00266 
00268         double *d_param_range_right;
00269 };
00270 
00271 #endif