4130 |
20 Jan 22 |
peter |
// $Id$ |
4130 |
20 Jan 22 |
peter |
2 |
|
4130 |
20 Jan 22 |
peter |
3 |
/* |
4130 |
20 Jan 22 |
peter |
Copyright (C) 2022 Peter Johansson |
4130 |
20 Jan 22 |
peter |
5 |
|
4130 |
20 Jan 22 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
4130 |
20 Jan 22 |
peter |
7 |
|
4130 |
20 Jan 22 |
peter |
The yat library is free software; you can redistribute it and/or |
4130 |
20 Jan 22 |
peter |
modify it under the terms of the GNU General Public License as |
4130 |
20 Jan 22 |
peter |
published by the Free Software Foundation; either version 3 of the |
4130 |
20 Jan 22 |
peter |
License, or (at your option) any later version. |
4130 |
20 Jan 22 |
peter |
12 |
|
4130 |
20 Jan 22 |
peter |
The yat library is distributed in the hope that it will be useful, |
4130 |
20 Jan 22 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4130 |
20 Jan 22 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
4130 |
20 Jan 22 |
peter |
General Public License for more details. |
4130 |
20 Jan 22 |
peter |
17 |
|
4130 |
20 Jan 22 |
peter |
You should have received a copy of the GNU General Public License |
4130 |
20 Jan 22 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
4130 |
20 Jan 22 |
peter |
20 |
*/ |
4130 |
20 Jan 22 |
peter |
21 |
|
4130 |
20 Jan 22 |
peter |
22 |
#include <config.h> |
4130 |
20 Jan 22 |
peter |
23 |
|
4130 |
20 Jan 22 |
peter |
24 |
#include "Suite.h" |
4130 |
20 Jan 22 |
peter |
25 |
|
4130 |
20 Jan 22 |
peter |
26 |
#include "yat/regression/Multivariate.h" |
4130 |
20 Jan 22 |
peter |
27 |
|
4130 |
20 Jan 22 |
peter |
28 |
#include "yat/utility/Matrix.h" |
4130 |
20 Jan 22 |
peter |
29 |
#include "yat/utility/MatrixConstView.h" |
4130 |
20 Jan 22 |
peter |
30 |
#include "yat/utility/Vector.h" |
4130 |
20 Jan 22 |
peter |
31 |
#include "yat/utility/VectorConstView.h" |
4130 |
20 Jan 22 |
peter |
32 |
|
4130 |
20 Jan 22 |
peter |
33 |
using namespace theplu::yat; |
4130 |
20 Jan 22 |
peter |
34 |
|
4130 |
20 Jan 22 |
peter |
35 |
class OldRegression : public regression::Multivariate |
4130 |
20 Jan 22 |
peter |
36 |
{ |
4130 |
20 Jan 22 |
peter |
37 |
public: |
4130 |
20 Jan 22 |
peter |
38 |
|
4130 |
20 Jan 22 |
peter |
39 |
void fit(const utility::Matrix& x, const utility::VectorBase& y) |
4130 |
20 Jan 22 |
peter |
40 |
{ |
4130 |
20 Jan 22 |
peter |
// some code |
4130 |
20 Jan 22 |
peter |
42 |
} |
4130 |
20 Jan 22 |
peter |
43 |
|
4130 |
20 Jan 22 |
peter |
44 |
const utility::Vector& fit_parameters(void) const |
4130 |
20 Jan 22 |
peter |
45 |
{ |
4130 |
20 Jan 22 |
peter |
46 |
return param_; |
4130 |
20 Jan 22 |
peter |
47 |
} |
4130 |
20 Jan 22 |
peter |
48 |
|
4130 |
20 Jan 22 |
peter |
49 |
double predict(const utility::VectorBase& x) const |
4130 |
20 Jan 22 |
peter |
50 |
{ |
4130 |
20 Jan 22 |
peter |
51 |
return 0.0; |
4130 |
20 Jan 22 |
peter |
52 |
} |
4130 |
20 Jan 22 |
peter |
53 |
|
4130 |
20 Jan 22 |
peter |
54 |
private: |
4130 |
20 Jan 22 |
peter |
55 |
utility::Vector param_; |
4130 |
20 Jan 22 |
peter |
56 |
}; |
4130 |
20 Jan 22 |
peter |
57 |
|
4130 |
20 Jan 22 |
peter |
58 |
|
4130 |
20 Jan 22 |
peter |
// Example how a new class is implemented |
4130 |
20 Jan 22 |
peter |
60 |
class NewRegression : public regression::Multivariate |
4130 |
20 Jan 22 |
peter |
61 |
{ |
4130 |
20 Jan 22 |
peter |
62 |
public: |
4130 |
20 Jan 22 |
peter |
63 |
void fit(const utility::Matrix& x, const utility::VectorBase& y) |
4130 |
20 Jan 22 |
peter |
64 |
{ |
4130 |
20 Jan 22 |
peter |
65 |
fit2(x, y); |
4130 |
20 Jan 22 |
peter |
66 |
} |
4130 |
20 Jan 22 |
peter |
67 |
|
4130 |
20 Jan 22 |
peter |
68 |
|
4130 |
20 Jan 22 |
peter |
69 |
void fit2(const utility::MatrixBase& x, const utility::VectorBase& y) |
4130 |
20 Jan 22 |
peter |
70 |
{ |
4130 |
20 Jan 22 |
peter |
// some magic code |
4130 |
20 Jan 22 |
peter |
72 |
} |
4130 |
20 Jan 22 |
peter |
73 |
|
4130 |
20 Jan 22 |
peter |
74 |
const utility::Vector& fit_parameters(void) const |
4130 |
20 Jan 22 |
peter |
75 |
{ |
4130 |
20 Jan 22 |
peter |
76 |
return param_; |
4130 |
20 Jan 22 |
peter |
77 |
} |
4130 |
20 Jan 22 |
peter |
78 |
|
4130 |
20 Jan 22 |
peter |
79 |
double predict(const utility::VectorBase& x) const |
4130 |
20 Jan 22 |
peter |
80 |
{ |
4130 |
20 Jan 22 |
peter |
81 |
return 0.0; |
4130 |
20 Jan 22 |
peter |
82 |
} |
4130 |
20 Jan 22 |
peter |
83 |
|
4130 |
20 Jan 22 |
peter |
84 |
private: |
4130 |
20 Jan 22 |
peter |
85 |
utility::Vector param_; |
4130 |
20 Jan 22 |
peter |
86 |
|
4130 |
20 Jan 22 |
peter |
87 |
}; |
4130 |
20 Jan 22 |
peter |
88 |
|
4130 |
20 Jan 22 |
peter |
89 |
|
4130 |
20 Jan 22 |
peter |
90 |
int main(int argc, char* argv[]) |
4130 |
20 Jan 22 |
peter |
91 |
{ |
4130 |
20 Jan 22 |
peter |
92 |
test::Suite suite(argc, argv); |
4130 |
20 Jan 22 |
peter |
93 |
|
4130 |
20 Jan 22 |
peter |
94 |
utility::Matrix X(1000, 10); |
4130 |
20 Jan 22 |
peter |
95 |
utility::Vector y(X.rows()); |
4130 |
20 Jan 22 |
peter |
96 |
OldRegression old_reg; |
4130 |
20 Jan 22 |
peter |
97 |
old_reg.fit(X, y); |
4130 |
20 Jan 22 |
peter |
98 |
|
4130 |
20 Jan 22 |
peter |
// skip last sample |
4130 |
20 Jan 22 |
peter |
100 |
utility::MatrixConstView Xview(X, 0, 0, X.rows()-1, X.columns()); |
4130 |
20 Jan 22 |
peter |
101 |
utility::VectorConstView yview(y, 0, Xview.rows()); |
4130 |
20 Jan 22 |
peter |
102 |
NewRegression new_reg; |
4130 |
20 Jan 22 |
peter |
103 |
new_reg.fit(X, y); |
4130 |
20 Jan 22 |
peter |
104 |
new_reg.fit2(Xview, yview); |
4130 |
20 Jan 22 |
peter |
105 |
|
4130 |
20 Jan 22 |
peter |
// when having old classes not implementing fit2, it should |
4130 |
20 Jan 22 |
peter |
// preferably be avoided yet let's test that it works |
4130 |
20 Jan 22 |
peter |
108 |
old_reg.fit2(Xview, yview); |
4130 |
20 Jan 22 |
peter |
109 |
old_reg.fit2(X, y); |
4130 |
20 Jan 22 |
peter |
110 |
|
4130 |
20 Jan 22 |
peter |
111 |
return suite.return_value(); |
4130 |
20 Jan 22 |
peter |
112 |
} |