source: trunk/yat/regression/Local.cc @ 682

Last change on this file since 682 was 682, checked in by Jari Häkkinen, 15 years ago

Addresses #153. Moved regression files to .../yat/regression.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1// $Id: Local.cc 682 2006-10-11 22:06:38Z jari $
2
3/*
4  Copyright (C) The authors contributing to this file.
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Local.h"
25#include "Kernel.h"
26#include "OneDimensionalWeighted.h"
27#include "yat/utility/vector.h"
28
29#include <algorithm>
30#include <cassert>
31#include <iostream>
32
33namespace theplu {
34namespace yat {
35namespace regression {
36
37  void Local::fit(const size_t step_size, const size_t nof_points)
38  {
39    if (step_size==0 || nof_points<3){
40      // Peter to Jari, throw exception?
41      std::cerr << "theplu::regression::Local "
42                << "Parameters invalid. Fitting ignored." << std::endl;
43      return;
44    }
45
46    size_t nof_fits=data_.size()/step_size;
47    x_= utility::vector(nof_fits);
48    y_predicted_ = utility::vector(x_.size());
49    y_err_ = utility::vector(x_.size());
50    sort(data_.begin(), data_.end());
51
52    // coying data to 2 utility vectors ONCE to use views from
53    utility::vector x(data_.size());
54    utility::vector y(data_.size());
55    for (size_t j=0; j<x.size(); j++){
56      x(j)=data_[j].first;
57      y(j)=data_[j].second;
58    }
59
60    // looping over regression points and perform local regression
61    for (size_t i=0; i<nof_fits; i++) {
62      size_t max_index = static_cast<size_t>( (i+0.5)*step_size );
63      size_t min_index;
64      double width; // distance from middle of windo to border of window
65      double x_mid; // middle of window
66      // right border case
67      if (max_index > data_.size()-1){
68        min_index = max_index - nof_points + 1;
69        max_index = data_.size()-1;
70        width = ( (( x(max_index)-x(0) )*(nof_points-1)) / 
71                  ( 2*(max_index-min_index)) );
72        x_mid = x(min_index)+width;
73      }
74      // normal middle case
75      else if (max_index > nof_points-1){
76        min_index = max_index - nof_points + 1;
77        width = (x(max_index)-x(min_index))/2;
78        x_mid = x(min_index)+width;
79      }
80      // left border case
81      else {
82        min_index = 0;
83        width = ( (( x(max_index)-x(0) )*(nof_points-1)) / 
84                  ( 2*(max_index-min_index)) );
85        x_mid = x(max_index)-width;
86      }
87      assert(min_index<data_.size());
88      assert(max_index<data_.size());
89                               
90      utility::vector x_local(x, min_index, max_index-min_index+1);
91      utility::vector y_local(y, min_index, max_index-min_index+1);
92
93      // calculating weights
94      utility::vector w(max_index-min_index+1);
95      for (size_t j=0; j<w.size(); j++)
96        w(j) = kernel_->weight( (x_local(j)- x_mid)/width );
97     
98      // fitting the regressor locally
99      regressor_->fit(x_local,y_local,w);
100      assert(i<y_predicted_.size());
101      assert(i<y_err_.size());
102      y_predicted_(i) = regressor_->predict(x(i*step_size));
103      y_err_(i) = regressor_->standard_error(x(i*step_size));
104    }
105  }
106
107  std::ostream& operator<<(std::ostream& os, const Local& r)
108  {
109    os << "# column 1: x\n"
110      << "# column 2: y\n"
111      << "# column 3: y_err\n";
112    for (size_t i=0; i<r.x().size(); i++) {
113      os << r.x()(i) << "\t" 
114         << r.y_predicted()(i) << "\t"
115         << r.y_err()(i) << "\n";
116    }   
117
118    return os;
119  }
120
121}}} // of namespaces regression, yat, and theplu
Note: See TracBrowser for help on using the repository browser.