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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 | 152 |
153 Args: | 153 Args: |
154 durations: List of interval lengths in milliseconds. | 154 durations: List of interval lengths in milliseconds. |
155 absolute: See TimestampsDiscrepancy. | 155 absolute: See TimestampsDiscrepancy. |
156 interval_multiplier: See TimestampsDiscrepancy. | 156 interval_multiplier: See TimestampsDiscrepancy. |
157 """ | 157 """ |
158 timestamps = reduce(lambda x, y: x + [x[-1] + y], durations, [0])[1:] | 158 timestamps = reduce(lambda x, y: x + [x[-1] + y], durations, [0])[1:] |
159 return TimestampsDiscrepancy(timestamps, absolute, interval_multiplier) | 159 return TimestampsDiscrepancy(timestamps, absolute, interval_multiplier) |
160 | 160 |
161 | 161 |
162 def ArithmeticMean(numerator, denominator): | 162 def ArithmeticMean(data): |
163 """Calculates arithmetic mean. | 163 """Calculates arithmetic mean. |
164 | 164 |
165 Both numerator and denominator can be given as either individual | 165 Both numerator and denominator can be given as either individual |
tonyg
2014/03/12 15:28:38
Nit: update the bit about the denominator.
| |
166 values or lists of values which will be summed. | 166 values or lists of values which will be summed. |
167 | 167 |
168 Args: | 168 Args: |
169 numerator: A quantity that represents a sum total value. | 169 numerator: A quantity that represents a sum total value. |
170 denominator: A quantity that represents a count of the number of things. | 170 denominator: A quantity that represents a count of the number of things. |
tonyg
2014/03/12 15:28:38
Nit: remove this line
| |
171 | 171 |
172 Returns: | 172 Returns: |
173 The arithmetic mean value, or 0 if the denominator value was 0. | 173 The arithmetic mean value, or 0 if the denominator value was 0. |
174 """ | 174 """ |
175 numerator_total = Total(numerator) | 175 numerator_total = Total(data) |
176 denominator_total = Total(denominator) | 176 denominator_total = Total(len(data)) |
177 return DivideIfPossibleOrZero(numerator_total, denominator_total) | 177 return DivideIfPossibleOrZero(numerator_total, denominator_total) |
178 | 178 |
179 | 179 |
180 def StandardDeviation(data): | |
tonyg
2014/03/12 15:28:38
Even though these are really common concepts, I re
| |
181 if len(data) == 1: | |
182 return 0.0 | |
183 | |
184 mean = ArithmeticMean(data) | |
185 variances = [float(x) - mean for x in data] | |
186 variances = [x * x for x in variances] | |
187 std_dev = math.sqrt(ArithmeticMean(variances)) | |
188 | |
189 return std_dev | |
190 | |
191 | |
192 def StandardErrorOfMean(data): | |
193 if len(data) <= 1: | |
194 return 0.0 | |
195 | |
196 std_dev = StandardDeviation(data) | |
197 | |
198 return std_dev / math.sqrt(len(data)) | |
199 | |
200 | |
201 def TrapezoidalRule(data, dx): | |
202 """ Calculate the integral according to trapezoidal rule | |
203 Approximates the definite integral of f from a to b by | |
204 the composite trapezoidal rule, using n subintervals | |
205 http://en.wikipedia.org/wiki/Trapezoidal_rule#Uniform_grid | |
206 """ | |
tonyg
2014/03/12 15:28:38
The content of this comment is perfect, but please
| |
207 n = len(data) - 1 | |
208 s = data[0] + data[n] | |
209 | |
210 for i in range(1, n): | |
211 s += 2 * data[i] | |
212 | |
213 return s * dx / 2.0 | |
214 | |
180 def Total(data): | 215 def Total(data): |
181 """Returns the float value of a number or the sum of a list.""" | 216 """Returns the float value of a number or the sum of a list.""" |
182 if type(data) == float: | 217 if type(data) == float: |
183 total = data | 218 total = data |
184 elif type(data) == int: | 219 elif type(data) == int: |
185 total = float(data) | 220 total = float(data) |
186 elif type(data) == list: | 221 elif type(data) == list: |
187 total = float(sum(data)) | 222 total = float(sum(data)) |
188 else: | 223 else: |
189 raise TypeError | 224 raise TypeError |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 new_values.append(value) | 291 new_values.append(value) |
257 else: | 292 else: |
258 new_values.append(0.001) | 293 new_values.append(0.001) |
259 # Compute the sum of the log of the values. | 294 # Compute the sum of the log of the values. |
260 log_sum = sum(map(math.log, new_values)) | 295 log_sum = sum(map(math.log, new_values)) |
261 # Raise e to that sum over the number of values. | 296 # Raise e to that sum over the number of values. |
262 mean = math.pow(math.e, (log_sum / len(new_values))) | 297 mean = math.pow(math.e, (log_sum / len(new_values))) |
263 # Return the rounded mean. | 298 # Return the rounded mean. |
264 return int(round(mean)) | 299 return int(round(mean)) |
265 | 300 |
OLD | NEW |