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