Changeset 3135


Ignore:
Timestamp:
Nov 27, 2013, 8:58:49 AM (7 years ago)
Author:
Peter
Message:

add function that calculates entropy from a range

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/statistics.cc

    r3114 r3135  
    4747using namespace theplu::yat;
    4848void test_benjamini_hochberg(test::Suite&);
     49void test_entropy(test::Suite&);
    4950void test_mad(test::Suite&);
    5051
     
    126127    statistics::tTest t_test;
    127128    classifier::Target target;
    128     add(t_test, boost::forward_iterator_archetype<double>(), 
     129    add(t_test, boost::forward_iterator_archetype<double>(),
    129130        boost::forward_iterator_archetype<double>(), target);
    130     add(t_test, boost::forward_iterator_archetype<utility::DataWeight>(), 
     131    add(t_test, boost::forward_iterator_archetype<utility::DataWeight>(),
    131132        boost::forward_iterator_archetype<utility::DataWeight>(), target);
    132            
    133133  }
    134134  test_benjamini_hochberg(suite);
     135  test_entropy(suite);
    135136  test_median_empty(suite);
    136137  return suite.return_value();
     
    164165
    165166
     167void test_entropy(test::Suite& suite)
     168{
     169  suite.out() << "testing entropy(2)\n";
     170  using statistics::entropy;
     171  std::vector<int> x(10000,0);
     172  x[512] = 42;
     173  double e = entropy(x.begin(), x.end());
     174  if (e>1e-15) {
     175    suite.add(false);
     176    suite.out() << "entropy: " << e << " expected close to 0\n";
     177  }
     178  x[0] = 42;
     179  e = entropy(x.begin(), x.end());
     180  if (e<=0) {
     181    suite.add(false);
     182    suite.out() << "entropy: " << e << " expected > 0\n";
     183  }
     184
     185  // do not run compiler test
     186  if (false) {
     187    entropy(boost::input_iterator_archetype<double>(),
     188            boost::input_iterator_archetype<double>());
     189  }
     190}
     191
     192
    166193void test_mad(test::Suite& suite)
    167194{
  • trunk/yat/statistics/utility.h

    r2673 r3135  
    1 #ifndef _theplu_yat_statistics_utility_ 
    2 #define _theplu_yat_statistics_utility_ 
     1#ifndef _theplu_yat_statistics_utility_
     2#define _theplu_yat_statistics_utility_
    33
    44// $Id$
     
    99  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
    1010  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
    11   Copyright (C) 2009, 2010, 2011 Peter Johansson
     11  Copyright (C) 2009, 2010, 2011, 2013 Peter Johansson
    1212
    1313  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    5151namespace theplu {
    5252namespace yat {
    53 namespace statistics { 
     53namespace statistics {
    5454
    5555  /**
     
    9797                              unsigned int n2, unsigned int t);
    9898
     99
     100  /**
     101     The entropy is calculated as \f$ \sum_i p_i \log p_i \f$ where
     102     \f$p_i = \frac{n_i}{\sum_j n_j} \f$
     103
     104     Requirements: \c InputIterator should be an \input_iterator
     105     Requirements: \c InputIterator::value_type must be convertible to \c double
     106   */
     107  template<typename InputIterator>
     108  double entropy(InputIterator first, InputIterator last);
    99109
    100110  /**
     
    251261
    252262
     263  template<typename InputIterator>
     264  double entropy(InputIterator first, InputIterator last)
     265  {
     266    BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
     267    using boost::Convertible;
     268    typedef typename InputIterator::value_type T;
     269    BOOST_CONCEPT_ASSERT((Convertible<T,double>));
     270    double sum = 0;
     271    double N = 0;
     272    for (; first != last; ++first) {
     273      if (*first) {
     274        N += *first;
     275        sum += *first * std::log(static_cast<double>(*first));
     276      }
     277    }
     278    return -sum / N + log(N);
     279  }
     280
     281
    253282  template <class RandomAccessIterator>
    254283  double mad(RandomAccessIterator first, RandomAccessIterator last,
Note: See TracChangeset for help on using the changeset viewer.