View | Details | Raw Unified | Return to bug 762
Collapse All | Expand All

(-)a/src/contrib/average.h (-21 / +39 lines)
 Lines 16-27    Link Here 
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
17
 *
18
 * Authors: Pavel Boyko <boyko@iitp.ru>
18
 * Authors: Pavel Boyko <boyko@iitp.ru>
19
 * Corrections and extensions: Timo Bingmann <tbns@idlebox.net>
19
 */
20
 */
20
21
21
#ifndef AVERAGE_H
22
#ifndef AVERAGE_H
22
#define AVERAGE_H
23
#define AVERAGE_H
23
#include <cmath>
24
#include <cmath>
24
#include <iostream>
25
#include <ostream>
25
#include <limits>
26
#include <limits>
26
27
27
/// Simple average, min, max and std. deviation calculator
28
/// Simple average, min, max and std. deviation calculator
 Lines 29-71   template <typename T = double> Link Here 
29
class Average
30
class Average
30
{
31
{
31
public:
32
public:
32
  Average () : 
33
  Average ()
33
    size (0), min (std::numeric_limits<T>::max ()), max (0), avg (0), avg2 (0) 
34
    : m_size (0), m_min (std::numeric_limits<T>::max ()), m_max (0),
35
      m_avg (0), m_avg2 (0) 
34
  {
36
  {
35
  }
37
  }
36
38
37
  /// Add new value
39
  /// Add new value
38
  void Update (T const & x)
40
  void Update (T const & x)
39
  {
41
  {
40
    min = std::min (x, min);
42
    m_min = std::min (x, m_min);
41
    max = std::max (x, max);
43
    m_max = std::max (x, m_max);
42
    avg = (size * avg + x) / (size + 1);
44
    m_avg = (m_size * m_avg + x) / (m_size + 1);
43
    avg2 = (size * avg2 + x * x) / (size + 1);
45
    m_avg2 = (m_size * m_avg2 + x * x) / (m_size + 1);
44
    size++;
46
    m_size++;
45
  }
47
  }
46
  /// Reset statistics
48
  /// Reset statistics
47
  void Reset ()
49
  void Reset ()
48
  {
50
  {
49
    size = 0;
51
    m_size = 0;
50
    min = std::numeric_limits<T>::max ();
52
    m_min = std::numeric_limits<T>::max ();
51
    max = 0;
53
    m_max = 0;
52
    avg = 0;
54
    m_avg = 0;
53
    avg2 = 0;
55
    m_avg2 = 0;
54
  }
56
  }
55
57
56
  ///\name Access results
58
  ///\name Access results
57
  //\{
59
  //\{
58
  uint32_t Count () const { return size; }
60
  uint32_t Count   () const { return m_size; }
59
  T        Min   () const { return min; }
61
  T        Min     () const { return m_min; }
60
  T        Max   () const { return max; }
62
  T        Max     () const { return m_max; }
61
  double   Avg   () const { return avg; }
63
62
  double   Err   () const { return sqrt ((avg2 - avg*avg)/(size - 1)); }
64
  /** Return average of sample set. */
65
  double   Avg     () const { return m_avg; }
66
67
  /** Return standard error (standard deviation) of sample set. */
68
  double   Err     () const { return sqrt (m_avg2 - m_avg*m_avg); }
69
70
  /** Return margin of error for 90% confidence level assuming a
71
      normal error distribution. */
72
  double   Margin90() const { return 1.645 * Err () / sqrt (m_size); }
73
74
  /** Return margin of error for 95% confidence level assuming a
75
      normal error distribution. */
76
  double   Margin95() const { return 1.960 * Err () / sqrt (m_size); }
77
78
  /** Return margin of error for 99% confidence level assuming a
79
      normal error distribution. */
80
  double   Margin99() const { return 2.576 * Err () / sqrt (m_size); }
63
  //\}
81
  //\}
64
82
65
private:
83
private:
66
  uint32_t size;
84
  uint32_t m_size;
67
  T      min, max;
85
  T      m_min, m_max;
68
  double avg, avg2;
86
  double m_avg, m_avg2;
69
};
87
};
70
88
71
/// Print avg (err) [min, max]
89
/// Print avg (err) [min, max]

Return to bug 762