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

Last change on this file since 718 was 718, checked in by Jari Häkkinen, 15 years ago

Addresses #170.

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