source: trunk/test/regression_test.cc @ 429

Last change on this file since 429 was 429, checked in by Peter, 18 years ago

separating weighted and non-weighted regression to different classes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1// $Id: regression_test.cc 429 2005-12-08 19:50:11Z 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
19using namespace theplu;
20
21bool Local_test(statistics::regression::OneDimensionalWeighted&, 
22                statistics::regression::Kernel&);
23
24
25int 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  // testing regression::LinearWeighted
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_Linear: cannot reproduce fit." << std::endl;
52    ok=false;
53  }
54
55  // testing regression::NaiveWeighted
56  statistics::regression::NaiveWeighted naive_w;
57  naive_w.fit(x,y,w);
58  y_predicted=0;
59  y_predicted_err=0;
60  naive_w.predict(0.0,y_predicted,y_predicted_err);
61  if (y_predicted!=(0.1*12+0.2*11+0.3*14+0.4*13)) {
62    *error << "regression_NaiveWeighted: cannot reproduce fit." << std::endl;
63    ok=false;
64  }
65
66  // testing regression::Local
67  statistics::regression::KernelBox kb;
68  statistics::regression::LinearWeighted rl;
69  if (!Local_test(rl,kb)) {
70    *error << "regression_Local: Linear cannot reproduce fit." << std::endl;
71    ok=false;
72  }
73  statistics::regression::NaiveWeighted rn;
74  if (!Local_test(rn,kb)) {
75    *error << "regression_Local: Naive cannot reproduce fit." << std::endl;
76    ok=false;
77  }
78
79  // testing regression::Polynomial
80  {
81    std::ifstream s("data/regression_gauss.data");
82    gslapi::matrix data(s);
83    gslapi::vector x(data.rows());
84    gslapi::vector ln_y(data.rows());
85    for (size_t i=0; i<data.rows(); ++i) {
86      x(i)=data(i,0);
87      ln_y(i)=log(data(i,1));
88    }
89
90    statistics::regression::Polynomial polynomialfit(2);
91    polynomialfit.fit(x,ln_y);
92    gslapi::vector fit=polynomialfit.fit_parameters();
93    if (fabs(fit[0]-1.012229646706 + fit[1]-0.012561322528 +
94             fit[2]+1.159674470130)>1e-11) {  // Jari, fix number!
95      *error << "regression_Polynomial: cannot reproduce fit." << std::endl;
96      ok=false;
97    }
98  }
99
100  if (error!=&std::cerr)
101    delete error;
102
103  return (ok ? 0 : -1);
104}
105
106
107
108bool Local_test(statistics::regression::OneDimensionalWeighted& r, 
109                statistics::regression::Kernel& k)
110{
111  statistics::regression::Local rl(r,k);
112  for (size_t i=0; i<1000; i++){
113    double x = i;
114    double y = 10.0;
115    rl.add(x, y);
116  }
117
118  std::ofstream myout("/dev/null");
119  rl.fit(myout,0.1,1);
120
121  std::vector<double> y = rl.y();
122  for (size_t i=0; i<y.size(); i++) 
123    if (y[i]!=10.0){
124      return false;
125    }
126  return true;
127}
Note: See TracBrowser for help on using the repository browser.