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};
00057         enum FitType{BuiltIn = 0, Plugin = 1, User = 2};
00058 
00059         Fit(ApplicationWindow *parent, Graph *g = 0, const QString& name = QString());
00060         Fit(ApplicationWindow *parent, Table *t, const QString& name = QString());
00061         ~Fit();
00062 
00064         virtual void fit();
00065         virtual bool run(){fit(); return true;};
00066 
00068         bool setWeightingData(WeightingMethod w, const QString& colName = QString::null);
00069 
00070         void setDataCurve(int curve, double start, double end);
00071         bool setDataFromTable(Table *t, const QString& xColName, const QString& yColName, int from = 1, int to = -1);
00072 
00073         QString resultFormula(){return d_result_formula;};
00074         QString formula(){return d_formula;};
00075         virtual void setFormula(const QString&){};
00076 
00077         int numParameters(){return d_p;};
00078         QStringList parameterNames(){return d_param_names;};
00079         virtual void setParametersList(const QStringList&){};
00080         void setParameterExplanations(const QStringList& lst){d_param_explain = lst;};
00081 
00082         double initialGuess(int parIndex){return gsl_vector_get(d_param_init, parIndex);};
00083         void setInitialGuess(int parIndex, double val){gsl_vector_set(d_param_init, parIndex, val);};
00084         void setInitialGuesses(double *x_init);
00085 
00086         virtual void guessInitialValues(){};
00087 
00088         void setParameterRange(int parIndex, double left, double right);
00089         void setAlgorithm(Algorithm s){d_solver = s;};
00090 
00092         void generateFunction(bool yes, int points = 100);
00093 
00095         virtual QString legendInfo();
00096 
00098         double* results(){return d_results;};
00099 
00101         double* residuals();
00102 
00104         QwtPlotCurve* showResiduals();
00105 
00106         void showPredictionLimits(double confidenceLevel);
00107         void showConfidenceLimits(double confidenceLevel);
00109         double lcl(int parIndex, double confidenceLevel);
00111         double ucl(int parIndex, double confidenceLevel);
00112 
00114         double* errors();
00115 
00117         double chiSquare() {return chi_2;};
00118 
00120         double rSquare();
00121 
00123         double adjustedRSquare(){return d_adjusted_r_square;};
00124 
00126         double rss(){return d_rss;};
00127 
00129         double rmse(){return sqrt(d_rss/(d_n - d_p));};
00130 
00132         void scaleErrors(bool yes = true){d_scale_errors = yes;};
00133 
00134         Table* parametersTable(const QString& tableName);
00135         void writeParametersToTable(Table *t, bool append = false);
00136 
00137         Matrix* covarianceMatrix(const QString& matrixName);
00138 
00139         bool save(const QString& fileName);
00140         bool load(const QString& fileName);
00141 
00142         FitType type(){return d_fit_type;};
00143         void setType(FitType t){d_fit_type = t;};
00144 
00145         QString fileName(){return d_file_name;};
00146         void setFileName(const QString& fn){d_file_name = fn;};
00147 
00149         void freeMemory();
00150 
00152         virtual double eval(double *, double){return 0.0;};
00153 
00154     private:
00155         void init();
00156 
00158         gsl_multimin_fminimizer * fitSimplex(gsl_multimin_function f, int &iterations, int &status);
00159 
00161         gsl_multifit_fdfsolver * fitGSL(gsl_multifit_function_fdf f, int &iterations, int &status);
00162 
00164         virtual void customizeFitResults(){};
00165 
00166     protected:
00168         void initWorkspace(int par);
00170         void freeWorkspace();
00172         virtual FunctionCurve * insertFitFunctionCurve(const QString& name, double *x, double *y, int penWidth = 1);
00173 
00175         virtual void generateFitCurve();
00176 
00178         virtual void calculateFitCurveData(double *X, double *Y) {Q_UNUSED(X) Q_UNUSED(Y)};
00179 
00181         virtual QString logFitInfo(int iterations, int status);
00182 
00183         fit_function d_f;
00184         fit_function_df d_df;
00185         fit_function_fdf d_fdf;
00186         fit_function_simplex d_fsimplex;
00187 
00189         int d_p;
00190 
00192         gsl_vector *d_param_init;
00193 
00197         bool is_non_linear;
00198 
00200         double *d_w;
00201 
00203         QStringList d_param_names;
00204 
00206         QStringList d_param_explain;
00207 
00209         bool d_gen_function;
00210 
00212         Algorithm d_solver;
00213 
00215         QString d_formula;
00216 
00218         QString d_result_formula;
00219 
00221         gsl_matrix *covar;
00222 
00224         WeightingMethod d_weighting;
00225 
00227         QString weighting_dataset;
00228 
00230         double *d_results;
00231 
00233         double *d_errors;
00234 
00236         double *d_residuals;
00237 
00239         double chi_2;
00240 
00242         double d_rss;
00243 
00245         double d_adjusted_r_square;
00246 
00248         bool d_scale_errors;
00249 
00251         Table *d_param_table;
00252 
00254         Matrix *d_cov_matrix;
00255 
00256         FitType d_fit_type;
00257 
00259         QString d_file_name;
00260 
00262         double *d_param_range_left;
00263 
00265         double *d_param_range_right;
00266 };
00267 
00268 #endif

Generated on Tue Nov 3 10:48:49 2009 for QtiPlot by  doxygen 1.6.1