1 | // $Id: Smoother.cc 1487 2008-09-10 08:41:36Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2008 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 | |
---|
29 | namespace theplu { |
---|
30 | namespace yat { |
---|
31 | namespace 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::reset(void) |
---|
71 | { |
---|
72 | std::fill(density_.begin(), density_.end(), 0.0); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | const std::vector<double>& Smoother::value(void) const |
---|
77 | { |
---|
78 | return x_; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | std::ostream& operator<<(std::ostream& os,const Smoother& smoother) |
---|
83 | { |
---|
84 | os << "# column 1: x\n" |
---|
85 | << "# column 2: estimated density\n"; |
---|
86 | |
---|
87 | for (size_t i=0; i<smoother.value().size(); ++i) { |
---|
88 | os << smoother.value()[i] << "\t"; |
---|
89 | os << smoother.density()[i] << "\n"; |
---|
90 | } |
---|
91 | |
---|
92 | return os; |
---|
93 | } |
---|
94 | |
---|
95 | }}} // of namespace statistics, yat, and theplu |
---|