mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
histogramUniform.hpp
Go to the documentation of this file.
1/** \file histogramUniform.hpp
2 * \author Jared R. Males
3 * \brief Header for the std::vector utilities
4 * \ingroup gen_math_files
5 *
6 */
7
8//***********************************************************************//
9// Copyright 2015, 2016, 2017 Jared R. Males (jaredmales@gmail.com)
10//
11// This file is part of mxlib.
12//
13// mxlib is free software: you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation, either version 3 of the License, or
16// (at your option) any later version.
17//
18// mxlib is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25//***********************************************************************//
26
27#ifndef math_histogramUniform_hpp
28#define math_histogramUniform_hpp
29
30namespace mx
31{
32namespace math
33{
34
35/// A histogram with uniform bin spacing
36/** Calculates the frequency in bins with uniform spacing.
37 *
38 * \tparam realT the real data type
39 *
40 * \ingroup gen_math
41 */
42template <typename realT>
44{
45 protected:
46 realT m_min{ 0 }; ///< The mininum bin location
47 realT m_max{ 0 }; ///< The maximum bin location
48 realT m_width{ 0 }; ///< The bin width
49
50 std::vector<realT> m_freqs; ///< The frequencies, one for each bin.
51
52 public:
53
54 /// Default c'tor, does not allocate.
55 /** Must call setup before use */
57 {
58 }
59
60 /// C'tor to setup the histogram, performing allocations.
61 histogramUniform( realT mn, ///< [in] the minimum bin location
62 realT mx, ///< [in] the maximum bin location
63 realT w ///< [in] the bin width
64 )
65 : m_min( mn ), m_max( mx ), m_width( w )
66 {
67 reset();
68 }
69
70 /// C'tor to setup the histogram, performing allocations, and accumulate from a vector of values.
71 /**
72 * Optionally normalizes the histogram
73 */
74 histogramUniform( realT mn, ///< [in] the minimum bin location
75 realT mx, ///< [in] the maximum bin location
76 realT w, ///< [in] the bin width
77 const std::vector<realT> &vals, ///< [in] The vector of values to accumulate
78 bool normalize = false ///< [in] [opt] whether or not to normalize after accumulation
79 )
80 : m_min( mn ), m_max( mx ), m_width( w )
81 {
82 reset();
83 accum( vals );
84 if( normalize )
85 {
86 this->normalize();
87 }
88 }
89
90 /// Setup the histogram, performing allocations.
91 void setup( realT mn, ///< [in] the new minimum bin location
92 realT mx, ///< [in] the new maximum bin location
93 realT w ///< [in] the bin width
94 )
95 {
96 m_min = mn;
97 m_max = mx;
98 m_width = w;
99
100 reset();
101 }
102
103 /// Resize and 0 the frequency vector. Assumes m_min, m_max, and m_width are set.
104 void reset()
105 {
106 m_freqs.resize( ( m_max - m_min ) / m_width + 1, 0 );
107 }
108
109 /// Accumulate a value in the appropriate bin.
110 void accum( const realT &val /**< [in] The value to accumulate */ )
111 {
112 int i = ( val - m_min ) / m_width;
113 if( i < 0 )
114 {
115 i = 0;
116 }
117 if( i >= m_freqs.size() )
118 {
119 i = m_freqs.size() - 1;
120 }
121
122 ++m_freqs[i];
123 }
124
125 /// Accumulate a vector of values.
126 void accum( const std::vector<realT> &vals /**< [in] The vector of values to accumulate */ )
127 {
128 for( int i = 0; i < vals.size(); ++i )
129 {
130 accum( vals[i] );
131 }
132 }
133
134 /// Get the frequency in the i-th bin.
135 realT freq( int i /**< [in] the bin number */ )
136 {
137 return m_freqs[i]; ///\returns the current value of m_freqs[i].
138 }
139
140 /// Get the number of bins
141 int bins()
142 {
143 return m_freqs.size(); ///\returns the size of the frequency vector.
144 }
145
146 /// Get the value of the left-edge of the i-th bin.
147 realT binLeft( int i /**< [in] the bin number */ )
148 {
149 return m_min + i * m_width; ///\returns the value of the left-edge of the i-th bin.
150 }
151
152 /// Get the value of the middle of the i-th bin.
153 realT binMid( int i /**< [in] the bin number */ )
154 {
155 return m_min + i * m_width + 0.5 * m_width; ///\returns the value of the middle of the i-th bin.
156 }
157
158 /// Get the value of the right edge of the i-th bin.
159 realT binRight( int i /**< [in] the bin number */ )
160 {
161 return m_min + i * m_width + 1.0 * m_width; ///\returns the value of the right edge of the i-th bin.
162 }
163
164 /// Normalize the current frequencies so that the integral over all bins is 1.
165 /** This normalizes the histogram so that it is a probability distribution, such that the sum
166 * \f$ \sum_i P_i \Delta x = 1 \f$ where \f$ \Delta x \f$ is the bin width.
167 */
168 void normalize( int excludeTop = 0 /**< [in] [optional] specifies a number of bins at the
169 top of the range to exclude from the sum */
170 )
171 {
172 realT sum = 0;
173
174 for( int i = 0; i < m_freqs.size() - excludeTop; ++i )
175 {
176 sum += m_freqs[i];
177 }
178
179 for( int i = 0; i < m_freqs.size(); ++i )
180 {
181 m_freqs[i] /= ( sum * m_width );
182 }
183 }
184};
185
186} // namespace math
187} // namespace mx
188
189#endif // math_histogramUniform_hpp
A histogram with uniform bin spacing.
histogramUniform(realT mn, realT mx, realT w)
C'tor to setup the histogram, performing allocations.
histogramUniform(realT mn, realT mx, realT w, const std::vector< realT > &vals, bool normalize=false)
C'tor to setup the histogram, performing allocations, and accumulate from a vector of values.
realT binRight(int i)
Get the value of the right edge of the i-th bin.
void normalize(int excludeTop=0)
Normalize the current frequencies so that the integral over all bins is 1.
void accum(const realT &val)
Accumulate a value in the appropriate bin.
realT freq(int i)
Get the frequency in the i-th bin.
void setup(realT mn, realT mx, realT w)
Setup the histogram, performing allocations.
void accum(const std::vector< realT > &vals)
Accumulate a vector of values.
realT m_min
The mininum bin location.
std::vector< realT > m_freqs
The frequencies, one for each bin.
realT binMid(int i)
Get the value of the middle of the i-th bin.
int bins()
Get the number of bins.
realT m_max
The maximum bin location.
realT binLeft(int i)
Get the value of the left-edge of the i-th bin.
histogramUniform()
Default c'tor, does not allocate.
void reset()
Resize and 0 the frequency vector. Assumes m_min, m_max, and m_width are set.
constexpr floatT six_fifths()
Return 6/5 in the specified precision.
The mxlib c++ namespace.
Definition mxlib.hpp:37