1
/***************************************************************************
4
--------------------------------------------------------------------
5
Copyright : (C) 2007-2009 by Knut Franke
6
Email (use @ for *) : knut.franke*gmx.de
7
Description : Base class for doing fits using the algorithms
10
***************************************************************************/
12
/***************************************************************************
14
* This program is free software; you can redistribute it and/or modify *
15
* it under the terms of the GNU General Public License as published by *
16
* the Free Software Foundation; either version 2 of the License, or *
17
* (at your option) any later version. *
19
* This program is distributed in the hope that it will be useful, *
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22
* GNU General Public License for more details. *
24
* You should have received a copy of the GNU General Public License *
25
* along with this program; if not, write to the Free Software *
26
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
27
* Boston, MA 02110-1301 USA *
29
***************************************************************************/
30
#ifndef ABSTRACT_FIT_H
31
#define ABSTRACT_FIT_H
33
#include "AbstractFilter.h"
38
class FitSetAlgorithmCmd;
41
* \brief Base class for doing fits using the algorithms provided by GSL.
43
* Implementations of AbstractFit essentially provide methods to compute a specific function to be
44
* fitted, its derivatives with respect to the fit parameters and sensible estimates for the
45
* parameters (given the data to be fitted).
47
* Input is accepted on two ports: X and Y. Y is assumed to be a function of X, which is
48
* approximated by the function specified by the implementation of AbstractFit being used.
50
* On its output ports, AbstractFit provides (in order) the current values of the parameters,
51
* their names, and textual descriptions. Current values can be initial values specified by
52
* the user, automated estimates made by the implementation, or the results of the fit.
53
* You only ever get to see the first two when you haven't connected the input ports of
54
* AbstractFit yet, because after the first connect (and possibly subsequent calls to
55
* setInitialValue()), the fit is automatically recomputed. After calling setAutoRefit(),
56
* it is also recomputed whenever the input data changes.
58
class AbstractFit : public AbstractFilter
61
enum Algorithm { ScaledLevenbergMarquardt, UnscaledLevenbergMarquardt, NelderMeadSimplex };
62
//! Possible sources for weighting data.
64
NoWeighting, //!< Use 1.0 as weight for all points (=> cannot produce sensible error estimates)
65
Poisson, //!< Assume data follows Poisson distribution (weight with sqrt(N)).
66
Dataset //!< Use weighting data from a user-specified data source.
68
static QString nameOf(Algorithm algo) {
70
case ScaledLevenbergMarquardt: return tr("scaled Levenberg-Marquardt");
71
case UnscaledLevenbergMarquardt: return tr("unscaled Levenberg-Marquardt");
72
case NelderMeadSimplex: return tr("Nelder-Mead / simplex");
75
static QString nameOf(Weighting weighting) {
77
case NoWeighting: return tr("no weighting");
78
case Poisson: return tr("Poisson / sqrt(N)");
79
case Dataset: return tr("user-supplied");
82
AbstractFit() : d_algorithm(ScaledLevenbergMarquardt), d_tolerance(1e-8), d_maxiter(1000), d_auto_refit(false), d_outdated(true) {}
83
virtual ~AbstractFit() {}
84
bool isOutdated() const { return d_outdated; }
85
void setAlgorithm(Algorithm a) { d_algorithm = a; }
86
void setAutoRefit(bool auto_refit = true) { d_auto_refit = auto_refit; }
88
//!\name Handling of fit parameters
90
void setInitialValue(int parameter, double value);
91
virtual void guessInitialValues() = 0;
94
//!\name Reimplemented from AbstractFilter
97
virtual int inputCount() const { return 2; }
98
virtual QString inputName(int port) const { return port==0 ? "X" : "Y"; }
99
virtual int outputCount() const { return 3; }
101
virtual void dataChanged(AbstractDataSource*) {
106
static virtual double fitFunctionSimplex(const gsl_vector*, void*) = 0;
107
static virtual int fitFunction(const gsl_vector*, void*, gsl_vector*) = 0;
108
static virtual int fitFunctionDf(const gsl_vector*, void*, gsl_matrix*) = 0;
109
static virtual int fitFunctionFdf(const gsl_vector*, void*, gsl_vector*, gsl_matrix*) {
113
//! Fit algorithm to use.
114
Algorithm d_algorithm;
115
//! Where to take weights from.
116
WeightingMethod d_weighting;
117
//! The tolerance ("epsilon") to be used for deciding when the fit was successful.
119
//! The maximum number of iterations to do before declaring the fit to have failed.
121
//! Whether to redo the fit each time input data changes.
123
//! If #d_auto_refit is false, this is true when the input data has changed since the last fit.
126
friend class FitSetAlgorithmCmd;
129
#endif // ifndef ABSTRACT_FIT_H