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

Last change on this file since 767 was 767, checked in by Peter, 15 years ago

Fixes #65

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