MeteoIODoc 2.11.0
libfit1DCore.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/***********************************************************************************/
3/* Copyright 2011 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
4/***********************************************************************************/
5/* This file is part of MeteoIO.
6 MeteoIO is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 MeteoIO is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef LIBFIT1DCORE_H
20#define LIBFIT1DCORE_H
21
24
25#include <vector>
26
27namespace mio {
28
29//class to use as an interface
30class FitModel {
31 public:
32 FitModel(const std::string& i_regname, const size_t& i_nParam, const size_t& i_min_nb_pts)
33 : Lambda(), X(), Y(), infoString(), regname(i_regname), nPts(0), nParam(i_nParam), min_nb_pts(i_min_nb_pts), fit_ready(false) {}
34 virtual ~FitModel() {}
35 virtual void setData(const std::vector<double>& in_X, const std::vector<double>& in_Y) = 0;
36 void setGuess(const std::vector<double>& lambda_in);
37 virtual void setLapseRate(const double& /*lapse_rate*/) {throw InvalidArgumentException("Lapse rates can only be forced for linear regressions!", AT);}
38 virtual void setDegree(const size_t& /*degree*/) {throw InvalidArgumentException("Degree can only be set for polynomial regression!", AT);}
39 virtual bool fit() = 0;
40 virtual double f(const double& x) const = 0;
41 double operator ()(const double& x) const { return f(x);}
42 std::vector<double> getParams() const;
43 std::string getName() const {return regname;}
44 std::string getInfo() const {return infoString;}
45 void setInfo(const std::string& info) {infoString=info;}
46 FitModel& operator =(const FitModel& source);
47 bool isReady() const {return fit_ready;}
48 std::string toString() const;
49 protected:
50 virtual bool checkInputs();
51
52 std::vector<double> Lambda; //parameters of the fit
53 std::vector<double> X; //X of input data set to fit
54 std::vector<double> Y; //Y of input data set to fit
55 std::string infoString;
56
57 const std::string regname; //model name
58 size_t nPts; //number of data points
59 size_t nParam; //number of parameters
60 size_t min_nb_pts; //minimum number of data points
61 bool fit_ready; //set to false if fit() must be called before using the fit
62};
63
74class FitLeastSquare : public FitModel {
75 public:
76 FitLeastSquare(const std::string& i_regname, const size_t& i_nParam, const size_t& i_min_nb_pts) : FitModel(i_regname, i_nParam, i_min_nb_pts) {}
77 void setData(const std::vector<double>& in_X, const std::vector<double>& in_Y);
78 bool fit();
79 virtual double f(const double& x) const = 0;
80
81 protected:
82 virtual void setDefaultGuess(); //set defaults guess values. Called by setData
83
84 private:
85 void initLambda();
86 void initDLambda(Matrix& dLambda) const;
87 double getDelta(const double& var) const;
88 double DDer(const double& x, const size_t& index);
89 bool computeFit();
90
91 static const double lambda_init; //initial default guess
92 static const double delta_init_abs; //initial delta, absolute
93 static const double delta_init_rel; //initial delta, relative
94 static const double eps_conv; //convergence criteria
95 static const unsigned int max_iter; //maximum number of iterations
96};
97
98} //end namespace
99
100#endif
#define AT
Definition: IOExceptions.h:28
A class to perform non-linear least square fitting. It works on a time serie and uses matrix arithmet...
Definition: libfit1DCore.h:74
FitLeastSquare(const std::string &i_regname, const size_t &i_nParam, const size_t &i_min_nb_pts)
Definition: libfit1DCore.h:76
bool fit()
Definition: libfit1DCore.cc:135
virtual double f(const double &x) const =0
void setData(const std::vector< double > &in_X, const std::vector< double > &in_Y)
Definition: libfit1DCore.cc:119
virtual void setDefaultGuess()
Definition: libfit1DCore.cc:128
Definition: libfit1DCore.h:30
std::vector< double > Lambda
Definition: libfit1DCore.h:52
FitModel & operator=(const FitModel &source)
Definition: libfit1DCore.cc:51
bool fit_ready
Definition: libfit1DCore.h:61
bool isReady() const
Definition: libfit1DCore.h:47
std::string getInfo() const
Definition: libfit1DCore.h:44
virtual bool checkInputs()
Definition: libfit1DCore.cc:63
std::vector< double > getParams() const
Definition: libfit1DCore.cc:30
void setGuess(const std::vector< double > &lambda_in)
Definition: libfit1DCore.cc:37
std::string getName() const
Definition: libfit1DCore.h:43
virtual void setData(const std::vector< double > &in_X, const std::vector< double > &in_Y)=0
virtual double f(const double &x) const =0
virtual bool fit()=0
size_t nPts
Definition: libfit1DCore.h:58
virtual void setDegree(const size_t &)
Definition: libfit1DCore.h:38
void setInfo(const std::string &info)
Definition: libfit1DCore.h:45
double operator()(const double &x) const
Definition: libfit1DCore.h:41
virtual void setLapseRate(const double &)
Definition: libfit1DCore.h:37
size_t nParam
Definition: libfit1DCore.h:59
virtual ~FitModel()
Definition: libfit1DCore.h:34
std::string toString() const
Definition: libfit1DCore.cc:85
size_t min_nb_pts
Definition: libfit1DCore.h:60
std::vector< double > Y
Definition: libfit1DCore.h:54
std::string infoString
Definition: libfit1DCore.h:55
const std::string regname
Definition: libfit1DCore.h:57
FitModel(const std::string &i_regname, const size_t &i_nParam, const size_t &i_min_nb_pts)
Definition: libfit1DCore.h:32
std::vector< double > X
Definition: libfit1DCore.h:53
thrown when encountered an unexpected function's argument (e.g. bad index, bad or missing parameter n...
Definition: IOExceptions.h:130
This class implements the basic operations on matrices. Elements are access in matrix notation: that ...
Definition: Matrix.h:42
Definition: Config.cc:31