source: trunk/yat/statistics/Histogram.cc @ 2034

Last change on this file since 2034 was 2034, checked in by Peter, 12 years ago

fixed typos

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1// $Id: Histogram.cc 2034 2009-08-20 18:02:25Z 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
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "Histogram.h"
26
27#include <algorithm>
28#include <cassert>
29#include <cmath>
30#include <functional>
31#include <ostream>
32
33namespace theplu {
34namespace yat {
35namespace 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  const Histogram& Histogram::operator+=(const Histogram& rhs)
167  {
168    assert(histogram_.size()==rhs.histogram_.size());
169    std::transform(histogram_.begin(), histogram_.end(),
170                   rhs.histogram_.begin(), histogram_.begin(),
171                   std::plus<double>());
172    assert(xmax_==rhs.xmax_);
173    assert(xmin_==rhs.xmin_);
174    sum_all_ += rhs.sum_all_;
175    sum_histogram_ += rhs.sum_histogram_;
176    return *this;
177  }
178
179
180  std::ostream& operator<<(std::ostream& s,const Histogram& histogram)
181  {
182    s << "# histogram min : " << histogram.xmin() << '\n';
183    s << "# histogram max : " << histogram.xmax() << '\n';
184    s << "# number of bins: " << histogram.nof_bins() << '\n';
185    s << "# nof points in histogram : " 
186      << histogram.averager_histogram().sum_w() << '\n';
187    s << "# nof points in total:      " 
188      << histogram.averager_all().sum_w() << '\n';
189    s << "# column 1: center of observation bin\n"
190      << "# column 2: frequency\n";
191
192    for (size_t i=0; i<histogram.nof_bins(); i++) {
193      s.width(12);
194      s << histogram.observation_value(i);
195      s.width(12);
196      s << histogram[i] << '\n';
197    }
198
199    return s;
200  }
201
202}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.