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

Last change on this file since 865 was 865, checked in by Peter, 14 years ago

changing URL to http://trac.thep.lu.se/trac/yat

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