source: trunk/yat/statistics/Smoother.cc @ 2039

Last change on this file since 2039 was 2039, checked in by Peter, 12 years ago

closes #554 - added function rescale in Smoother

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1// $Id: Smoother.cc 2039 2009-08-26 14:42:57Z peter $
2
3/*
4  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
5
6  This file is part of the yat library, http://dev.thep.lu.se/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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "Smoother.h"
23#include "yat/regression/Kernel.h"
24
25#include <algorithm>
26#include <cassert>
27#include <ostream>
28
29namespace theplu {
30namespace yat {
31namespace statistics {
32
33  Smoother::Smoother(const regression::Kernel& kernel, double width,
34                     const std::vector<double>& values)
35    : kernel_(kernel), width_(width), x_(values)
36  {
37    density_.resize(values.size(), 0.0);
38    assert(density_.size()==x_.size());
39  }
40
41  Smoother::Smoother(const regression::Kernel& kernel, double width,
42                     double min, double max, size_t n)
43    : density_(std::vector<double>(n)), kernel_(kernel), width_(width)
44  {
45    x_.reserve(n);
46    double step_size = (max-min)/n;
47    for (double x=min; x_.size()<n; x+=step_size)
48      x_.push_back(x);
49    assert(x_.back()==max);
50    assert(density_.size()==x_.size());
51  }
52
53
54  void Smoother::add(const double x, const double w)
55  {
56    if (w==0)
57      return;
58    // Peter, we should probably do something clever here to avoid x+=0
59    for (size_t i=0; i<x_.size(); ++i)
60      density_[i] += w*kernel_( (x-x_[i])/width_);
61  }
62
63 
64  const std::vector<double>& Smoother::density(void) const
65  {
66    return density_;
67  }
68
69
70  void Smoother::rescale(double factor)
71  {
72    for (size_t i=0; i<x_.size(); ++i)
73      x_[i] *= factor;
74  }
75
76
77  void Smoother::reset(void)
78  {
79    std::fill(density_.begin(), density_.end(), 0.0);
80  }
81
82
83  const std::vector<double>& Smoother::value(void) const
84  {
85    return x_;
86  }
87
88
89  std::ostream& operator<<(std::ostream& os,const Smoother& smoother)
90  {
91    os << "# column 1: x\n"
92       << "# column 2: estimated density\n";
93
94    for (size_t i=0; i<smoother.value().size(); ++i) {
95      os << smoother.value()[i] << "\t";
96      os << smoother.density()[i] << "\n";
97    }
98
99    return os;
100  }
101
102}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.