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

Last change on this file since 729 was 729, checked in by Peter, 15 years ago

Fixes #159. Also removed some inlines in OneDimensionalWeighted? by adding source file. Refs #81

  • 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 729 2007-01-05 16:00:15Z peter $
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  Local::Local(OneDimensionalWeighted& r, Kernel& k)
38    : kernel_(&k), regressor_(&r)
39  {
40  }
41
42  Local::~Local(void)
43  {
44  }
45
46  void Local::add(const double x, const double y)
47  {
48    data_.push_back(std::make_pair(x,y));
49  }
50
51  void Local::fit(const size_t step_size, const size_t nof_points)
52  {
53    if (step_size==0 || nof_points<3){
54      // Peter to Jari, throw exception?
55      std::cerr << "theplu::regression::Local "
56                << "Parameters invalid. Fitting ignored." << std::endl;
57      return;
58    }
59
60    size_t nof_fits=data_.size()/step_size;
61    x_= utility::vector(nof_fits);
62    y_predicted_ = utility::vector(x_.size());
63    y_err_ = 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_->weight( (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.