diff --git a/dlib/statistics/statistics.h b/dlib/statistics/statistics.h index 28df37cbf..98571a08b 100644 --- a/dlib/statistics/statistics.h +++ b/dlib/statistics/statistics.h @@ -37,6 +37,8 @@ namespace dlib sum_sqr = 0; n = 0; maximum_n = std::numeric_limits::max(); + min_value = std::numeric_limits::max(); + max_value = std::numeric_limits::min(); } void set_max_n ( @@ -56,6 +58,11 @@ namespace dlib sum = n_div_n*sum + val*div_n; sum_sqr = n_div_n*sum_sqr + val*div_n*val; + if (val < min_value) + min_value = val; + if (val > max_value) + max_value = val; + if (n < maximum_n) ++n; } @@ -78,13 +85,39 @@ namespace dlib return sum; } + T max ( + ) const + { + // make sure requires clause is not broken + DLIB_ASSERT(current_n() > 1, + "\tT running_stats::max" + << "\n\tyou have to add some numbers to this object first" + << "\n\tthis: " << this + ); + + return max_value; + } + + T min ( + ) const + { + // make sure requires clause is not broken + DLIB_ASSERT(current_n() > 1, + "\tT running_stats::min" + << "\n\tyou have to add some numbers to this object first" + << "\n\tthis: " << this + ); + + return min_value; + } + T variance ( ) const { // make sure requires clause is not broken DLIB_ASSERT(current_n() > 1, "\tT running_stats::variance" - << "\n\tsize of queue should not be zero" + << "\n\tyou have to add some numbers to this object first" << "\n\tthis: " << this ); @@ -99,7 +132,7 @@ namespace dlib // make sure requires clause is not broken DLIB_ASSERT(current_n() > 1, "\tT running_stats::variance" - << "\n\tsize of queue should not be zero" + << "\n\tyou have to add some numbers to this object first" << "\n\tthis: " << this ); return (val-mean())/std::sqrt(variance()); @@ -110,6 +143,8 @@ namespace dlib T sum_sqr; T n; T maximum_n; + T min_value; + T max_value; }; // ---------------------------------------------------------------------------------------- diff --git a/dlib/statistics/statistics_abstract.h b/dlib/statistics/statistics_abstract.h index c9e8fc467..c63a68557 100644 --- a/dlib/statistics/statistics_abstract.h +++ b/dlib/statistics/statistics_abstract.h @@ -115,6 +115,24 @@ namespace dlib object so far. !*/ + T max ( + ) const; + /*! + requires + - current_n() > 1 + ensures + - returns the largest value presented to this object so far. + !*/ + + T min ( + ) const; + /*! + requires + - current_n() > 1 + ensures + - returns the smallest value presented to this object so far. + !*/ + T scale ( const T& val ) const;