source: trunk/yat/statistics/Histogram.h @ 1203

Last change on this file since 1203 was 1203, checked in by Peter, 14 years ago

docs improvements in Histogram

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1#ifndef _theplu_yat_statistics_histogram_
2#define _theplu_yat_statistics_histogram_
3
4// $Id: Histogram.h 1203 2008-03-05 14:05:10Z peter $
5
6/*
7  Copyright (C) 2004 Jari Häkkinen
8  Copyright (C) 2005 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2006 Jari Häkkinen
10  Copyright (C) 2007 Peter Johansson
11
12  This file is part of the yat library, http://trac.thep.lu.se/yat
13
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 2 of the
17  License, or (at your option) any later version.
18
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with this program; if not, write to the Free Software
26  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27  02111-1307, USA.
28*/
29
30#include "AveragerWeighted.h"
31#include "yat/utility/iterator_traits.h"
32
33#include <string>
34#include <vector>
35
36namespace theplu {
37namespace yat {
38namespace statistics {
39
40  ///
41  /// @brief Histograms provide a convenient way of presenting the
42  /// distribution of a set of data.
43  ///
44  /// A histogram consists of a set of
45  /// bins which count the number of events falling into these
46  /// bins. Currently only one dimensional histograms with uniformly
47  /// spaced bins are supported.
48  ///
49  class Histogram
50  {
51  public:
52
53    ///
54    /// The default constructor.
55    ///
56    Histogram(void);
57
58    ///
59    /// The copy constructor.
60    ///
61    Histogram(const Histogram&);
62
63    ///
64    /// Construct a histogram object that covers \f$(xmin,xmax]\f$
65    /// with the bin spacing \f$(xmax-xmin)/n\f$.
66    ///
67    Histogram(const double xmin, const double xmax, const size_t n);
68
69    virtual ~Histogram(void);
70
71    ///
72    /// Update the histogram by adding \a weight to the bin whose
73    /// range contains the observation \a x. No bins are updated when
74    /// \a x lies outside the range of the histogram but the value is
75    /// added to the overall integral of the histogram.
76    ///
77    /// @short Add a data point to the histogram.
78    ///
79    /// @return 0 if \a x lies within the range of the histogram, -1
80    /// if \a x is smaller than the lower limit of the histogram, and
81    /// similarly, 1 is returned if \a x is greater than or equal to
82    /// the upper limit.
83    ///
84    int add(const double x,const double weight=1.0);
85
86    ///
87    /// Gives access to the AveragerWeighted object that keeps track of
88    /// average of all events presented to the histogram.
89    ///
90    /// @short Average of all events presented to the histogram.
91    ///
92    /// @return A const reference to an AveragerWeighted object.
93    ///
94    const AveragerWeighted& averager_all(void) const;
95
96    ///
97    /// Gives access to the AveragerWeighted object that keeps track of
98    /// average of events that fits within the histogram lower and
99    /// upper limits. This function is equivalent to averager().
100    ///
101    /// @short Average of events fitting within histogram.
102    ///
103    /// @return A const reference to an AveragerWeighted object.
104    ///
105    const AveragerWeighted& averager_histogram(void) const;
106
107    ///
108    /// @return The number of bins in the histogram
109    ///
110    size_t nof_bins(void) const;
111
112    ///
113    ///  There are two ways to normalize the counts.
114    ///
115    /// If choice is true: The normalized count is the count in a
116    /// bin divided by the total number of observations. In this
117    /// case the relative counts are normalized to sum to unity (
118    /// minus values outside histogram).  This is the intuitive case
119    /// where the height of the histogram bar represents the
120    /// proportion of the data in each class.
121    ///
122    /// If choice is false: The normalized count is the count in the
123    /// class divided by the number of observations times the bin
124    /// width. For this normalization, the area (or integral) under
125    /// the histogram is equal to unity (minus the missing area
126    /// corresponding to counts outside histogram). From a
127    /// probabilistic point of view, this normalization results in a
128    /// relative histogram that is most akin to the probability
129    /// density function If you want to overlay a probability density
130    /// on top of the histogram, use this normalization. Although this
131    /// normalization is less intuitive (relative frequencies greater
132    /// than 1 are quite permissible), it is the appropriate
133    /// normalization if you are using the histogram to model a
134    /// probability density function.
135    ///
136    /// @short Normalizing the histogram
137    ///
138    void normalize(bool choice = true);
139
140    ///
141    /// @return The value in the middle of bin \a k.
142    ///
143    /// @note No check is done that \a k is within the size of the
144    /// histogram.
145    ///
146    double observation_value(const size_t k) const;
147
148    ///
149    /// Set everyting to default values, here it means that everything
150    /// is set to zero except the boundary values that are kept.
151    ///
152    void reset(void);
153
154    ///
155    /// @return The width of the bins in the histogram.
156    ///
157    double spacing(void) const;
158
159    ///
160    /// @return The histogram upper boundary.
161    ///
162    /// @note The upper boundary value is outside the histogram.
163    ///
164    double xmax(void) const;
165
166    ///
167    /// @return The histogram lower boundary.
168    ///
169    /// @note The lower boundary value is inside the histogram.
170    ///
171    double xmin(void) const;
172
173    ///
174    /// @return The count of bin \a k in the histogram.
175    ///
176    double operator[](size_t k) const;
177
178    ///
179    /// The assignment operator
180    ///
181    const Histogram& operator=(const Histogram&);
182
183  private:
184    // Returns zero if outside boundaries
185    size_t bin(double d);
186
187    std::vector<double> histogram_;
188    double xmax_;
189    double xmin_;
190    statistics::AveragerWeighted sum_all_;      // average of all data
191    statistics::AveragerWeighted sum_histogram_;// average of data in histogram
192  };
193
194  /**
195     Add a range [first, last) of values to Histogram.
196   */
197  template<typename ForwardIterator>
198  void add(Histogram& h, 
199           ForwardIterator first, ForwardIterator last)
200  {
201    while (first!=last) {
202      h.add(utility::iterator_traits<ForwardIterator>().data(),
203            utility::iterator_traits<ForwardIterator>().weight());
204      ++first;
205    }
206  }
207
208///
209/// The Histogram output operator
210///
211std::ostream& operator<<(std::ostream& s,const Histogram&);
212
213}}} // of namespace statistics, yat, and theplu
214
215#endif
Note: See TracBrowser for help on using the repository browser.