Skip to content

Pvariance and Pstdev functions implemented #1004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions integration_tests/test_statistics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from statistics import (mean, fmean, geometric_mean, harmonic_mean,
variance, stdev)
from ltypes import i32, f64, i64
from statistics import (mean, fmean, geometric_mean, harmonic_mean, variance, stdev, pvariance, pstdev)
from ltypes import i32, f64, i64, f32

eps: f64
eps = 1e-12
Expand Down Expand Up @@ -97,6 +96,31 @@ def test_stdev():
k = stdev(b)
assert abs(k - 0.41967844833872525) < eps

def test_pvariance():
a: list[i32]
a = [1, 2, 5, 4, 8, 9, 12]
j: f64
j = pvariance(a)
assert abs(j - 13.551020408163266) < eps

b: list[f64]
b = [2.74, 1.23, 2.63, 2.22, 3.0, 1.98]
k: f64
k = pvariance(b)
assert abs(k - 0.34103333333333335) < eps

def test_pstdev():
a: list[i32]
a = [1, 2, 3, 4, 5]
j: f64
j = pstdev(a)
assert abs(j - 1.4142135623730951) < eps

b: list[f64]
b = [1.23, 1.45, 2.1, 2.2, 1.9]
k: f64
k = pstdev(b)
assert abs(k - 0.37537181567080935) < eps

def check():
test_mean()
Expand All @@ -105,5 +129,7 @@ def check():
test_fmean()
test_variance()
test_stdev()
test_pvariance()
test_pstdev()

check()
54 changes: 52 additions & 2 deletions src/runtime/statistics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from ltypes import i32, f64, overload
from math import sqrt
from ltypes import i32, f64, i64, f64, overload


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

@overload
def pvariance(x: list[f64]) -> f64:
"""
Returns the population varience of a data sequence of numbers
"""
n: i32
n = len(x)
if n < 1:
raise Exception("n > 1 for variance")
xmean: f64
xmean = mean(x)
num: f64
num = 0.0
i: i32
for i in range(n):
num += (x[i] - xmean)**2
return num / n

@overload
def pvariance(x: list[i32]) -> f64:
"""
Returns the population varience of a data sequence of numbers
"""
n: i32
n = len(x)
if n < 1:
raise Exception("n > 1 for variance")
xmean: f64
xmean = mean(x)
num: f64
num = 0.0
i: i32
for i in range(n):
num += (x[i] - xmean)**2
return num / n


@overload
def pstdev(x: list[f64]) -> f64:
"""
Returns the population standard deviation of a data sequence of numbers
"""
return pvariance(x)**0.5

@overload
def pstdev(x: list[i32]) -> f64:
"""
Returns the population standard deviation of a data sequence of numbers
"""
return pvariance(x)**0.5