source: trunk/src/RegressionLocal.cc @ 235

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

Major modifications

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.4 KB
Line 
1// $Id: RegressionLocal.cc 235 2005-02-21 14:53:48Z 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
12namespace theplu {
13namespace statistics {
14
15  RegressionLocal::RegressionLocal(Regression& r, 
16                                   RegressionKernel& k)
17    : kernel_(&k), regressor_(&r)
18  {
19  }
20
21  void RegressionLocal::fit(const double f, const u_int step_size)
22  {
23    sort(data_.begin(), data_.end());
24
25    // number of points on each side
26    size_t nof_points = static_cast<size_t>(f*data_.size());
27    double width = 0;
28    int min_index = 0;
29    for (size_t i=0; i<data_.size(); i+=step_size) {
30      x_.push_back(data_[i].first);
31     
32      // determining boundaries window/kernel
33     
34      // extreme left case
35      if (data_[i].first <
36          (data_[nof_points-1].first+data_[nof_points-1].first)/2 ){
37        min_index=0;
38        width = data_[nof_points-1].first-data_[i].first;
39      }
40      // extreme right case
41      else if (data_[i].first > (data_[data_.size()-1].first+
42                            data_[data_.size()-nof_points].first)/2 ){
43        min_index = data_.size() - nof_points;
44        width = data_[i].first - data_[data_.size()-nof_points].first;
45      }
46      else {
47        // stepping forward until x is in the middle
48        while(data_[min_index+nof_points-1].first-data_[i].first
49              < data_[i].first-data_[min_index].first)
50          min_index++;
51        width = data_[min_index+nof_points-1].first-data_[i].first;
52        // Peter, should check if x gets closer to centrum if we step
53        // back one step
54      }
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      regressor_->predict(x_[i], y_[i], y_err_[i]);
77    }
78  }
79
80  std::ostream& RegressionLocal::print(std::ostream& s) const
81  {
82    for (size_t i=0; i<x_.size(); i++) {
83      regressor_->print(s);
84      s << std::endl;
85    }
86    return s;
87  }
88
89
90}} // of namespace cpptools and namespace theplu
Note: See TracBrowser for help on using the repository browser.