-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
40 lines (33 loc) · 1.23 KB
/
stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def variance(data):
"""Returns the population variance of a list (array) of numbers in data.
The variance is the sum of squared differences between data values
and their mean, divided by the number of items in the list (n).
This is different from the Python library function statistics.variance
which returns the sample variance, where the sum is divided by (n-1).
Example: variance([1,5]) is ((1-3)**2 + (5-3)**2)/2 = 4.
(These are Google style comments for arguments, returns, and exceptions.)
Args:
data: list of numbers for which variance will be computed.
Must contain at least one element.
Returns:
population variance of values in data list.
Raises:
ValueError if the data parameter is empty.
>>> variance([1])
0.0
>>> variance([1, 1, 1, 1])
0.0
>>> variance([1, 2])
0.25
>>> variance([1000000, 1000004])
4.0
"""
# some deliberately misformatted code. Use flake8 to fix.
n=len(data)
if n==0:
raise ValueError( )
average = sum(data)/n
return sum( [(x-average)**2 for x in data] )/n
def stdev(data):
"""The population standard deviation of a list of data values."""
return sqrt(variance(data))