Changeset 3135
 Timestamp:
 Nov 27, 2013, 8:58:49 AM (9 years ago)
 Location:
 trunk
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test/statistics.cc
r3114 r3135 47 47 using namespace theplu::yat; 48 48 void test_benjamini_hochberg(test::Suite&); 49 void test_entropy(test::Suite&); 49 50 void test_mad(test::Suite&); 50 51 … … 126 127 statistics::tTest t_test; 127 128 classifier::Target target; 128 add(t_test, boost::forward_iterator_archetype<double>(), 129 add(t_test, boost::forward_iterator_archetype<double>(), 129 130 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>(), 131 132 boost::forward_iterator_archetype<utility::DataWeight>(), target); 132 133 133 } 134 134 test_benjamini_hochberg(suite); 135 test_entropy(suite); 135 136 test_median_empty(suite); 136 137 return suite.return_value(); … … 164 165 165 166 167 void 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>1e15) { 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 166 193 void test_mad(test::Suite& suite) 167 194 { 
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_ 3 3 4 4 // $Id$ … … 9 9 Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér 10 10 Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson 11 Copyright (C) 2009, 2010, 2011 Peter Johansson11 Copyright (C) 2009, 2010, 2011, 2013 Peter Johansson 12 12 13 13 This file is part of the yat library, http://dev.thep.lu.se/yat … … 51 51 namespace theplu { 52 52 namespace yat { 53 namespace statistics { 53 namespace statistics { 54 54 55 55 /** … … 97 97 unsigned int n2, unsigned int t); 98 98 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); 99 109 100 110 /** … … 251 261 252 262 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 253 282 template <class RandomAccessIterator> 254 283 double mad(RandomAccessIterator first, RandomAccessIterator last,
Note: See TracChangeset
for help on using the changeset viewer.