1 | // $Id: RegressionLocal.cc 253 2005-03-03 11:08:37Z 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(Regression& r, |
---|
16 | RegressionKernel& k) |
---|
17 | : kernel_(&k), regressor_(&r) |
---|
18 | { |
---|
19 | } |
---|
20 | |
---|
21 | void RegressionLocal::fit(std::ostream& s, const double f, |
---|
22 | const u_int step_size) |
---|
23 | { |
---|
24 | sort(data_.begin(), data_.end()); |
---|
25 | regressor_->print_header(s); |
---|
26 | s << std::endl; |
---|
27 | |
---|
28 | // number of points on each side |
---|
29 | size_t nof_points = static_cast<size_t>(f*data_.size()); |
---|
30 | double width = 0; |
---|
31 | int min_index = 0; |
---|
32 | for (size_t i=0; i<data_.size(); i+=step_size) { |
---|
33 | // determining boundaries for window/kernel |
---|
34 | |
---|
35 | // extreme left case |
---|
36 | if (data_[i].first < |
---|
37 | (data_[nof_points-1].first+data_[nof_points-1].first)/2 ){ |
---|
38 | min_index=0; |
---|
39 | width = data_[nof_points-1].first-data_[i].first; |
---|
40 | } |
---|
41 | // extreme right case |
---|
42 | else if (data_[i].first > (data_[data_.size()-1].first+ |
---|
43 | data_[data_.size()-nof_points].first)/2 ){ |
---|
44 | min_index = data_.size() - nof_points; |
---|
45 | width = data_[i].first - data_[data_.size()-nof_points].first; |
---|
46 | } |
---|
47 | else { |
---|
48 | // stepping forward until x is in the middle |
---|
49 | while(data_[min_index+nof_points-1].first-data_[i].first |
---|
50 | < data_[i].first-data_[min_index].first) |
---|
51 | min_index++; |
---|
52 | width = data_[min_index+nof_points-1].first-data_[i].first; |
---|
53 | // Peter, should check if x gets closer to centrum if we step |
---|
54 | // back one step |
---|
55 | } |
---|
56 | |
---|
57 | // copying data |
---|
58 | // Peter, too much copying. Move the copying outside loop and |
---|
59 | // use a vector view (when available in gslapi::vector class). |
---|
60 | gslapi::vector x(nof_points); |
---|
61 | gslapi::vector y(nof_points); |
---|
62 | gslapi::vector w(nof_points); |
---|
63 | for (size_t j=0; j<x.size(); j++) |
---|
64 | x(j)=data_[min_index+j].first; |
---|
65 | for (size_t j=0; j<y.size(); j++){ |
---|
66 | y(j)=data_[min_index+j].second; |
---|
67 | } |
---|
68 | // calculating weights |
---|
69 | for (size_t j=0; j<y.size(); j++) |
---|
70 | w(j) = kernel_->weight( (x(j)-(data_[min_index].first+ |
---|
71 | data_[min_index+nof_points-1].first) /2) |
---|
72 | /width ); |
---|
73 | |
---|
74 | // fitting the regressor locally |
---|
75 | regressor_->fit(x,y,w); |
---|
76 | x_.push_back(data_[i].first); |
---|
77 | y_.push_back(0.0); |
---|
78 | y_err_.push_back(0.0); |
---|
79 | regressor_->predict(x_[x_.size()-1], y_[y_.size()-1], |
---|
80 | y_err_[y_err_.size()-1]); |
---|
81 | regressor_->print(s); |
---|
82 | s << std::endl; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | }} // of namespace cpptools and namespace theplu |
---|