# Changeset 3137 for trunk/yat/statistics/utility.h

Ignore:
Timestamp:
Nov 28, 2013, 5:11:23 AM (8 years ago)
Message:

closes #772. new function that calculates mutual information

File:
1 edited

### Legend:

Unmodified
 r3136 #include "yat/utility/deprecate.h" #include "yat/utility/iterator_traits.h" #include "yat/utility/Vector.h" #include "yat/utility/VectorBase.h" #include "yat/utility/yat_assert.h" #include #include #include #include #include #include #include namespace theplu { bool sorted=false); /** \brief Calculates the mutual information of \a A. The elements in A are unnormalized probabilies of the joint distribution. The mutual information is calculated as \f$\sum \sum p(x,y) \log_2 \frac {p(x,y)} {p(x)p(y)} \f$ where \f$p(x,y) = \frac {A_{xy}}{\sum_{x,y} A_{xy}} \f$; \f$p(x) = \sum_y A_{xy} / \sum_{x,y} A_{xy} \f$; \f$p(y) = \sum_x A_{xy} / \sum_{x,y} A_{xy} \f$ Requirements: - \c T must be a model of \ref concept_container_2d - \c T::value_type must be convertible to \c double \return mutual information in bits; if you want in natural base multiply with \c M_LN2 (defined in \c gsl/gsl_math.h ) \since New in yat 0.12 */ template double mutual_information(const T& A); /** return median(ad.begin(), ad.end(), true); } template double median(RandomAccessIterator first, RandomAccessIterator last, bool sorted) { return percentile2(first, last, 50.0, sorted); } template double percentile(RandomAccessIterator first, RandomAccessIterator last, template double median(RandomAccessIterator first, RandomAccessIterator last, bool sorted) { return percentile2(first, last, 50.0, sorted); } template double mutual_information(const T& n) { BOOST_CONCEPT_ASSERT((utility::Container2D)); using boost::Convertible; BOOST_CONCEPT_ASSERT((Convertible)); // p_x = \sum_y p_xy // Mutual Information is defined as // \sum_xy p_xy * log (p_xy / (p_x p_y)) = // \sum_xy p_xy * [log p_xy - log p_x - log p_y] // \sum_xy p_xy log p_xy - p_xy log p_x - p_xy log p_y // \sum_xy p_xy log p_xy - \sum_x p_x log p_x - \sum_y p_y log p_y // - entropy_xy + entropy_x + entropy_y utility::Vector rowsum(n.columns(), 0); for (size_t c = 0; c double percentile(RandomAccessIterator first, RandomAccessIterator last, double p, bool sorted) {