OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 """A collection of statistical utility functions to be used by metrics.""" | 5 """A collection of statistical utility functions to be used by metrics.""" |
6 | 6 |
7 import bisect | 7 import bisect |
8 import math | 8 import math |
9 | 9 |
10 | 10 |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 return sorted_values[0] | 206 return sorted_values[0] |
207 elif percentile >= (n - 0.5) / n: | 207 elif percentile >= (n - 0.5) / n: |
208 return sorted_values[-1] | 208 return sorted_values[-1] |
209 else: | 209 else: |
210 floor_index = int(math.floor(n * percentile - 0.5)) | 210 floor_index = int(math.floor(n * percentile - 0.5)) |
211 floor_value = sorted_values[floor_index] | 211 floor_value = sorted_values[floor_index] |
212 ceil_value = sorted_values[floor_index+1] | 212 ceil_value = sorted_values[floor_index+1] |
213 alpha = n * percentile - 0.5 - floor_index | 213 alpha = n * percentile - 0.5 - floor_index |
214 return floor_value + alpha * (ceil_value - floor_value) | 214 return floor_value + alpha * (ceil_value - floor_value) |
215 | 215 |
| 216 |
| 217 def GeometricMean(values): |
| 218 """Compute a rounded geometric mean from an array of values.""" |
| 219 if not values: |
| 220 return None |
| 221 # To avoid infinite value errors, make sure no value is less than 0.001. |
| 222 new_values = [] |
| 223 for value in values: |
| 224 if value > 0.001: |
| 225 new_values.append(value) |
| 226 else: |
| 227 new_values.append(0.001) |
| 228 # Compute the sum of the log of the values. |
| 229 log_sum = sum(map(math.log, new_values)) |
| 230 # Raise e to that sum over the number of values. |
| 231 mean = math.pow(math.e, (log_sum / len(new_values))) |
| 232 # Return the rounded mean. |
| 233 return int(round(mean)) |
| 234 |
OLD | NEW |