diff --git a/eo/src/utils/eoStat.h b/eo/src/utils/eoStat.h index a0cf08a67..bc267e11e 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