1 | // $Id: Histogram.cc 1275 2008-04-11 06:10:12Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004 Jari Häkkinen |
---|
5 | Copyright (C) 2005 Peter Johansson |
---|
6 | Copyright (C) 2006 Jari Häkkinen |
---|
7 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2008 Peter Johansson |
---|
9 | |
---|
10 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
11 | |
---|
12 | The yat library is free software; you can redistribute it and/or |
---|
13 | modify it under the terms of the GNU General Public License as |
---|
14 | published by the Free Software Foundation; either version 2 of the |
---|
15 | License, or (at your option) any later version. |
---|
16 | |
---|
17 | The yat library is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
20 | General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License |
---|
23 | along with this program; if not, write to the Free Software |
---|
24 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
25 | 02111-1307, USA. |
---|
26 | */ |
---|
27 | |
---|
28 | #include "Histogram.h" |
---|
29 | |
---|
30 | #include <cmath> |
---|
31 | #include <fstream> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace statistics { |
---|
36 | |
---|
37 | |
---|
38 | Histogram::Histogram(void) |
---|
39 | : xmax_(0), xmin_(0), sum_all_(), sum_histogram_() |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | Histogram::Histogram(const Histogram& b) |
---|
45 | { |
---|
46 | *this=b; |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | Histogram::Histogram(const double min, const double max, const size_t n) |
---|
51 | : histogram_(std::vector<double>(n,0.0)), |
---|
52 | xmax_(max), xmin_(min), |
---|
53 | sum_all_(), sum_histogram_() |
---|
54 | { |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | Histogram::~Histogram(void) |
---|
59 | { |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | int Histogram::add(const double x, const double w) |
---|
64 | { |
---|
65 | sum_all_.add(x,w); |
---|
66 | if (x<xmin_) |
---|
67 | return -1; |
---|
68 | else if (x>=xmax_) |
---|
69 | return 1; |
---|
70 | |
---|
71 | sum_histogram_.add(x,w); |
---|
72 | histogram_[bin(x)] += w; |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | const AveragerWeighted& Histogram::averager_all(void) const |
---|
78 | { |
---|
79 | return sum_all_; |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | const AveragerWeighted& Histogram::averager_histogram(void) const |
---|
84 | { |
---|
85 | return sum_histogram_; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | size_t Histogram::bin(double d) |
---|
90 | { |
---|
91 | return (((d<xmin_) || (d>xmax_)) ? 0 : |
---|
92 | static_cast<size_t>(floor((d-xmin_)/spacing() ))); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | size_t Histogram::nof_bins(void) const |
---|
97 | { |
---|
98 | return histogram_.size(); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | void Histogram::normalize(bool choice) |
---|
103 | { |
---|
104 | double scale_factor; |
---|
105 | if (choice) |
---|
106 | scale_factor = sum_all_.sum_w(); |
---|
107 | else |
---|
108 | scale_factor = sum_all_.sum_w()*spacing(); |
---|
109 | for (size_t i=0; i<histogram_.size(); i++) |
---|
110 | histogram_[i]/=scale_factor; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | double Histogram::observation_value(const size_t k) const |
---|
115 | { |
---|
116 | return xmin_+spacing()*(k+0.5); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | void Histogram::reset(void) |
---|
121 | { |
---|
122 | for (size_t i=0; i<histogram_.size(); i++) |
---|
123 | histogram_[i]=0; |
---|
124 | sum_all_.reset(); |
---|
125 | sum_histogram_.reset(); |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | double Histogram::spacing(void) const |
---|
130 | { |
---|
131 | return (xmax_-xmin_)/nof_bins(); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | double Histogram::xmax(void) const |
---|
136 | { |
---|
137 | return xmax_; |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | double Histogram::xmin(void) const |
---|
142 | { |
---|
143 | return xmin_; |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | double Histogram::operator[](size_t k) const |
---|
148 | { |
---|
149 | return histogram_[k]; |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | const Histogram& Histogram::operator=(const Histogram& b) |
---|
154 | { |
---|
155 | if (this==&b) |
---|
156 | return *this; |
---|
157 | histogram_=b.histogram_; |
---|
158 | xmax_=b.xmax_; |
---|
159 | xmin_=b.xmin_; |
---|
160 | sum_all_=b.sum_all_; |
---|
161 | sum_histogram_=b.sum_histogram_; |
---|
162 | return *this; |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | std::ostream& operator<<(std::ostream& s,const Histogram& histogram) |
---|
167 | { |
---|
168 | s << "# histogram min : " << histogram.xmin() << '\n'; |
---|
169 | s << "# histogram max : " << histogram.xmax() << '\n'; |
---|
170 | s << "# number of bins: " << histogram.nof_bins() << '\n'; |
---|
171 | s << "# nof points in histogram : " |
---|
172 | << histogram.averager_histogram().sum_w() << '\n'; |
---|
173 | s << "# nof points in total: " |
---|
174 | << histogram.averager_all().sum_w() << '\n'; |
---|
175 | s << "# column 1: center of observation bin\n" |
---|
176 | << "# column 2: frequency\n"; |
---|
177 | |
---|
178 | for (size_t i=0; i<histogram.nof_bins(); i++) { |
---|
179 | s.width(12); |
---|
180 | s << histogram.observation_value(i); |
---|
181 | s.width(12); |
---|
182 | s << histogram[i] << '\n'; |
---|
183 | } |
---|
184 | |
---|
185 | return s; |
---|
186 | } |
---|
187 | |
---|
188 | }}} // of namespace statistics, yat, and theplu |
---|