INF120/inf120stat.py

23 lines
502 B
Python

from math import sqrt, isclose
def mean(l):
return sum(l)/len(l)
def std(l):
m = mean(l)
diffs = [(x-m)**2 for x in l]
return sqrt(sum(diffs)/len(diffs))
def test_mean():
l = [1.0, 2.0, 3.0, 4.0]
expected = 2.5
return isclose(mean(l), expected, abs_tol=0.001)
def test_std():
l = [1.0, 2.0, 3.0, 4.0]
expected = 1.11803
return isclose(std(l), expected, abs_tol=0.001)
if __name__ == '__main__':
print('mean:', test_mean())
print('std:', test_std())