Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: telemetry/telemetry/value/list_of_scalar_values.py

Issue 2087463002: [telemetry] Fix computation of standard deviations (Closed) Base URL: git@github.com:catapult-project/catapult@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 import numbers 5 import numbers
6 import math 6 import math
7 7
8 from telemetry import value as value_module 8 from telemetry import value as value_module
9 from telemetry.value import none_values 9 from telemetry.value import none_values
10 from telemetry.value import summarizable 10 from telemetry.value import summarizable
11 11
12 12
13 def Variance(sample): 13 def Variance(sample):
14 """ Compute the population variance. 14 """ Compute the population variance.
15 15
16 Args: 16 Args:
17 sample: a list of numbers. 17 sample: a list of numbers.
18 """ 18 """
19 k = len(sample) - 1 # Bessel correction 19 k = len(sample) - 1 # Bessel correction
20 if k <= 0: 20 if k <= 0:
21 return 0 21 return 0.0
22 m = _Mean(sample) 22 m = _Mean(sample)
23 return sum((x - m)**2 for x in sample)/k 23 return sum((x - m)**2 for x in sample)/k
24 24
25 25
26 def StandardDeviation(sample): 26 def StandardDeviation(sample):
27 """ Compute standard deviation for a list of numbers. 27 """ Compute standard deviation for a list of numbers.
28 28
29 Args: 29 Args:
30 sample: a list of numbers. 30 sample: a list of numbers.
31 """ 31 """
(...skipping 15 matching lines...) Expand all
47 total_degrees_of_freedom = 0 47 total_degrees_of_freedom = 0
48 for i in xrange(len(list_of_samples)): 48 for i in xrange(len(list_of_samples)):
49 l = list_of_samples[i] 49 l = list_of_samples[i]
50 k = len(l) - 1 # Bessel correction 50 k = len(l) - 1 # Bessel correction
51 if k <= 0: 51 if k <= 0:
52 continue 52 continue
53 variance = list_of_variances[i] if list_of_variances else Variance(l) 53 variance = list_of_variances[i] if list_of_variances else Variance(l)
54 pooled_variance += k * variance 54 pooled_variance += k * variance
55 total_degrees_of_freedom += k 55 total_degrees_of_freedom += k
56 if total_degrees_of_freedom: 56 if total_degrees_of_freedom:
57 return (pooled_variance/total_degrees_of_freedom) ** 0.5 57 return (pooled_variance / total_degrees_of_freedom) ** 0.5
58 return 0 58 else:
59 return 0.0
59 60
60 61
61 def _Mean(values): 62 def _Mean(values):
62 return float(sum(values)) / len(values) if len(values) > 0 else 0.0 63 return float(sum(values)) / len(values) if len(values) > 0 else 0.0
63 64
64 65
65 class ListOfScalarValues(summarizable.SummarizableValue): 66 class ListOfScalarValues(summarizable.SummarizableValue):
66 """ ListOfScalarValues represents a list of numbers. 67 """ ListOfScalarValues represents a list of numbers.
67 68
68 By default, std is the standard deviation of all numbers in the list. Std can 69 By default, std is the standard deviation of all numbers in the list. Std can
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 list_of_samples = [] 207 list_of_samples = []
207 none_value_reason = None 208 none_value_reason = None
208 pooled_std = None 209 pooled_std = None
209 for v in values: 210 for v in values:
210 if v.values is None: 211 if v.values is None:
211 merged_values = None 212 merged_values = None
212 none_value_reason = none_values.MERGE_FAILURE_REASON 213 none_value_reason = none_values.MERGE_FAILURE_REASON
213 break 214 break
214 merged_values.extend(v.values) 215 merged_values.extend(v.values)
215 list_of_samples.append(v.values) 216 list_of_samples.append(v.values)
216 if merged_values: 217 if merged_values and page is None:
218 # Pooled standard deviation is only used when merging values comming from
219 # different pages. Otherwise, fall back to the default computation in
220 # ListOfScalarValues' constructor.
eakuefner 2016/06/20 17:26:32 supernit: s/s'/s's/
perezju 2016/06/21 09:48:07 Grammar confuses me. I rephrased this to avoid the
217 pooled_std = PooledStandardDeviation( 221 pooled_std = PooledStandardDeviation(
218 list_of_samples, list_of_variances=[v.variance for v in values]) 222 list_of_samples, list_of_variances=[v.variance for v in values])
219 return ListOfScalarValues( 223 return ListOfScalarValues(
220 page, name, v0.units, 224 page, name, v0.units,
221 merged_values, 225 merged_values,
222 important=v0.important, 226 important=v0.important,
223 description=v0.description, 227 description=v0.description,
224 tir_label=tir_label, 228 tir_label=tir_label,
225 same_page_merge_policy=v0.same_page_merge_policy, 229 same_page_merge_policy=v0.same_page_merge_policy,
226 std=pooled_std, 230 std=pooled_std,
227 none_value_reason=none_value_reason, 231 none_value_reason=none_value_reason,
228 improvement_direction=v0.improvement_direction, 232 improvement_direction=v0.improvement_direction,
229 grouping_keys=grouping_keys) 233 grouping_keys=grouping_keys)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698