source: trunk/test/regression_test.cc @ 392

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

minor changes

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