source: trunk/yat/statistics/Histogram.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 Author Date Id Revision
File size: 5.0 KB
Line 
1// $Id: Histogram.cc 2919 2012-12-19 06:54:23Z peter $
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, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2011, 2012 Peter Johansson
9
10  This file is part of the yat library, http://dev.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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <config.h>
27
28#include "Histogram.h"
29
30#include <yat/utility/utility.h>
31
32#include <algorithm>
33#include <cassert>
34#include <cmath>
35#include <functional>
36#include <istream>
37#include <ostream>
38
39namespace theplu {
40namespace yat {
41namespace statistics {
42
43
44  Histogram::Histogram(void)
45    : xmax_(0), xmin_(0), sum_all_(), sum_histogram_()
46  {
47  }
48
49
50  Histogram::Histogram(std::istream& is)
51  {
52    std::string line;
53    getline(is, line, ':');
54    getline(is, line, '\n');
55    xmin_ = utility::convert<double>(line);
56    getline(is, line, ':');
57    getline(is, line, '\n');
58    xmax_ = utility::convert<double>(line);
59    getline(is, line, ':');
60    getline(is, line, '\n');
61    size_t n = utility::convert<size_t>(line);
62    histogram_.resize(n);
63    getline(is, line, '\n');
64    getline(is, line, ':');
65    getline(is, line, '\n');
66    double total = utility::convert<double>(line);
67    getline(is, line, '\n');
68    getline(is, line, '\n');
69    std::vector<std::vector<double> > data;
70    utility::load(is, data, '\0', '\n');
71    for (size_t i=0; i<histogram_.size(); ++i) {
72      assert(data[i].size()==2);
73      add(data[i][0], data[i][1]);
74    }
75    add(xmax_, total - averager_all().sum_w());
76  }
77
78
79  Histogram::Histogram(const Histogram& b)
80  {
81    *this=b;
82  }
83
84
85  Histogram::Histogram(const double min, const double max, const size_t n)
86    : histogram_(std::vector<double>(n,0.0)),
87      xmax_(max), xmin_(min),
88      sum_all_(), sum_histogram_()
89  {
90  }
91
92
93  Histogram::~Histogram(void)
94  {
95  }
96
97
98  int Histogram::add(const double x, const double w)
99  {
100    sum_all_.add(x,w);
101    if (x<xmin_)
102      return -1;
103    else if (x>=xmax_)
104      return 1;
105
106    sum_histogram_.add(x,w);
107    histogram_[bin(x)] += w;
108    return 0;
109  }
110
111
112  const AveragerWeighted& Histogram::averager_all(void) const
113  {
114    return sum_all_;
115  }
116
117
118  const AveragerWeighted& Histogram::averager_histogram(void) const
119  {
120    return sum_histogram_;
121  }
122
123
124  size_t Histogram::bin(double d)
125  {
126    return (((d<xmin_) || (d>xmax_)) ? 0 :
127            static_cast<size_t>(floor((d-xmin_)/spacing() )));
128  }
129
130
131  size_t Histogram::nof_bins(void) const
132  {
133    return histogram_.size();
134  }
135
136
137  void Histogram::normalize(bool choice)
138  {
139    if (choice) 
140      rescale(1.0/sum_all_.sum_w());
141    else
142      rescale(1.0/sum_all_.sum_w()/spacing());
143  }
144
145
146  double Histogram::observation_value(const size_t k) const
147  {
148    return xmin_+spacing()*(k+0.5);
149  }
150
151
152  void Histogram::rescale(double factor)
153  {
154    for (size_t i=0; i<histogram_.size(); i++)
155      histogram_[i]*=factor;
156  }
157
158
159  void Histogram::reset(void)
160  {
161    for (size_t i=0; i<histogram_.size(); i++)
162      histogram_[i]=0;
163    sum_all_.reset();
164    sum_histogram_.reset();
165  }
166
167
168  double Histogram::spacing(void) const
169  {
170    return (xmax_-xmin_)/nof_bins();
171  }
172
173
174  double Histogram::xmax(void) const
175  {
176    return xmax_;
177  }
178
179
180  double Histogram::xmin(void) const
181  {
182    return xmin_;
183  }
184
185
186  double Histogram::operator[](size_t k) const
187  {
188    return histogram_[k];
189  }
190
191
192  const Histogram& Histogram::operator=(const Histogram& b)
193  {
194    if (this==&b)
195      return *this;
196    histogram_=b.histogram_;
197    xmax_=b.xmax_;
198    xmin_=b.xmin_;
199    sum_all_=b.sum_all_;
200    sum_histogram_=b.sum_histogram_;
201    return *this;
202  }
203
204
205  const Histogram& Histogram::operator+=(const Histogram& rhs)
206  {
207    assert(histogram_.size()==rhs.histogram_.size());
208    std::transform(histogram_.begin(), histogram_.end(),
209                   rhs.histogram_.begin(), histogram_.begin(),
210                   std::plus<double>());
211    assert(xmax_==rhs.xmax_);
212    assert(xmin_==rhs.xmin_);
213    sum_all_ += rhs.sum_all_;
214    sum_histogram_ += rhs.sum_histogram_;
215    return *this;
216  }
217
218
219  std::ostream& operator<<(std::ostream& s,const Histogram& histogram)
220  {
221    s << "# histogram min : " << histogram.xmin() << '\n';
222    s << "# histogram max : " << histogram.xmax() << '\n';
223    s << "# number of bins: " << histogram.nof_bins() << '\n';
224    s << "# nof points in histogram : " 
225      << histogram.averager_histogram().sum_w() << '\n';
226    s << "# nof points in total:      " 
227      << histogram.averager_all().sum_w() << '\n';
228    s << "# column 1: center of observation bin\n"
229      << "# column 2: frequency\n";
230
231    for (size_t i=0; i<histogram.nof_bins(); i++) {
232      s.width(12);
233      s << histogram.observation_value(i);
234      s.width(12);
235      s << histogram[i] << '\n';
236    }
237
238    return s;
239  }
240
241}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.