1 | // $Id: RegressionLocal.cc 221 2004-12-30 22:36:25Z peter $ |
---|
2 | |
---|
3 | #include "RegressionLocal.h" |
---|
4 | |
---|
5 | #include "Regression.h" |
---|
6 | #include "RegressionKernel.h" |
---|
7 | #include "RegressionLinear.h" |
---|
8 | #include "vector.h" |
---|
9 | |
---|
10 | #include "algorithm" |
---|
11 | |
---|
12 | namespace theplu { |
---|
13 | namespace statistics { |
---|
14 | |
---|
15 | RegressionLocal::RegressionLocal(const gslapi::vector& x, |
---|
16 | const gslapi::vector& y, |
---|
17 | Regression& r, |
---|
18 | RegressionKernel& k, |
---|
19 | const size_t nof_predictions) |
---|
20 | : kernel_(&k), regression_(&r) |
---|
21 | { |
---|
22 | estimated_x_ = gslapi::vector(nof_predictions); |
---|
23 | estimated_y_ = estimated_x_; |
---|
24 | estimated_y_err_ = estimated_x_; |
---|
25 | //sorting data with respect to x |
---|
26 | // Peter, could this be done without all the copying |
---|
27 | std::vector<std::pair<double, double> > data; |
---|
28 | for (size_t i=0; i<x.size(); i++){ |
---|
29 | std::pair<double, double> tmp(x(i),y(i)); |
---|
30 | data_.push_back(tmp); |
---|
31 | } |
---|
32 | sort(data_.begin(), data_.end()); |
---|
33 | for (size_t i=0; i<nof_predictions; i++) |
---|
34 | estimated_x_(i) = |
---|
35 | data_[static_cast<size_t>((i+0.5)/nof_predictions*data_.size())].first; |
---|
36 | } |
---|
37 | |
---|
38 | void RegressionLocal::fit(const double f) |
---|
39 | { |
---|
40 | // number of points on each side |
---|
41 | size_t nof_data_points = static_cast<size_t>(f/2*data_.size()); |
---|
42 | size_t index_min = 0; |
---|
43 | size_t index_max = 0; |
---|
44 | for (size_t i=0; i<estimated_x_.size(); i++) { |
---|
45 | // extreme left case |
---|
46 | if (data_[nof_data_points-1].first>estimated_x_(i)){ |
---|
47 | index_min = 0; |
---|
48 | index_max = 2*nof_data_points-1; |
---|
49 | } |
---|
50 | |
---|
51 | // extreme right case |
---|
52 | else if (data_[data_.size()-estimated_x_.size()-1].first< |
---|
53 | estimated_x_(i)){ |
---|
54 | index_min = data_.size()-2*nof_data_points; |
---|
55 | index_max = data_.size()-1; |
---|
56 | } |
---|
57 | |
---|
58 | // normal case - window in the middle |
---|
59 | else { |
---|
60 | for (size_t j=nof_data_points; j<data_.size(); j++) |
---|
61 | if (data_[j].first>estimated_x_(i)) { |
---|
62 | index_min = j-nof_data_points; |
---|
63 | index_max = j+nof_data_points-1; |
---|
64 | break; |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | // copying data |
---|
69 | // Peter, this is a stupid solution which could be |
---|
70 | // improved by using a moving view (in the vector class). |
---|
71 | gslapi::vector x(index_max-index_min+1); |
---|
72 | gslapi::vector y(index_max-index_min+1); |
---|
73 | gslapi::vector w(index_max-index_min+1); |
---|
74 | for (size_t j=0; j<x.size(); j++) |
---|
75 | x(j)=data_[index_min+j].first; |
---|
76 | for (size_t j=0; j<y.size(); j++){ |
---|
77 | y(j)=data_[index_min+j].second; |
---|
78 | if (y(j)>10) |
---|
79 | std::cout << x(j) << "\t" << y(j) << std::endl; |
---|
80 | } |
---|
81 | // calculating weights |
---|
82 | for (size_t j=0; j<y.size(); j++) |
---|
83 | w(j) = kernel_->weight( 2*(x(j)-data_[index_min].first)/ |
---|
84 | (data_[index_max].first- |
---|
85 | data_[index_min].first)-1); |
---|
86 | |
---|
87 | |
---|
88 | // fitting the regressor locally |
---|
89 | std::cout << "x: " << estimated_x_(i) << std::endl; |
---|
90 | std::cout << "x.begin: " << x(0) << std::endl; |
---|
91 | std::cout << "x.end: " << x(x.size()-1) << std::endl; |
---|
92 | regression_->fit(x,y,w); |
---|
93 | std::cout << "estimating value\n"; |
---|
94 | regression_->predict(estimated_x_(i), |
---|
95 | estimated_y_(i), |
---|
96 | estimated_y_err_(i)); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | std::ostream& operator<< (std::ostream& s, const RegressionLocal& r) |
---|
101 | { |
---|
102 | gslapi::vector x=r.estimated_x(); |
---|
103 | gslapi::vector y(r.estimated_y()); |
---|
104 | gslapi::vector y_err(r.estimated_y_err()); |
---|
105 | for (size_t i=0; i<x.size(); i++){ |
---|
106 | s << x(i) << "\t"; |
---|
107 | s << y(i) << "\t"; |
---|
108 | s << y_err(i) << "\n"; |
---|
109 | } |
---|
110 | return s; |
---|
111 | } |
---|
112 | |
---|
113 | }} // of namespace cpptools and namespace theplu |
---|