numeric
Histogram.hpp
Go to the documentation of this file.
1 #ifndef __NUMERIC_HISTOGRAM_HPP__
2 #define __NUMERIC_HISTOGRAM_HPP__
3 
4 #include <algorithm>
5 
6 namespace numeric
7 {
8 
9 template <class T>
10 struct Buckets
11 {
12  int count;
13  double min_val, max_val;
14  std::vector<T> buckets;
15 
16  Buckets( int count, double min_val, double max_val, const T& initial = T() )
17  : count( count ), min_val( min_val ), max_val( max_val ), buckets( count, initial )
18  {
19  assert( count > 0 );
20  assert( min_val < max_val );
21  }
22 
23  const T& operator[]( size_t idx ) const
24  {
25  return buckets[idx];
26  }
27 
28  T& operator[]( size_t idx )
29  {
30  return buckets[idx];
31  }
32 
33  size_t getIndex( double value ) const
34  {
35  int idx = (value - min_val) / (max_val - min_val) * count;
36  return std::min( count-1, std::max( 0, idx ) );
37  }
38 
39  T& get( double value )
40  {
41  return buckets[ getIndex( value ) ];
42  }
43 
44  double getBucketWidth() const
45  {
46  return (max_val - min_val)/count;
47  }
48 
49  double getUpperBound( size_t idx ) const
50  {
51  return min_val + ( (idx+1) * getBucketWidth() );
52  }
53 
54  double getLowerBound( size_t idx ) const
55  {
56  return min_val + ( (idx) * getBucketWidth() );
57  }
58 
59  double getCenter( size_t idx ) const
60  {
61  return min_val + ( (idx+0.5) * getBucketWidth() );
62  }
63 
64  size_t size() const
65  {
66  return buckets.size();
67  }
68 
69 };
70 
86 struct Histogram : public Buckets<size_t>
87 {
88  size_t n;
89 
99  Histogram( int count, double min_val, double max_val )
100  : Buckets<size_t>( count, min_val, max_val ), n(0)
101  {
102  }
103 
111  void update( double value )
112  {
113  ++n;
114  ++get( value );
115  }
116 
122  double getRelative( size_t idx ) const
123  {
124  return operator[]( idx ) / getBucketWidth() / (double)total();
125  }
126 
130  size_t total() const
131  {
132  return n;
133  }
134 };
135 
136 }
137 
138 #endif
T & operator[](size_t idx)
Definition: Histogram.hpp:28
Histogram class, that splits an interval into n regular bins, and counts how often a value is added t...
Definition: Histogram.hpp:86
double getBucketWidth() const
Definition: Histogram.hpp:44
double getRelative(size_t idx) const
Definition: Histogram.hpp:122
int count
Definition: Histogram.hpp:12
double getCenter(size_t idx) const
Definition: Histogram.hpp:59
Definition: Circle.hpp:6
std::vector< T > buckets
Definition: Histogram.hpp:14
const T & operator[](size_t idx) const
Definition: Histogram.hpp:23
Histogram(int count, double min_val, double max_val)
Constructs a histogram where the number of update() calls with a value that all into a bin are counte...
Definition: Histogram.hpp:99
Buckets(int count, double min_val, double max_val, const T &initial=T())
Definition: Histogram.hpp:16
double getLowerBound(size_t idx) const
Definition: Histogram.hpp:54
double getUpperBound(size_t idx) const
Definition: Histogram.hpp:49
size_t n
Definition: Histogram.hpp:88
Definition: Histogram.hpp:10
double min_val
Definition: Histogram.hpp:13
size_t size() const
Definition: Histogram.hpp:64
double max_val
Definition: Histogram.hpp:13
size_t getIndex(double value) const
Definition: Histogram.hpp:33
size_t total() const
Definition: Histogram.hpp:130
void update(double value)
Definition: Histogram.hpp:111