1
/***************************************************************************
4
--------------------------------------------------------------------
5
Copyright : (C) 2006 by Ion Vasilief, Tilman Hoener zu Siederdissen
6
Email (use @ for *) : ion_vasilief*yahoo.fr, thzs*gmx.net
7
Description : Fit base class
9
***************************************************************************/
11
/***************************************************************************
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. *
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. *
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 *
28
***************************************************************************/
34
#include "ApplicationWindow.h"
37
#include <gsl/gsl_multifit_nlin.h>
38
#include <gsl/gsl_multimin.h>
44
class Fit : public Filter
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 *);
55
enum Algorithm{ScaledLevenbergMarquardt, UnscaledLevenbergMarquardt, NelderMeadSimplex};
56
enum WeightingMethod{NoWeighting, Instrumental, Statistical, Dataset};
58
Fit(ApplicationWindow *parent, Graph *g = 0, const char * name = 0);
61
//! Actually does the fit. Should be reimplemented in derived classes.
63
virtual bool run(){return false;};
65
//! Sets the data set to be used for weighting
66
bool setWeightingData(WeightingMethod w, const QString& colName = QString::null);
68
void setDataCurve(int curve, double start, double end);
70
QString formula(){return d_formula;};
71
int numParameters() {return d_p;}
73
void setInitialGuess(int parIndex, double val){gsl_vector_set(d_param_init, parIndex, val);};
74
void setInitialGuesses(double *x_init);
76
virtual void guessInitialValues(){};
78
void setAlgorithm(Algorithm s){d_solver = s;};
80
//! Specifies weather the result of the fit is a function curve
81
void generateFunction(bool yes, int points = 100);
83
//! Output string added to the plot as a new legend
84
virtual QString legendInfo();
86
//! Returns a vector with the fit results
87
double* results(){return d_results;};
89
//! Returns a vector with the standard deviations of the results
92
//! Returns the sum of squares of the residuals from the best-fit line
93
double chiSquare() {return chi_2;};
98
//! Specifies wheather the errors must be scaled with sqrt(chi_2/dof)
99
void scaleErrors(bool yes = true){d_scale_errors = yes;};
101
Table* parametersTable(const QString& tableName);
102
Matrix* covarianceMatrix(const QString& matrixName);
105
//! Pointer to the GSL multifit minimizer (for simplex algorithm)
106
gsl_multimin_fminimizer * fitSimplex(gsl_multimin_function f, int &iterations, int &status);
108
//! Pointer to the GSL multifit solver
109
gsl_multifit_fdfsolver * fitGSL(gsl_multifit_function_fdf f, int &iterations, int &status);
111
//! Customs and stores the fit results according to the derived class specifications. Used by exponential fits.
112
virtual void storeCustomFitResults(double *par);
115
//! Adds the result curve as a FunctionCurve to the plot, if d_gen_function = true
116
void insertFitFunctionCurve(const QString& name, double *x, double *y, int penWidth = 1);
118
//! Adds the result curve to the plot
119
virtual void generateFitCurve(double *par);
121
//! Calculates the data for the output fit curve and store itin the X an Y vectors
122
virtual void calculateFitCurveData(double *par, double *X, double *Y) { Q_UNUSED(par) Q_UNUSED(X) Q_UNUSED(Y) };
124
//! Output string added to the result log
125
virtual QString logFitInfo(double *par, int iterations, int status, const QString& plotName);
128
fit_function_df d_df;
129
fit_function_fdf d_fdf;
130
fit_function_simplex d_fsimplex;
132
//! Number of fit parameters
135
//! Initial guesses for the fit parameters
136
gsl_vector *d_param_init;
138
/*! \brief Tells whether the fitter uses non-linear/simplex fitting
139
* with an initial parameters set, that must be freed in the destructor.
143
//! weighting data set used for the fit
146
//! Names of the fit parameters
147
QStringList d_param_names;
149
//! Stores a list of short explanations for the significance of the fit parameters
150
QStringList d_param_explain;
152
//! Specifies weather the result curve is a FunctionCurve or a normal curve with the same x values as the fit data
161
//! Covariance matrix
164
//! The kind of weighting to be performed on the data
165
WeightingMethod d_weihting;
167
//! The name of the weighting dataset
168
QString weighting_dataset;
170
//! Stores the result parameters
173
//! Stores standard deviations of the result parameters
176
//! The sum of squares of the residuals from the best-fit line
179
//! Specifies wheather the errors must be scaled with sqrt(chi_2/dof)