From 91ab6b8296dc162efef69c19118405c5c487c7b3 Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Fri, 22 Oct 2010 10:08:57 +0200 Subject: [PATCH] a stat object for computing interquartile range (a robust measure of dispersion) --- eo/src/utils/eoStat.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/eo/src/utils/eoStat.h b/eo/src/utils/eoStat.h index a0cf08a6..bc267e11 100644 --- a/eo/src/utils/eoStat.h +++ b/eo/src/utils/eoStat.h @@ -453,4 +453,31 @@ public : } }; */ + +//! A robust measure of dispersion (also called midspread or middle fifty) that is the difference between the third and the first quartile. +template +class eoInterquartileRangeStat : public eoStat< EOT, typename EOT::Fitness > +{ +public: + using eoStat::value; + + eoInterquartileRangeStat( typename EOT::Fitness start, std::string description = "IQR" ) : eoStat( start, description ) {} + + virtual void operator()( const eoPop & _pop ) + { + eoPop pop = _pop; + + unsigned int quartile = pop.size()/4; + std::nth_element( pop.begin(), pop.begin()+quartile*1, pop.end() ); + typename EOT::Fitness Q1 = pop[quartile].fitness(); + + std::nth_element( pop.begin(), pop.begin()+quartile*3, pop.end() ); + typename EOT::Fitness Q3 = pop[quartile*3].fitness(); + + value() = Q1 - Q3; + } + + virtual std::string className(void) const { return "eoInterquartileRangeStat"; } +}; + #endif