source: trunk/yat/statistics/Smoother.cc

Last change on this file was 2919, checked in by Peter, 9 years ago

update copyright years

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