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