SimpleStats.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef SimpleStats_h
  26. #define SimpleStats_h
  27. #include <wtf/MathExtras.h>
  28. #include <wtf/StdLibExtras.h>
  29. namespace WTF {
  30. // Simple and cheap way of tracking statistics if you're not worried about chopping on
  31. // the sum of squares (i.e. the sum of squares is unlikely to exceed 2^52).
  32. class SimpleStats {
  33. public:
  34. SimpleStats()
  35. : m_count(0)
  36. , m_sum(0)
  37. , m_sumOfSquares(0)
  38. {
  39. }
  40. void add(double value)
  41. {
  42. m_count++;
  43. m_sum += value;
  44. m_sumOfSquares += value * value;
  45. }
  46. bool operator!() const
  47. {
  48. return !m_count;
  49. }
  50. double count() const
  51. {
  52. return m_count;
  53. }
  54. double sum() const
  55. {
  56. return m_sum;
  57. }
  58. double sumOfSquares() const
  59. {
  60. return m_sumOfSquares;
  61. }
  62. double mean() const
  63. {
  64. return m_sum / m_count;
  65. }
  66. // NB. This gives a biased variance as it divides by the number of samples rather
  67. // than the degrees of freedom. This is fine once the count grows large, which in
  68. // our case will happen rather quickly.
  69. double variance() const
  70. {
  71. if (m_count < 2)
  72. return 0;
  73. // Compute <x^2> - <x>^2
  74. double secondMoment = m_sumOfSquares / m_count;
  75. double firstMoment = m_sum / m_count;
  76. double result = secondMoment - firstMoment * firstMoment;
  77. // It's possible to get -epsilon. Protect against this and turn it into
  78. // +0.
  79. if (result <= 0)
  80. return 0;
  81. return result;
  82. }
  83. // NB. This gives a biased standard deviation. See above.
  84. double standardDeviation() const
  85. {
  86. return sqrt(variance());
  87. }
  88. private:
  89. double m_count;
  90. double m_sum;
  91. double m_sumOfSquares;
  92. };
  93. } // namespace WTF
  94. #endif // SimpleStats_h