forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lcompilers#769 from Madhav2310/stats
Mean, Geometric Mean & Harmonic mean Functions implemented in Statistics.py module
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from statistics import (mean, geometric_mean, harmonic_mean) | ||
from ltypes import i32, f64, i64 | ||
|
||
eps: f64 | ||
eps = 1e-12 | ||
|
||
def test_mean(): | ||
b: list[i32] | ||
b = [9, 4, 10] | ||
j: f64 | ||
j = mean(b) | ||
assert abs(j - 7.666666666666667) < eps | ||
|
||
c: list[f64] | ||
c = [2.0, 3.1, 11.1] | ||
k: f64 | ||
k = mean(c) | ||
assert abs(k - 5.4) < eps | ||
|
||
def test_geometric_mean(): | ||
c: list[i32] | ||
c = [1,2,3] | ||
k: f64 | ||
k = geometric_mean(c) | ||
assert abs(k - 1.8171205928321397) < eps | ||
|
||
def test_harmonic_mean(): | ||
c: list[i32] | ||
c = [9,2,46] | ||
k: f64 | ||
k = harmonic_mean(c) | ||
assert abs(k - 4.740458015267175) < eps | ||
|
||
|
||
def check(): | ||
test_mean() | ||
test_geometric_mean() | ||
test_harmonic_mean() | ||
|
||
check() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from ltypes import i32, f64 | ||
|
||
@overload | ||
def mean(x: list[i32]) -> f64: | ||
k: i32 = len(x) | ||
if k == 0: | ||
return 0.0 | ||
sum: f64 | ||
sum = 0.0 | ||
i: i32 | ||
|
||
for i in range(k): | ||
sum += x[i] | ||
ans: f64 | ||
ans = sum/k | ||
return ans | ||
|
||
@overload | ||
def mean(x: list[f64]) -> f64: | ||
k: i32 = len(x) | ||
if k == 0: | ||
return 0.0 | ||
sum: f64 | ||
sum = 0.0 | ||
i: i32 | ||
for i in range(k): | ||
sum += x[i] | ||
ans: f64 | ||
ans = sum/k | ||
return ans | ||
|
||
@overload | ||
def geometric_mean(x: list[i32]) -> f64: | ||
k: i32 = len(x) | ||
if k == 0: | ||
return 0.0 | ||
product: f64 | ||
product = 1.0 | ||
i: i32 | ||
for i in range(k): | ||
if x[i] < 1: | ||
raise ValueError('geometric mean requires a non-empty dataset containing positive numbers') | ||
product *= x[i] | ||
ans: f64 | ||
ans = product**(1/k) | ||
return ans | ||
|
||
@overload | ||
def harmonic_mean(x: list[i32]) -> f64: | ||
k: i32 = len(x) | ||
if k == 0: | ||
return 0.0 | ||
sum: f64 | ||
sum = 0.0 | ||
i: i32 | ||
for i in range(k): | ||
if x[i] < 0: | ||
raise ValueError('harmonic mean does not support negative values') | ||
if x[i] ==0: | ||
return 0.0 | ||
sum += 1 / x[i] | ||
ans: f64 | ||
ans = k/sum | ||
return ans |