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

Last change on this file since 1310 was 1310, checked in by Peter, 13 years ago

closes #359

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