OLD | NEW |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2009 IITP RAS | 3 * Copyright (c) 2009 IITP RAS |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 5 * This program is free software; you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 2 as | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 27 matching lines...) Expand all Loading... |
38 | 38 |
39 template <typename T = double> | 39 template <typename T = double> |
40 class Average | 40 class Average |
41 { | 41 { |
42 public: | 42 public: |
43 Average () | 43 Average () |
44 : m_size (0), m_min (std::numeric_limits<T>::max ()), m_max (0) | 44 : m_size (0), m_min (std::numeric_limits<T>::max ()), m_max (0) |
45 { | 45 { |
46 } | 46 } |
47 | 47 |
48 /// Add new sample | 48 /** |
| 49 * Add new sample |
| 50 * \param x the new sample |
| 51 */ |
49 void Update (T const & x) | 52 void Update (T const & x) |
50 { | 53 { |
51 // Give the variance calculator the next value. | 54 // Give the variance calculator the next value. |
52 m_varianceCalculator.Update (x); | 55 m_varianceCalculator.Update (x); |
53 | 56 |
54 m_min = std::min (x, m_min); | 57 m_min = std::min (x, m_min); |
55 m_max = std::max (x, m_max); | 58 m_max = std::max (x, m_max); |
56 m_size++; | 59 m_size++; |
57 } | 60 } |
58 /// Reset statistics | 61 /// Reset statistics |
59 void Reset () | 62 void Reset () |
60 { | 63 { |
61 m_varianceCalculator.Reset (); | 64 m_varianceCalculator.Reset (); |
62 | 65 |
63 m_size = 0; | 66 m_size = 0; |
64 m_min = std::numeric_limits<T>::max (); | 67 m_min = std::numeric_limits<T>::max (); |
65 m_max = 0; | 68 m_max = 0; |
66 } | 69 } |
67 | 70 |
68 // Sample statistics | 71 // Sample statistics |
69 /// Sample size | 72 /** |
| 73 * Get Sample size |
| 74 * \returns the sample size |
| 75 */ |
70 uint32_t Count () const { return m_size; } | 76 uint32_t Count () const { return m_size; } |
71 /// Minimum | 77 /** |
| 78 * Get Minimum |
| 79 * \returns the minimum value |
| 80 */ |
72 T Min () const { return m_min; } | 81 T Min () const { return m_min; } |
73 /// Maximum | 82 /** |
| 83 * Get Maximum |
| 84 * \returns the maximum value |
| 85 */ |
74 T Max () const { return m_max; } | 86 T Max () const { return m_max; } |
75 /// Sample average | 87 /** |
| 88 * Get the sample average |
| 89 * \returns the sample average |
| 90 */ |
76 double Avg () const { return m_varianceCalculator.getMean ();} | 91 double Avg () const { return m_varianceCalculator.getMean ();} |
77 /// Estimate of mean, alias to Avg | 92 /** |
| 93 * Get the Estimate of mean, alias to Avg |
| 94 * \returns the mean |
| 95 */ |
78 double Mean () const { return Avg (); } | 96 double Mean () const { return Avg (); } |
79 /// Unbiased estimate of variance | 97 /** |
| 98 * Get the Unbiased estimate of variance |
| 99 * \returns the variance |
| 100 */ |
80 double Var () const { return m_varianceCalculator.getVariance ();} | 101 double Var () const { return m_varianceCalculator.getVariance ();} |
81 /// Standard deviation | 102 /** |
| 103 * Get the Standard deviation |
| 104 * \returns the standard deviation |
| 105 */ |
82 double Stddev () const { return std::sqrt (Var ()); } | 106 double Stddev () const { return std::sqrt (Var ()); } |
83 | 107 |
84 /** | 108 /** |
85 * \name Error of the mean estimates | 109 * \name Error of the mean estimates |
86 * | 110 * |
87 * @{ | 111 * @{ |
88 */ | 112 */ |
89 /** | 113 /** |
90 * \brief Margin of error of the mean for 90% confidence level | 114 * \brief Margin of error of the mean for 90% confidence level |
91 * | 115 * |
(...skipping 30 matching lines...) Expand all Loading... |
122 double Error99 () const { return 2.576 * std::sqrt (Var () / Count ()); } | 146 double Error99 () const { return 2.576 * std::sqrt (Var () / Count ()); } |
123 /**@}*/ | 147 /**@}*/ |
124 | 148 |
125 private: | 149 private: |
126 uint32_t m_size; //!< Number of sampled data. | 150 uint32_t m_size; //!< Number of sampled data. |
127 T m_min; //!< Minimum value observed. | 151 T m_min; //!< Minimum value observed. |
128 T m_max; //!< Maximum value observed. | 152 T m_max; //!< Maximum value observed. |
129 MinMaxAvgTotalCalculator<double> m_varianceCalculator; //!< Variance calculato
r. | 153 MinMaxAvgTotalCalculator<double> m_varianceCalculator; //!< Variance calculato
r. |
130 }; | 154 }; |
131 | 155 |
132 /// Print avg (err) [min, max] | 156 /** |
| 157 * Print avg (err) [min, max] |
| 158 * \param os the output stream |
| 159 * \param x the average |
| 160 * \returns the output stream |
| 161 */ |
133 template <typename T> | 162 template <typename T> |
134 std::ostream & operator<< (std::ostream & os, Average<T> const & x) | 163 std::ostream & operator<< (std::ostream & os, Average<T> const & x) |
135 { | 164 { |
136 if (x.Count () != 0) | 165 if (x.Count () != 0) |
137 os << x.Avg () << " (" << x.Stddev () << ") [" << x.Min () << ", " << x.Max
() << "]"; | 166 os << x.Avg () << " (" << x.Stddev () << ") [" << x.Min () << ", " << x.Max
() << "]"; |
138 else | 167 else |
139 os << "NA"; // not available | 168 os << "NA"; // not available |
140 return os; | 169 return os; |
141 } | 170 } |
142 } | 171 } |
143 #endif /* AVERAGE_H */ | 172 #endif /* AVERAGE_H */ |
OLD | NEW |