MeteoIODoc 2.11.0
libfit1D.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 LIBFIT1D_H
20#define LIBFIT1D_H
21
23#include <meteoio/IOUtils.h>
24
25#include <vector>
26
27namespace mio {
28
29class Zero : public FitModel {
30 public:
31 Zero() : FitModel("Zero", 0, 0) {fit_ready=true;}
32 void setData(const std::vector<double>& /*in_X*/, const std::vector<double>& /*in_Y*/) {}
33 bool fit() { return true;}
34 double f(const double& /*x*/) const {return 0.;}
35};
36
37class SimpleLinear : public FitModel {
38 public:
39 SimpleLinear() : FitModel("SimpleLinear", 2, 2), fixed_lapse_rate(IOUtils::nodata) {fit_ready=false;}
40 SimpleLinear(const std::string& i_regname) : FitModel(i_regname, 2, 2), fixed_lapse_rate(IOUtils::nodata) {fit_ready=false;}
41 void setData(const std::vector<double>& in_X, const std::vector<double>& in_Y);
42 bool fit();
43 double f(const double& x) const;
44 void setLapseRate(const double& in_lapse_rate) {fixed_lapse_rate = in_lapse_rate; fit_ready = false; min_nb_pts--;}
45 protected:
47};
48
49class NoisyLinear : public SimpleLinear {
50 public:
51 NoisyLinear() : SimpleLinear("NoisyLinear") {fit_ready=false;}
52 bool fit();
53};
54
56 public:
57 SphericVario() : FitLeastSquare("SphericVario", 3, 4) {fit_ready = false;}
58 void setDefaultGuess();
59 double f(const double& x) const;
60};
61
62class LinVario : public FitLeastSquare {
63 public:
64 LinVario() : FitLeastSquare("LinVario", 2, 3) {fit_ready = false;}
65 void setDefaultGuess();
66 double f(const double& x) const;
67};
68
69class ExpVario : public FitLeastSquare {
70 public:
71 ExpVario() : FitLeastSquare("ExpVario", 3, 4) {fit_ready = false;}
72 void setDefaultGuess();
73 double f(const double& x) const;
74};
75
77 public:
78 RatQuadVario() : FitLeastSquare("RatQuadVario", 3, 4) {fit_ready = false;}
79 void setDefaultGuess();
80 double f(const double& x) const;
81};
82
83class LinearLS : public FitLeastSquare {
84 public:
85 LinearLS() : FitLeastSquare("LinearLS", 2, 3) {fit_ready = false;}
86 void setDefaultGuess();
87 double f(const double& x) const;
88};
89
90class Quadratic : public FitLeastSquare {
91 public:
92 Quadratic() : FitLeastSquare("Quadratic", 3, 4) {fit_ready = false;}
93 void setDefaultGuess();
94 double f(const double& x) const;
95};
96
119 public:
120 //the default-constructor sets the degree to 2 (= quadratic regression)
121 PolynomialRegression() : FitModel("POLYNOMIAL", 3, 3), degree(2) {fit_ready=false;}
122 PolynomialRegression(const std::string& i_regname, const size_t& degreeOfRegression) :
123 FitModel(i_regname, degreeOfRegression+1, degreeOfRegression+1),
124 degree(degreeOfRegression){fit_ready=false;}
125 void setData(const std::vector<double>& in_X, const std::vector<double>& in_Y);
126 bool fit();
127 double f(const double& x) const;
128 void setDegree(const size_t& in_degree) {degree = in_degree; fit_ready = false; min_nb_pts=in_degree+1; nParam=in_degree+1; }
129 protected:
130 size_t degree;
131};
132
159class Fit1D {
160 public:
162 typedef enum REGRESSION {
174
179 Fit1D() : model(nullptr) {}
180
188 Fit1D(const regression& regType, const std::vector<double>& in_X, const std::vector<double>& in_Y, const bool& updatefit=true);
189
197 Fit1D(const std::string& regType, const std::vector<double>& in_X, const std::vector<double>& in_Y, const bool& updatefit=true);
198
203 Fit1D(const Fit1D& i_fit);
204
205 ~Fit1D() {delete model;}
206
215 bool setModel(const regression& i_regType, const std::vector<double>& in_X, const std::vector<double>& in_Y, const bool& updatefit=true);
216
225 bool setModel(const std::string& i_regType, const std::vector<double>& in_X, const std::vector<double>& in_Y, const bool& updatefit=true);
226
233 void setGuess(const std::vector<double>& lambda_in) {model->setGuess(lambda_in);}
234
240 void setLapseRate(const double& lapse_rate) {model->setLapseRate(lapse_rate);}
241
247 void setDegree(const size_t& degree) {model->setDegree(degree);}
248
255 bool fit() {return model->fit();}
256
263 double f(const double& x) const {return model->f(x);}
264
270 std::vector<double> getParams() const { return model->getParams();}
271
276 std::string getName() const {return model->getName();}
277
283 std::string getInfo() const {return model->getInfo();}
284
291 void setInfo(const std::string& info) {model->setInfo(info);}
292
293 Fit1D& operator =(const Fit1D& source);
294
301 double operator ()(const double& x) const {return model->f(x);}
302
303 std::string toString() const {return model->toString();}
304
305 private:
306 FitModel *model;
307};
308
378 public:
379 FitLinClosedForm() : predictors(), observations(), Beta(), regname("MultiLinear"), infoString(), nPreds(0), fit_ready(false) {}
380
381 void addData(const std::vector<double>& vecPreds, const double& obs);
382 bool fit();
383 double f(const std::vector<double>& x) const;
384 double operator ()(const std::vector<double>& x) const { return f(x);}
385 std::vector<double> getParams() const;
386 std::string getName() const {return regname;}
387 std::string getInfo() const {return infoString;}
388 void setInfo(const std::string& info) {infoString=info;}
390 bool isReady() const {return fit_ready;}
391 std::string toString() const;
392 private:
393 std::vector< std::vector<double> > predictors;
394 std::vector<double> observations;
395 Matrix Beta;
396 const std::string regname; //model name
397 std::string infoString;
398 size_t nPreds;
399 bool fit_ready;
400};
401
402} //end namespace
403
404#endif
Definition: libfit1D.h:69
ExpVario()
Definition: libfit1D.h:71
double f(const double &x) const
Definition: libfit1D.cc:297
void setDefaultGuess()
Definition: libfit1D.cc:310
A class to perform 1D regressions.
Definition: libfit1D.h:159
std::string getName() const
Return the name of the fit model.
Definition: libfit1D.h:276
regression
Keywords for regression model.
Definition: libfit1D.h:162
@ NOISY_LINEAR
same as SIMPLE_LINEAR but trying to remove outliers
Definition: libfit1D.h:165
@ SPHERICVARIO
spherical variogram
Definition: libfit1D.h:168
@ RATQUADVARIO
rational quadratic variogram
Definition: libfit1D.h:169
@ QUADRATIC
quadratic
Definition: libfit1D.h:171
@ LINVARIO
linear variogram
Definition: libfit1D.h:166
@ EXPVARIO
exponential variogram
Definition: libfit1D.h:167
@ ZERO
always return zero (this is a way to disable detrending)
Definition: libfit1D.h:163
@ POLYNOMIAL
polynomial regression
Definition: libfit1D.h:172
@ LINEARLS
linear, using least squares
Definition: libfit1D.h:170
@ SIMPLE_LINEAR
basic, cheap linear fit
Definition: libfit1D.h:164
double operator()(const double &x) const
Calculate a value using the computed least square fit. The fit has to be computed before.
Definition: libfit1D.h:301
bool setModel(const regression &i_regType, const std::vector< double > &in_X, const std::vector< double > &in_Y, const bool &updatefit=true)
Set or reset the regression model.
Definition: libfit1D.cc:78
Fit1D & operator=(const Fit1D &source)
Definition: libfit1D.cc:50
Fit1D()
Empty Constructor. The model must be set afterwards. If the model has not been set before calling oth...
Definition: libfit1D.h:179
std::vector< double > getParams() const
Calculate the parameters of the fit. The fit has to be computed before.
Definition: libfit1D.h:270
bool fit()
Compute the regression parameters.
Definition: libfit1D.h:255
void setGuess(const std::vector< double > &lambda_in)
Provide a set of initial values for the model parameters. The model can be used right after providing...
Definition: libfit1D.h:233
~Fit1D()
Definition: libfit1D.h:205
double f(const double &x) const
Calculate a value using the computed least square fit. The fit has to be computed before.
Definition: libfit1D.h:263
void setInfo(const std::string &info)
Set the information string. This is useful to append some extra information to the information string...
Definition: libfit1D.h:291
void setDegree(const size_t &degree)
Set the degree of the polynomial regression This will throw an exception for all other regression mod...
Definition: libfit1D.h:247
std::string toString() const
Definition: libfit1D.h:303
void setLapseRate(const double &lapse_rate)
Set a forced lapse rate for linear regressions This will throw an exception for all other regression ...
Definition: libfit1D.h:240
std::string getInfo() const
Return a string of information about the fit. The fit has to be computed before.
Definition: libfit1D.h:283
A class to perform non-linear least square fitting. It works on a time serie and uses matrix arithmet...
Definition: libfit1DCore.h:74
A class to perform multiple linear regressions relying on the closed form solution.
Definition: libfit1D.h:377
std::string getInfo() const
Definition: libfit1D.h:387
void addData(const std::vector< double > &vecPreds, const double &obs)
Add one data point to the model.
Definition: libfit1D.cc:393
std::string getName() const
Definition: libfit1D.h:386
std::string toString() const
Definition: libfit1D.cc:490
bool isReady() const
Definition: libfit1D.h:390
FitLinClosedForm & operator=(const FitLinClosedForm &source)
Definition: libfit1D.cc:507
bool fit()
Definition: libfit1D.cc:411
std::vector< double > getParams() const
Definition: libfit1D.cc:478
FitLinClosedForm()
Definition: libfit1D.h:379
double operator()(const std::vector< double > &x) const
Definition: libfit1D.h:384
double f(const std::vector< double > &x) const
Definition: libfit1D.cc:462
void setInfo(const std::string &info)
Definition: libfit1D.h:388
Definition: libfit1DCore.h:30
bool fit_ready
Definition: libfit1DCore.h:61
std::string getInfo() const
Definition: libfit1DCore.h:44
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 double f(const double &x) const =0
virtual bool fit()=0
virtual void setDegree(const size_t &)
Definition: libfit1DCore.h:38
void setInfo(const std::string &info)
Definition: libfit1DCore.h:45
virtual void setLapseRate(const double &)
Definition: libfit1DCore.h:37
size_t nParam
Definition: libfit1DCore.h:59
std::string toString() const
Definition: libfit1DCore.cc:85
size_t min_nb_pts
Definition: libfit1DCore.h:60
Definition: libfit1D.h:62
double f(const double &x) const
Definition: libfit1D.cc:274
void setDefaultGuess()
Definition: libfit1D.cc:286
LinVario()
Definition: libfit1D.h:64
Definition: libfit1D.h:83
void setDefaultGuess()
Definition: libfit1D.cc:350
LinearLS()
Definition: libfit1D.h:85
double f(const double &x) const
Definition: libfit1D.cc:345
This class implements the basic operations on matrices. Elements are access in matrix notation: that ...
Definition: Matrix.h:42
Definition: libfit1D.h:49
bool fit()
Definition: libfit1D.cc:154
NoisyLinear()
Definition: libfit1D.h:51
A class to perform 1D polynomial regression.
Definition: libfit1D.h:118
double f(const double &x) const
Definition: libfit1D.cc:193
size_t degree
Definition: libfit1D.h:130
PolynomialRegression()
Definition: libfit1D.h:121
bool fit()
Definition: libfit1D.cc:202
void setData(const std::vector< double > &in_X, const std::vector< double > &in_Y)
Definition: libfit1D.cc:185
void setDegree(const size_t &in_degree)
Definition: libfit1D.h:128
PolynomialRegression(const std::string &i_regname, const size_t &degreeOfRegression)
Definition: libfit1D.h:122
Definition: libfit1D.h:90
double f(const double &x) const
Definition: libfit1D.cc:362
void setDefaultGuess()
Definition: libfit1D.cc:367
Quadratic()
Definition: libfit1D.h:92
Definition: libfit1D.h:76
void setDefaultGuess()
Definition: libfit1D.cc:334
RatQuadVario()
Definition: libfit1D.h:78
double f(const double &x) const
Definition: libfit1D.cc:321
Definition: libfit1D.h:37
bool fit()
Definition: libfit1D.cc:122
SimpleLinear()
Definition: libfit1D.h:39
void setLapseRate(const double &in_lapse_rate)
Definition: libfit1D.h:44
double f(const double &x) const
Definition: libfit1D.cc:118
SimpleLinear(const std::string &i_regname)
Definition: libfit1D.h:40
double fixed_lapse_rate
Definition: libfit1D.h:46
void setData(const std::vector< double > &in_X, const std::vector< double > &in_Y)
Definition: libfit1D.cc:111
Definition: libfit1D.h:55
SphericVario()
Definition: libfit1D.h:57
double f(const double &x) const
Definition: libfit1D.cc:251
void setDefaultGuess()
Definition: libfit1D.cc:268
Definition: libfit1D.h:29
Zero()
Definition: libfit1D.h:31
double f(const double &) const
Definition: libfit1D.h:34
bool fit()
Definition: libfit1D.h:33
void setData(const std::vector< double > &, const std::vector< double > &)
Definition: libfit1D.h:32
const double nodata
This is the internal nodata value.
Definition: IOUtils.h:75
Definition: Config.cc:31