21 #ifndef KJB_PROB_HISTOGRAM
22 #define KJB_PROB_HISTOGRAM
38 #include <boost/bind.hpp>
50 std::map<double, int> m_hist;
58 template<
class InputIterator>
64 create_continuous(first, last);
68 create_discrete(first, last);
92 const std::map<double, int>&
as_map()
const
98 template<
class InputIterator>
99 void create_continuous(InputIterator first, InputIterator last);
101 template<
class InputIterator>
102 void create_discrete(InputIterator first, InputIterator last);
107 return std::make_pair(d, i);
110 template<
class InputIterator>
111 void Histogram::create_continuous(InputIterator first, InputIterator last)
113 double mn = *std::min_element(first, last);
114 double mx = *std::max_element(first, last);
116 m_bin_size = (mx - mn) / m_num_bins;
118 std::generate_n(std::insert_iterator<std::map<double, int> >(m_hist, m_hist.begin()), m_num_bins,
120 boost::bind<double>(Increase_by<double>(mn, m_bin_size)),
123 for(InputIterator p = first; p != last; p++)
125 std::map<double, int>::iterator pair_p = m_hist.upper_bound(*p);
131 template<
class InputIterator>
132 void Histogram::create_discrete(InputIterator first, InputIterator last)
134 for(InputIterator p = first; p != last; p++)
136 std::map<double, int>::iterator pair_p = m_hist.find(*p);
137 if(pair_p != m_hist.end())
143 m_hist.insert(std::make_pair(*p, 1));
147 m_num_bins = m_hist.size();
159 template<
class InputIterator>
166 const std::pair<double, double>&
range_1,
167 const std::pair<double, double>&
range_2
168 ) : m_num_bins_1(num_bins_1),
169 m_num_bins_2(num_bins_2),
174 create_map(first, last);
192 const std::pair<double, double>&
range_1()
const;
193 const std::pair<double, double>&
range_2()
const;
198 const std::map<double, std::map<double, int> >&
as_map()
const;
209 std::pair<double, double>&
range_1();
210 std::pair<double, double>&
range_2();
237 std::pair<double, double> m_range_1;
238 std::pair<double, double> m_range_2;
239 std::map<double, std::map<double, int> > m_hist;
240 mutable Matrix m_hist_mat;
241 mutable bool m_updated;
244 template<
class InputIterator>
245 void create_map (InputIterator first, InputIterator last);
246 void compute_matrix()
const;
250 double chi_square(
const Histogram_2d& hist_1,
const Histogram_2d& hist_2);
252 double chi_square(
const Matrix& h1,
const Matrix& h2);
318 template<
typename InputIterator>
319 void Histogram_2d::create_map
325 double d1_range = m_range_1.second - m_range_1.first;
326 double d2_range = m_range_2.second - m_range_2.first;
327 assert(d1_range >= 0.0 && d2_range >= 0.0);
328 m_bin_size_1 = d1_range/(m_num_bins_1 - 1);
329 m_bin_size_2 = d2_range/(m_num_bins_2 - 1);
333 double d1 = m_range_1.first;
334 for(
size_t i = 0;
i < m_num_bins_1;
i++, d1 += m_bin_size_1)
336 double d2 = m_range_2.first;
337 for(
size_t j = 0; j < m_num_bins_2; j++, d2 += m_bin_size_2)
344 for(InputIterator p = first; p != last; p++)
346 std::map<double, std::map<double, int> >::iterator pair_p = m_hist.upper_bound((*p)[0]);
348 std::map<double, int>::iterator pair_pp = (pair_p->second).upper_bound((*p)[1]);
375 Matrix n_hist(m_hist_mat);
376 n_hist /= total_counts;
389 double area = m_bin_size_1 * m_bin_size_2;
390 Matrix n_hist(m_hist_mat);
391 n_hist = n_hist / total_counts * area;
size_t num_bins_1() const
Return the number of bins of this histogram.
Definition: prob_histogram.h:254
const std::pair< double, double > & range_1() const
Return the ranges histogram.
Definition: prob_histogram.h:274
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
double bin_size_2() const
Definition: prob_histogram.h:269
const std::map< double, std::map< double, int > > & as_map() const
Return a map of the histogram.
double bin_size() const
Return the bin size of this histogram.
Definition: prob_histogram.h:83
r
Definition: APPgetLargeConnectedEdges.m:127
Matrix normalized() const
Return the normalized 2D histogram of which the sum of all the elements 1.0.
Definition: prob_histogram.h:368
double chi_square(const Histogram_2d &hist_1, const Histogram_2d &hist_2)
Definition: prob_histogram.cpp:45
double bin_size_1() const
Return the bin size of this histogram.
Definition: prob_histogram.h:264
std::pair< double, int > make_double_int_pair_(double d, int i)
Definition: prob_histogram.h:105
const std::map< double, int > & as_map() const
Return a vector of the bins, represented by their lower bounds.
Definition: prob_histogram.h:92
Matrix::Vec_type sum_matrix_cols(const Matrix &m)
Compute the matrix's sum across columns (a.k.a. row-wise sum)
Definition: m_matrix.cpp:1972
const std::pair< double, double > & range_2() const
Definition: prob_histogram.h:279
A class that represents a histogram of data. ATM, the data must be doubles.
Definition: prob_histogram.h:47
const Matrix & as_matrix() const
Return the 2D histogram as a kjb::Matrix Note: the 2D histogrma is not normalized.
Definition: prob_histogram.h:356
Matrix densities() const
Return the densities of the 2D histogram for each cell p, density(p) = counts(p) * area(p)/total_coun...
Definition: prob_histogram.h:382
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
Histogram(InputIterator first, InputIterator last, int num_bins)
Create a histogram from data with the given number of bins.
Definition: prob_histogram.h:59
size_t num_bins_2() const
Definition: prob_histogram.h:259
int num_bins() const
Return the number of bins of this histogram.
Definition: prob_histogram.h:75
Support for error handling exception classes in libKJB.
Histogram_2d(InputIterator first, InputIterator last, size_t num_bins_1, size_t num_bins_2, const std::pair< double, double > &range_1, const std::pair< double, double > &range_2)
Create a 2D histogram from bivirate data given the number of bins of each dimension.
Definition: prob_histogram.h:161
Definition: prob_histogram.h:152
Value_type sum_vector_elements() const
Return this vector's elements sum.
Definition: m_vector.h:1512