1 | // $Id: regression_test.cc 430 2005-12-08 22:53:08Z peter $ |
---|
2 | |
---|
3 | |
---|
4 | #include <c++_tools/gslapi/matrix.h> |
---|
5 | #include <c++_tools/statistics/KernelBox.h> |
---|
6 | #include <c++_tools/statistics/Linear.h> |
---|
7 | #include <c++_tools/statistics/LinearWeighted.h> |
---|
8 | #include <c++_tools/statistics/Local.h> |
---|
9 | #include <c++_tools/statistics/Naive.h> |
---|
10 | #include <c++_tools/statistics/NaiveWeighted.h> |
---|
11 | #include <c++_tools/statistics/Polynomial.h> |
---|
12 | |
---|
13 | #include <cmath> |
---|
14 | |
---|
15 | #include <fstream> |
---|
16 | #include <iostream> |
---|
17 | |
---|
18 | |
---|
19 | using namespace theplu; |
---|
20 | |
---|
21 | bool Local_test(statistics::regression::OneDimensionalWeighted&, |
---|
22 | statistics::regression::Kernel&); |
---|
23 | |
---|
24 | |
---|
25 | int main(const int argc,const char* argv[]) |
---|
26 | { |
---|
27 | using namespace theplu; |
---|
28 | std::ostream* error; |
---|
29 | if (argc>1 && argv[1]==std::string("-v")) |
---|
30 | error = &std::cerr; |
---|
31 | else { |
---|
32 | error = new std::ofstream("/dev/null"); |
---|
33 | if (argc>1) |
---|
34 | std::cout << "regression_test -v : for printing extra information\n"; |
---|
35 | } |
---|
36 | *error << "testing regression" << std::endl; |
---|
37 | bool ok = true; |
---|
38 | |
---|
39 | // test data for Linear and Naive (Weighted and non-weighted) |
---|
40 | gslapi::vector x(4); x(0)=1970; x(1)=1980; x(2)=1990; x(3)=2000; |
---|
41 | gslapi::vector y(4); y(0)=12; y(1)=11; y(2)=14; y(3)=13; |
---|
42 | gslapi::vector w(4); w(0)=0.1; w(1)=0.2; w(2)=0.3; w(3)=0.4; |
---|
43 | |
---|
44 | *error << "testing regression::LinearWeighted" << std::endl; |
---|
45 | statistics::regression::LinearWeighted linear_w; |
---|
46 | linear_w.fit(x,y,w); |
---|
47 | double y_predicted=0; |
---|
48 | double y_predicted_err=0; |
---|
49 | linear_w.predict(1990,y_predicted,y_predicted_err); |
---|
50 | if (y_predicted!=12.8){ |
---|
51 | *error << "regression_LinearWeighted: cannot reproduce fit." << std::endl; |
---|
52 | ok=false; |
---|
53 | } |
---|
54 | |
---|
55 | // testing regression::NaiveWeighted |
---|
56 | *error << "testing regression::NaiveWeighted" << std::endl; |
---|
57 | statistics::regression::NaiveWeighted naive_w; |
---|
58 | naive_w.fit(x,y,w); |
---|
59 | y_predicted=0; |
---|
60 | y_predicted_err=0; |
---|
61 | naive_w.predict(0.0,y_predicted,y_predicted_err); |
---|
62 | if (y_predicted!=(0.1*12+0.2*11+0.3*14+0.4*13)) { |
---|
63 | *error << "regression_NaiveWeighted: cannot reproduce fit." << std::endl; |
---|
64 | ok=false; |
---|
65 | } |
---|
66 | |
---|
67 | // testing regression::Local |
---|
68 | *error << "testing regression::Local" << std::endl; |
---|
69 | statistics::regression::KernelBox kb; |
---|
70 | statistics::regression::LinearWeighted rl; |
---|
71 | if (!Local_test(rl,kb)) { |
---|
72 | *error << "regression_Local: Linear cannot reproduce fit." << std::endl; |
---|
73 | ok=false; |
---|
74 | } |
---|
75 | statistics::regression::NaiveWeighted rn; |
---|
76 | if (!Local_test(rn,kb)) { |
---|
77 | *error << "regression_Local: Naive cannot reproduce fit." << std::endl; |
---|
78 | ok=false; |
---|
79 | } |
---|
80 | |
---|
81 | // testing regression::Polynomial |
---|
82 | *error << "testing regression::Polynomial" << std::endl; |
---|
83 | { |
---|
84 | std::ifstream s("data/regression_gauss.data"); |
---|
85 | gslapi::matrix data(s); |
---|
86 | gslapi::vector x(data.rows()); |
---|
87 | gslapi::vector ln_y(data.rows()); |
---|
88 | for (size_t i=0; i<data.rows(); ++i) { |
---|
89 | x(i)=data(i,0); |
---|
90 | ln_y(i)=log(data(i,1)); |
---|
91 | } |
---|
92 | |
---|
93 | statistics::regression::Polynomial polynomialfit(2); |
---|
94 | polynomialfit.fit(x,ln_y); |
---|
95 | gslapi::vector fit=polynomialfit.fit_parameters(); |
---|
96 | if (fabs(fit[0]-1.012229646706 + fit[1]-0.012561322528 + |
---|
97 | fit[2]+1.159674470130)>1e-11) { // Jari, fix number! |
---|
98 | *error << "regression_Polynomial: cannot reproduce fit." << std::endl; |
---|
99 | ok=false; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | *error << "testing regression::Linear" << std::endl; |
---|
104 | statistics::regression::Linear lin; |
---|
105 | |
---|
106 | *error << "testing regression::Naive" << std::endl; |
---|
107 | statistics::regression::Naive naive; |
---|
108 | |
---|
109 | if (error!=&std::cerr) |
---|
110 | delete error; |
---|
111 | |
---|
112 | return (ok ? 0 : -1); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | bool Local_test(statistics::regression::OneDimensionalWeighted& r, |
---|
118 | statistics::regression::Kernel& k) |
---|
119 | { |
---|
120 | statistics::regression::Local rl(r,k); |
---|
121 | gslapi::vector points(1000); |
---|
122 | for (size_t i=0; i<1000; i++){ |
---|
123 | rl.add(i, 10); |
---|
124 | points(i)=i; |
---|
125 | } |
---|
126 | |
---|
127 | rl.fit(100, points); |
---|
128 | |
---|
129 | gslapi::vector y = rl.y_predicted(); |
---|
130 | for (size_t i=0; i<y.size(); i++) |
---|
131 | if (y(i)!=10.0){ |
---|
132 | return false; |
---|
133 | } |
---|
134 | return true; |
---|
135 | } |
---|