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
3 # found in the LICENSE file.
5 """General statistical or mathematical functions."""
10 def TruncatedMean(data_set
, truncate_percent
):
11 """Calculates the truncated mean of a set of values.
13 Note that this isn't just the mean of the set of values with the highest
14 and lowest values discarded; the non-discarded values are also weighted
15 differently depending how many values are discarded.
18 data_set: Non-empty list of values.
19 truncate_percent: How much of the upper and lower portions of the data set
20 to discard, expressed as a value in [0, 1].
23 The truncated mean as a float.
26 TypeError: The data set was empty after discarding values.
29 data_set
= sorted(data_set
)
31 discard_num_float
= len(data_set
) * truncate_percent
32 discard_num_int
= int(math
.floor(discard_num_float
))
33 kept_weight
= len(data_set
) - discard_num_float
* 2
35 data_set
= data_set
[discard_num_int
:len(data_set
)-discard_num_int
]
37 weight_left
= 1.0 - (discard_num_float
- discard_num_int
)
40 # If the % to discard leaves a fractional portion, need to weight those
42 unweighted_vals
= data_set
[1:len(data_set
)-1]
43 weighted_vals
= [data_set
[0], data_set
[len(data_set
)-1]]
44 weighted_vals
= [w
* weight_left
for w
in weighted_vals
]
45 data_set
= weighted_vals
+ unweighted_vals
47 kept_weight
= len(data_set
)
49 truncated_mean
= reduce(lambda x
, y
: float(x
) + float(y
),
50 data_set
) / kept_weight
56 """Calculates the arithmetic mean of a list of values."""
57 return TruncatedMean(values
, 0.0)
61 """Calculates the sample variance."""
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)
71 def StandardDeviation(values
):
72 """Calculates the sample standard deviation of the given list of values."""
73 return math
.sqrt(Variance(values
))
76 def RelativeChange(before
, after
):
77 """Returns the relative change of before and after, relative to before.
79 There are several different ways to define relative difference between
80 two numbers; sometimes it is defined as relative to the smaller number,
81 or to the mean of the two numbers. This version returns the difference
82 relative to the first of the two numbers.
85 before: A number representing an earlier value.
86 after: Another number, representing a later value.
89 A non-negative floating point number; 0.1 represents a 10% change.
95 difference
= after
- before
96 return math
.fabs(difference
/ before
)
99 def PooledStandardError(work_sets
):
100 """Calculates the pooled sample standard error for a set of samples.
103 work_sets: A collection of collections of numbers.
106 Pooled sample standard error.
112 for current_set
in work_sets
:
113 std_dev
= StandardDeviation(current_set
)
114 numerator
+= (len(current_set
) - 1) * std_dev
** 2
115 denominator1
+= len(current_set
) - 1
116 if len(current_set
) > 0:
117 denominator2
+= 1.0 / len(current_set
)
119 if denominator1
== 0:
122 return math
.sqrt(numerator
/ denominator1
) * math
.sqrt(denominator2
)
125 # Redefining built-in 'StandardError'
126 # pylint: disable=W0622
127 def StandardError(values
):
128 """Calculates the standard error of a list of values."""
131 std_dev
= StandardDeviation(values
)
132 return std_dev
/ math
.sqrt(len(values
))