OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """General statistical or mathematical functions.""" | 5 """General statistical or mathematical functions.""" |
6 | 6 |
7 import math | 7 import math |
8 | 8 |
9 | 9 |
10 def TruncatedMean(data_set, truncate_percent): | 10 def TruncatedMean(data_set, truncate_percent): |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 data_set) / kept_weight | 50 data_set) / kept_weight |
51 | 51 |
52 return truncated_mean | 52 return truncated_mean |
53 | 53 |
54 | 54 |
55 def Mean(values): | 55 def Mean(values): |
56 """Calculates the arithmetic mean of a list of values.""" | 56 """Calculates the arithmetic mean of a list of values.""" |
57 return TruncatedMean(values, 0.0) | 57 return TruncatedMean(values, 0.0) |
58 | 58 |
59 | 59 |
| 60 def Variance(values): |
| 61 """Calculates the sample variance.""" |
| 62 if len(values) == 1: |
| 63 return 0.0 |
| 64 mean = Mean(values) |
| 65 differences_from_mean = [float(x) - mean for x in values] |
| 66 squared_differences = [float(x * x) for x in differences_from_mean] |
| 67 variance = sum(squared_differences) / (len(values) - 1) |
| 68 return variance |
| 69 |
| 70 |
60 def StandardDeviation(values): | 71 def StandardDeviation(values): |
61 """Calculates the sample standard deviation of the given list of values.""" | 72 """Calculates the sample standard deviation of the given list of values.""" |
62 if len(values) == 1: | 73 return math.sqrt(Variance(values)) |
63 return 0.0 | |
64 | |
65 mean = Mean(values) | |
66 differences_from_mean = [float(x) - mean for x in values] | |
67 squared_differences = [float(x * x) for x in differences_from_mean] | |
68 variance = sum(squared_differences) / (len(values) - 1) | |
69 std_dev = math.sqrt(variance) | |
70 | |
71 return std_dev | |
72 | 74 |
73 | 75 |
74 def RelativeChange(before, after): | 76 def RelativeChange(before, after): |
75 """Returns the relative change of before and after, relative to before. | 77 """Returns the relative change of before and after, relative to before. |
76 | 78 |
77 There are several different ways to define relative difference between | 79 There are several different ways to define relative difference between |
78 two numbers; sometimes it is defined as relative to the smaller number, | 80 two numbers; sometimes it is defined as relative to the smaller number, |
79 or to the mean of the two numbers. This version returns the difference | 81 or to the mean of the two numbers. This version returns the difference |
80 relative to the first of the two numbers. | 82 relative to the first of the two numbers. |
81 | 83 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 | 123 |
122 | 124 |
123 # Redefining built-in 'StandardError' | 125 # Redefining built-in 'StandardError' |
124 # pylint: disable=W0622 | 126 # pylint: disable=W0622 |
125 def StandardError(values): | 127 def StandardError(values): |
126 """Calculates the standard error of a list of values.""" | 128 """Calculates the standard error of a list of values.""" |
127 if len(values) <= 1: | 129 if len(values) <= 1: |
128 return 0.0 | 130 return 0.0 |
129 std_dev = StandardDeviation(values) | 131 std_dev = StandardDeviation(values) |
130 return std_dev / math.sqrt(len(values)) | 132 return std_dev / math.sqrt(len(values)) |
OLD | NEW |