Fit.h
Go to the documentation of this file.
/***************************************************************************
File : Fit.h
Project : QtiPlot
--------------------------------------------------------------------
Copyright : (C) 2006 by Ion Vasilief
Email (use @ for *) : ion_vasilief*yahoo.fr
Description : Fit base class
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
***************************************************************************/
#ifndef FIT_H
#define FIT_H
#include <QObject>
#include <ApplicationWindow.h>
#include "Filter.h"
#include <gsl/gsl_multifit_nlin.h>
#include <gsl/gsl_multimin.h>
class Table;
class Matrix;
class Fit : public Filter
{
Q_OBJECT
public:
typedef double (*fit_function_simplex)(const gsl_vector *, void *);
typedef int (*fit_function)(const gsl_vector *, void *, gsl_vector *);
typedef int (*fit_function_df)(const gsl_vector *, void *, gsl_matrix *);
typedef int (*fit_function_fdf)(const gsl_vector *, void *, gsl_vector *, gsl_matrix *);
enum Algorithm{ScaledLevenbergMarquardt, UnscaledLevenbergMarquardt, NelderMeadSimplex};
enum WeightingMethod{NoWeighting, Instrumental, Statistical, Dataset, Direct};
enum FitType{BuiltIn = 0, Plugin = 1, User = 2};
Fit(ApplicationWindow *parent, QwtPlotCurve *c);
Fit(ApplicationWindow *parent, Graph *g = 0, const QString& name = QString());
Fit(ApplicationWindow *parent, Table *t, const QString& name = QString());
~Fit();
virtual void fit();
virtual bool run(){fit(); return true;};
bool setWeightingData(WeightingMethod w, const QString& colName = QString::null);
void setDataCurve(QwtPlotCurve *curve, double start, double end);
bool setDataFromTable(Table *t, const QString& xColName, const QString& yColName, int from = 1, int to = -1, bool sort = false);
QString resultFormula(){return d_result_formula;};
QString formula(){return d_formula;};
virtual bool setFormula(const QString&, bool = true){return true;};
int numParameters(){return d_p;};
QStringList parameterNames(){return d_param_names;};
virtual bool setParametersList(const QStringList&){return true;};
void setParameterExplanations(const QStringList& lst){d_param_explain = lst;};
double initialGuess(int parIndex){return gsl_vector_get(d_param_init, parIndex);};
void setInitialGuess(int parIndex, double val){gsl_vector_set(d_param_init, parIndex, val);};
void setInitialGuesses(double *x_init);
virtual void guessInitialValues(){};
void setParameterRange(int parIndex, double left, double right);
void setAlgorithm(Algorithm s){d_solver = s;};
void generateFunction(bool yes, int points = 100);
virtual QString legendInfo();
double* results(){return d_results;};
double* residuals();
QwtPlotCurve* showResiduals();
void showPredictionLimits(double confidenceLevel);
void showConfidenceLimits(double confidenceLevel);
double lcl(int parIndex, double confidenceLevel);
double ucl(int parIndex, double confidenceLevel);
double* errors();
double chiSquare() {return chi_2;};
double rSquare();
double adjustedRSquare(){return d_adjusted_r_square;};
double rss(){return d_rss;};
double rmse(){return sqrt(d_rss/(d_n - d_p));};
void scaleErrors(bool yes = true){d_scale_errors = yes;};
Table* parametersTable(const QString& tableName);
void writeParametersToTable(Table *t, bool append = false);
Matrix* covarianceMatrix(const QString& matrixName);
bool save(const QString& fileName);
bool load(const QString& fileName);
FitType type(){return d_fit_type;};
void setType(FitType t){d_fit_type = t;};
QString fileName(){return d_file_name;};
void setFileName(const QString& fn){d_file_name = fn;};
virtual double eval(double *, double){return 0.0;};
private:
void init();
gsl_multimin_fminimizer * fitSimplex(gsl_multimin_function f, int &iterations, int &status);
gsl_multifit_fdfsolver * fitGSL(gsl_multifit_function_fdf f, int &iterations, int &status);
virtual void customizeFitResults(){};
virtual bool removeDataSingularities(){return true;};
protected:
void initWorkspace(int par);
void freeWorkspace();
virtual void freeMemory();
virtual FunctionCurve * insertFitFunctionCurve(const QString& name, int penWidth = 1, bool updateData = true);
virtual void generateFitCurve();
virtual void calculateFitCurveData(double *X, double *Y) {Q_UNUSED(X) Q_UNUSED(Y)};
virtual QString logFitInfo(int iterations, int status);
fit_function d_f;
fit_function_df d_df;
fit_function_fdf d_fdf;
fit_function_simplex d_fsimplex;
int d_p;
gsl_vector *d_param_init;
bool is_non_linear;
double *d_w;
QStringList d_param_names;
QStringList d_param_explain;
bool d_gen_function;
Algorithm d_solver;
QString d_formula;
QString d_result_formula;
gsl_matrix *covar;
WeightingMethod d_weighting;
QString weighting_dataset;
double *d_results;
double *d_errors;
double *d_residuals;
double chi_2;
double d_rss;
double d_adjusted_r_square;
bool d_scale_errors;
QPointer <Table> d_param_table;
Matrix *d_cov_matrix;
FitType d_fit_type;
QString d_file_name;
double *d_param_range_left;
double *d_param_range_right;
};
#endif
Fit::Fit
Fit(ApplicationWindow *parent, QwtPlotCurve *c)
Definition: Fit.cpp:49
Fit::residuals
double * residuals()
Returns a vector with the fit residuals.
Definition: Fit.cpp:634
Fit::parametersTable
Table * parametersTable(const QString &tableName)
Definition: Fit.cpp:550
Fit::showConfidenceLimits
void showConfidenceLimits(double confidenceLevel)
Definition: Fit.cpp:685
Fit::chi_2
double chi_2
The sum of squares of the residuals from the best-fit line.
Definition: Fit.h:242
Fit::BuiltIn
Definition: Fit.h:57
Fit::~Fit
~Fit()
Definition: Fit.cpp:1180
Fit::setDataFromTable
bool setDataFromTable(Table *t, const QString &xColName, const QString &yColName, int from=1, int to=-1, bool sort=false)
Definition: Fit.cpp:228
Fit::logFitInfo
virtual QString logFitInfo(int iterations, int status)
Output string added to the result log.
Definition: Fit.cpp:292
Fit::d_results
double * d_results
Stores the result parameters.
Definition: Fit.h:233
Fit::fit_function
int(* fit_function)(const gsl_vector *, void *, gsl_vector *)
Definition: Fit.h:51
Fit::d_residuals
double * d_residuals
Stores fit residuals.
Definition: Fit.h:239
Fit::covar
gsl_matrix * covar
Covariance matrix.
Definition: Fit.h:224
Fit::setAlgorithm
void setAlgorithm(Algorithm s)
Definition: Fit.h:90
Fit::Statistical
Definition: Fit.h:56
Fit::setParameterExplanations
void setParameterExplanations(const QStringList &lst)
Definition: Fit.h:81
Fit::d_fsimplex
fit_function_simplex d_fsimplex
Definition: Fit.h:189
Graph
A 2D-plotting widget.
Definition: Graph.h:123
Fit::fit_function_fdf
int(* fit_function_fdf)(const gsl_vector *, void *, gsl_vector *, gsl_matrix *)
Definition: Fit.h:53
Fit::setFormula
virtual bool setFormula(const QString &, bool=true)
Definition: Fit.h:76
Fit::fitSimplex
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
Fit::NelderMeadSimplex
Definition: Fit.h:55
Fit::fit
virtual void fit()
Actually does the fit. Should be reimplemented in derived classes.
Definition: Fit.cpp:903
Fit::covarianceMatrix
Matrix * covarianceMatrix(const QString &matrixName)
Definition: Fit.cpp:602
Fit::setInitialGuesses
void setInitialGuesses(double *x_init)
Definition: Fit.cpp:279
Fit::fileName
QString fileName()
Definition: Fit.h:146
Fit::setParameterRange
void setParameterRange(int parIndex, double left, double right)
Definition: Fit.cpp:1102
Fit::ucl
double ucl(int parIndex, double confidenceLevel)
Upper Confidence Limit.
Definition: Fit.cpp:795
Fit::fit_function_simplex
double(* fit_function_simplex)(const gsl_vector *, void *)
Definition: Fit.h:50
Fit::generateFitCurve
virtual void generateFitCurve()
Adds the result curve to the plot.
Definition: Fit.cpp:979
Fit::writeParametersToTable
void writeParametersToTable(Table *t, bool append=false)
Definition: Fit.cpp:568
Fit::setParametersList
virtual bool setParametersList(const QStringList &)
Definition: Fit.h:80
Filter
Abstract base class for data analysis operations.
Definition: Filter.h:43
Filter.h
Fit::initialGuess
double initialGuess(int parIndex)
Definition: Fit.h:83
Fit::ScaledLevenbergMarquardt
Definition: Fit.h:55
Fit::adjustedRSquare
double adjustedRSquare()
Returns adjusted R^2.
Definition: Fit.h:124
Fit::weighting_dataset
QString weighting_dataset
The name of the weighting dataset.
Definition: Fit.h:230
Fit::lcl
double lcl(int parIndex, double confidenceLevel)
Lower Confidence Limit.
Definition: Fit.cpp:786
Fit::setFileName
void setFileName(const QString &fn)
Definition: Fit.h:147
Fit::showPredictionLimits
void showPredictionLimits(double confidenceLevel)
Definition: Fit.cpp:804
Fit::d_solver
Algorithm d_solver
Algorithm type.
Definition: Fit.h:215
Fit::removeDataSingularities
virtual bool removeDataSingularities()
Removes any data singularities before fitting.
Definition: Fit.h:165
Fit::d_param_names
QStringList d_param_names
Names of the fit parameters.
Definition: Fit.h:206
Fit::d_result_formula
QString d_result_formula
The result fit formula, where the fit parameters are replaced with the calculated values...
Definition: Fit.h:221
Fit::setWeightingData
bool setWeightingData(WeightingMethod w, const QString &colName=QString::null)
Sets the data set to be used for weighting.
Definition: Fit.cpp:433
Fit::showResiduals
QwtPlotCurve * showResiduals()
Plot residuals and display data values in a column.
Definition: Fit.cpp:647
Fit::parameterNames
QStringList parameterNames()
Definition: Fit.h:79
Fit::initWorkspace
void initWorkspace(int par)
Allocates the memory for the fit workspace.
Definition: Fit.cpp:1111
Fit::d_param_range_right
double * d_param_range_right
Stores the right limits of the research interval for the result parameters.
Definition: Fit.h:268
Fit::d_w
double * d_w
weighting data set used for the fit
Definition: Fit.h:203
Fit::FitType
FitType
Definition: Fit.h:57
Fit::d_adjusted_r_square
double d_adjusted_r_square
Adjusted R^2.
Definition: Fit.h:248
Fit
Fit base class.
Definition: Fit.h:44
Fit::formula
QString formula()
Definition: Fit.h:75
Fit::chiSquare
double chiSquare()
Returns the sum of squares of the residuals from the best-fit line.
Definition: Fit.h:118
Fit::Direct
Definition: Fit.h:56
Filter::d_n
int d_n
Size of the data arrays.
Definition: Filter.h:159
Fit::run
virtual bool run()
Actually does the job. Should be reimplemented in derived classes.
Definition: Fit.h:66
Fit::is_non_linear
bool is_non_linear
Tells whether the fitter uses non-linear/simplex fitting with an initial parameters set...
Definition: Fit.h:200
Fit::load
bool load(const QString &fileName)
Definition: Fit.cpp:1078
Fit::rmse
double rmse()
Returns the Root Mean Squared Error.
Definition: Fit.h:130
Fit::errors
double * errors()
Returns a vector with the standard deviations of the results.
Definition: Fit.cpp:619
Fit::setType
void setType(FitType t)
Definition: Fit.h:144
Fit::d_param_range_left
double * d_param_range_left
Stores the left limits of the research interval for the result parameters.
Definition: Fit.h:265
ApplicationWindow
QtiPlot&#39;s main window.
Definition: ApplicationWindow.h:133
Fit::d_fdf
fit_function_fdf d_fdf
Definition: Fit.h:188
FunctionCurve
Definition: FunctionCurve.h:37
Fit::d_file_name
QString d_file_name
Path of the XML file where the user stores the fit model.
Definition: Fit.h:262
Fit::Dataset
Definition: Fit.h:56
Matrix
Matrix worksheet class.
Definition: Matrix.h:57
Fit::d_rss
double d_rss
Residual sum of squares.
Definition: Fit.h:245
Fit::d_fit_type
FitType d_fit_type
Definition: Fit.h:259
Fit::d_weighting
WeightingMethod d_weighting
The kind of weighting to be performed on the data.
Definition: Fit.h:227
Fit::fitGSL
gsl_multifit_fdfsolver * fitGSL(gsl_multifit_function_fdf f, int &iterations, int &status)
Pointer to the GSL multifit solver.
Definition: Fit.cpp:106
Fit::setDataCurve
void setDataCurve(QwtPlotCurve *curve, double start, double end)
Definition: Fit.cpp:247
Fit::freeWorkspace
void freeWorkspace()
Frees the memory allocated for the fit workspace.
Definition: Fit.cpp:1133
Fit::rSquare
double rSquare()
Returns R^2.
Definition: Fit.cpp:379
Fit::resultFormula
QString resultFormula()
Definition: Fit.h:74
Fit::d_errors
double * d_errors
Stores standard deviations of the result parameters.
Definition: Fit.h:236
Fit::User
Definition: Fit.h:57
Fit::init
void init()
Definition: Fit.cpp:67
Fit::Plugin
Definition: Fit.h:57
Fit::d_cov_matrix
Matrix * d_cov_matrix
Matrix window used for the output of covariance matrix.
Definition: Fit.h:257
Fit::numParameters
int numParameters()
Definition: Fit.h:78
Fit::d_gen_function
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
Fit::scaleErrors
void scaleErrors(bool yes=true)
Specifies wheather the errors must be scaled with sqrt(chi_2/dof)
Definition: Fit.h:133
Fit::guessInitialValues
virtual void guessInitialValues()
Definition: Fit.h:87
Fit::generateFunction
void generateFunction(bool yes, int points=100)
Specifies weather the result of the fit is a function curve.
Definition: Fit.cpp:285
Fit::d_p
int d_p
Number of fit parameters.
Definition: Fit.h:192
Fit::d_f
fit_function d_f
Definition: Fit.h:186
Fit::calculateFitCurveData
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
Fit::results
double * results()
Returns a vector with the fit results.
Definition: Fit.h:99
Fit::d_param_table
QPointer< Table > d_param_table
Table window used for the output of fit parameters.
Definition: Fit.h:254
Fit::insertFitFunctionCurve
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
Fit::d_formula
QString d_formula
The fit formula given on input.
Definition: Fit.h:218
Table
MDI window providing a spreadsheet table with column logic.
Definition: Table.h:57
ApplicationWindow.h
Fit::UnscaledLevenbergMarquardt
Definition: Fit.h:55
Fit::eval
virtual double eval(double *, double)
Calculates the data for the output fit curve.
Definition: Fit.h:150
Fit::fit_function_df
int(* fit_function_df)(const gsl_vector *, void *, gsl_matrix *)
Definition: Fit.h:52
Fit::freeMemory
virtual void freeMemory()
Frees all the arrays with size d_n. Used when changing the source data sets.
Definition: Fit.cpp:1166
Fit::d_scale_errors
bool d_scale_errors
Specifies wheather the errors must be scaled with sqrt(chi_2/dof)
Definition: Fit.h:251
Fit::d_df
fit_function_df d_df
Definition: Fit.h:187
Fit::WeightingMethod
WeightingMethod
Definition: Fit.h:56
Fit::rss
double rss()
Returns the Residual Sum of Squares.
Definition: Fit.h:127
Fit::legendInfo
virtual QString legendInfo()
Output string added to the plot as a new legend.
Definition: Fit.cpp:402
Fit::Algorithm
Algorithm
Definition: Fit.h:55
Fit::d_param_explain
QStringList d_param_explain
Stores a list of short explanations for the significance of the fit parameters.
Definition: Fit.h:209
Fit::d_param_init
gsl_vector * d_param_init
Initial guesses for the fit parameters.
Definition: Fit.h:195
Fit::setInitialGuess
void setInitialGuess(int parIndex, double val)
Definition: Fit.h:84
Fit::type
FitType type()
Definition: Fit.h:143
Fit::save
bool save(const QString &fileName)
Definition: Fit.cpp:1043
Fit::Instrumental
Definition: Fit.h:56
Fit::NoWeighting
Definition: Fit.h:56
Fit::customizeFitResults
virtual void customizeFitResults()
Customs and stores the fit results according to the derived class specifications. Used by exponential...
Definition: Fit.h:162