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

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

Addresses #65 and #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1// $Id: Local.cc 703 2006-12-18 00:47:44Z 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  Local::Local(OneDimensionalWeighted& r, Kernel& k)
38    : kernel_(&k), regressor_(&r)
39  {
40  }
41
42  Local::~Local(void)
43  {
44  }
45
46  void Local::fit(const size_t step_size, const size_t nof_points)
47  {
48    if (step_size==0 || nof_points<3){
49      // Peter to Jari, throw exception?
50      std::cerr << "theplu::regression::Local "
51                << "Parameters invalid. Fitting ignored." << std::endl;
52      return;
53    }
54
55    size_t nof_fits=data_.size()/step_size;
56    x_= utility::vector(nof_fits);
57    y_predicted_ = utility::vector(x_.size());
58    y_err_ = utility::vector(x_.size());
59    sort(data_.begin(), data_.end());
60
61    // coying data to 2 utility vectors ONCE to use views from
62    utility::vector x(data_.size());
63    utility::vector y(data_.size());
64    for (size_t j=0; j<x.size(); j++){
65      x(j)=data_[j].first;
66      y(j)=data_[j].second;
67    }
68
69    // looping over regression points and perform local regression
70    for (size_t i=0; i<nof_fits; i++) {
71      size_t max_index = static_cast<size_t>( (i+0.5)*step_size );
72      size_t min_index;
73      double width; // distance from middle of windo to border of window
74      double x_mid; // middle of window
75      // right border case
76      if (max_index > data_.size()-1){
77        min_index = max_index - nof_points + 1;
78        max_index = data_.size()-1;
79        width = ( (( x(max_index)-x(0) )*(nof_points-1)) / 
80                  ( 2*(max_index-min_index)) );
81        x_mid = x(min_index)+width;
82      }
83      // normal middle case
84      else if (max_index > nof_points-1){
85        min_index = max_index - nof_points + 1;
86        width = (x(max_index)-x(min_index))/2;
87        x_mid = x(min_index)+width;
88      }
89      // left border case
90      else {
91        min_index = 0;
92        width = ( (( x(max_index)-x(0) )*(nof_points-1)) / 
93                  ( 2*(max_index-min_index)) );
94        x_mid = x(max_index)-width;
95      }
96      assert(min_index<data_.size());
97      assert(max_index<data_.size());
98                               
99      utility::vector x_local(x, min_index, max_index-min_index+1);
100      utility::vector y_local(y, min_index, max_index-min_index+1);
101
102      // calculating weights
103      utility::vector w(max_index-min_index+1);
104      for (size_t j=0; j<w.size(); j++)
105        w(j) = kernel_->weight( (x_local(j)- x_mid)/width );
106     
107      // fitting the regressor locally
108      regressor_->fit(x_local,y_local,w);
109      assert(i<y_predicted_.size());
110      assert(i<y_err_.size());
111      y_predicted_(i) = regressor_->predict(x(i*step_size));
112      y_err_(i) = regressor_->standard_error(x(i*step_size));
113    }
114  }
115
116  std::ostream& operator<<(std::ostream& os, const Local& r)
117  {
118    os << "# column 1: x\n"
119      << "# column 2: y\n"
120      << "# column 3: y_err\n";
121    for (size_t i=0; i<r.x().size(); i++) {
122      os << r.x()(i) << "\t" 
123         << r.y_predicted()(i) << "\t"
124         << r.y_err()(i) << "\n";
125    }   
126
127    return os;
128  }
129
130}}} // of namespaces regression, yat, and theplu
Note: See TracBrowser for help on using the repository browser.