source: trunk/yat/statistics/Local.cc @ 676

Last change on this file since 676 was 675, checked in by Jari Häkkinen, 16 years ago

References #83. Changing project name to yat. Compilation will fail in this revision.

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