Class to fit an arbitrary function to data and errors by non-linear least squares. The function object should provide a typedef of value_type
and
operator()
TPAR
to denote the type of the parameters, NPAR
their number, and T
the type of the argument: class function { public: typedef T value_type; value_type operator()( const T x, const FVector<TPAR,NPAR>& p, FVector<TPAR,NPAR>& df_dpi ) const { value_type value = ...; for( int i=1; i<=NPAR; ++i ) df_dpi(i) = ...; return value; } }
Here's an example class that fits a quadratic function:
class function { public: typedef float value_type; float operator()( const float x, const FVector<float,3>& p, FVector<float,3>& df_dpi ) const { float value = p(1)*x*x + p(2)*x + p(3); df_dpi(1) = x*x; df_dpi(2) = x; df_dpi(3) = 1.0f; return value; } };
This function is used in the following way:
// just to illustrate the template parameters: // TFUNC is the function object, // TPAR the type of the parameters (needs not be the same as the input or return type of the function), // NPAR the number of parameters. template<typename TFUNC, typename TPAR, int NPAR> class LMFit; function F; LMFit< function, float, 3> M( F ); MArray<float,1> X(10); X = 1.27355, -0.654883, 3.7178, 2.31818, 2.6652, -2.02182, 4.82368, -4.36208, 4.84084, 2.44391; MArray<float,1> Y(10); Y = 4.30655,0.420333,18.6992,8.27967,10.8136,3.3598,29.3496,15.9234,29.4432,9.6158; MArray<float,1> dY2(10); dY2 = 1.0f; FVector<float,3> P0; // initial guess. P0 = 0.3, -1.0, 0.0; M.eval( X, Y, dY2, -9999.0f, P0 ); // perform the fitting. cout << M.getResult() << endl; // result also in P0 on exit.
About any container can be used for passing X, Y, and dY data, as long as the container provides
typedef Container::const_itertor ...
typedef Container::value_type ...
Container::value_type
FVector<float,2>
MArray<FVector<float,2> >
ltl::LMFit< TFUNC, TPAR, NPAR >::LMFit | ( | const TFUNC & | func, | |
const int | itermax = 1000 , |
|||
const TPAR | tol = 1e-7 , |
|||
const FVector< bool, NPAR > | ignin = false , |
|||
const TPAR | astart = 1e-3 , |
|||
const TPAR | astep = 10.0 | |||
) | [inline] |
Construct class with fit constraints.
void ltl::LMFit< TFUNC, TPAR, NPAR >::setIgnore | ( | const FVector< bool, NPAR > & | ignin | ) | [inline] |
specify which parameters to leave constant during fit [ignin(i)=true].
void ltl::LMFit< TFUNC, TPAR, NPAR >::eval | ( | const TDATAX & | x, | |
const TDATAY & | y, | |||
const TDATAY & | dy2, | |||
const typename TDATAY::value_type | nan_y, | |||
const FVector< TPAR, NPAR > & | inpar | |||
) | [inline] |
Fit to data and ignoring nan, start with inpar.
FVector<TPAR,NPAR> ltl::LMFit< TFUNC, TPAR, NPAR >::getResult | ( | ) | [inline] |
Return result vector.
double ltl::LMFit< TFUNC, TPAR, NPAR >::getChiSquare | ( | ) | const [inline] |
Return final .
int ltl::LMFit< TFUNC, TPAR, NPAR >::getNIteration | ( | ) | const [inline] |
Return No needed iterations.
FVector<TPAR,NPAR> ltl::LMFit< TFUNC, TPAR, NPAR >::getVariance | ( | ) | [inline] |
Return in fit parameters.