Skip to content

Commit c089957

Browse files
authored
pvariance and pstdev implemented in statistics module (#1004)
1 parent bac0986 commit c089957

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

integration_tests/test_statistics.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from statistics import (mean, fmean, geometric_mean, harmonic_mean,
2-
variance, stdev)
3-
from ltypes import i32, f64, i64
1+
from statistics import (mean, fmean, geometric_mean, harmonic_mean, variance, stdev, pvariance, pstdev)
2+
from ltypes import i32, f64, i64, f32
43

54
eps: f64
65
eps = 1e-12
@@ -97,6 +96,31 @@ def test_stdev():
9796
k = stdev(b)
9897
assert abs(k - 0.41967844833872525) < eps
9998

99+
def test_pvariance():
100+
a: list[i32]
101+
a = [1, 2, 5, 4, 8, 9, 12]
102+
j: f64
103+
j = pvariance(a)
104+
assert abs(j - 13.551020408163266) < eps
105+
106+
b: list[f64]
107+
b = [2.74, 1.23, 2.63, 2.22, 3.0, 1.98]
108+
k: f64
109+
k = pvariance(b)
110+
assert abs(k - 0.34103333333333335) < eps
111+
112+
def test_pstdev():
113+
a: list[i32]
114+
a = [1, 2, 3, 4, 5]
115+
j: f64
116+
j = pstdev(a)
117+
assert abs(j - 1.4142135623730951) < eps
118+
119+
b: list[f64]
120+
b = [1.23, 1.45, 2.1, 2.2, 1.9]
121+
k: f64
122+
k = pstdev(b)
123+
assert abs(k - 0.37537181567080935) < eps
100124

101125
def check():
102126
test_mean()
@@ -105,5 +129,7 @@ def check():
105129
test_fmean()
106130
test_variance()
107131
test_stdev()
132+
test_pvariance()
133+
test_pstdev()
108134

109135
check()

src/runtime/statistics.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from ltypes import i32, f64, overload
2-
from math import sqrt
1+
from ltypes import i32, f64, i64, f64, overload
32

43

54
@overload
@@ -272,3 +271,54 @@ def stdev(x: list[i32]) -> f64:
272271
"""
273272
return variance(x)**0.5
274273

274+
@overload
275+
def pvariance(x: list[f64]) -> f64:
276+
"""
277+
Returns the population varience of a data sequence of numbers
278+
"""
279+
n: i32
280+
n = len(x)
281+
if n < 1:
282+
raise Exception("n > 1 for variance")
283+
xmean: f64
284+
xmean = mean(x)
285+
num: f64
286+
num = 0.0
287+
i: i32
288+
for i in range(n):
289+
num += (x[i] - xmean)**2
290+
return num / n
291+
292+
@overload
293+
def pvariance(x: list[i32]) -> f64:
294+
"""
295+
Returns the population varience of a data sequence of numbers
296+
"""
297+
n: i32
298+
n = len(x)
299+
if n < 1:
300+
raise Exception("n > 1 for variance")
301+
xmean: f64
302+
xmean = mean(x)
303+
num: f64
304+
num = 0.0
305+
i: i32
306+
for i in range(n):
307+
num += (x[i] - xmean)**2
308+
return num / n
309+
310+
311+
@overload
312+
def pstdev(x: list[f64]) -> f64:
313+
"""
314+
Returns the population standard deviation of a data sequence of numbers
315+
"""
316+
return pvariance(x)**0.5
317+
318+
@overload
319+
def pstdev(x: list[i32]) -> f64:
320+
"""
321+
Returns the population standard deviation of a data sequence of numbers
322+
"""
323+
return pvariance(x)**0.5
324+

0 commit comments

Comments
 (0)