From 2874bf9a039f65be24429850c07bc05fbde8bec9 Mon Sep 17 00:00:00 2001 From: Madhav2310 Date: Fri, 12 Aug 2022 13:10:16 +0530 Subject: [PATCH 1/7] gmean, Hmean expanded for other datatypes --- src/runtime/statistics.py | 84 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/src/runtime/statistics.py b/src/runtime/statistics.py index 4622e0c827..25bec8d16d 100644 --- a/src/runtime/statistics.py +++ b/src/runtime/statistics.py @@ -102,7 +102,7 @@ def fmean(x: list[f32]) -> f64: """ return mean(x) - +@overload def geometric_mean(x: list[i32]) -> f64: """ Returns the geometric mean of a data sequence of numbers @@ -115,11 +115,50 @@ def geometric_mean(x: list[i32]) -> f64: i: i32 for i in range(k): + if(x[i]<=0): + raise Exception("geometric mean requires a non-empty dataset containing positive numbers") product *= float(x[i]) return product**(1/k) +@overload +def geometric_mean(x: list[i64]) -> f64: + """ + Returns the geometric mean of a data sequence of numbers + """ + 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]<=0): + raise Exception("geometric mean requires a non-empty dataset containing positive numbers") + product *= float(x[i]) + + return product**(1/k) +@overload +def geometric_mean(x: list[f64]) -> f64: + """ + Returns the geometric mean of a data sequence of numbers + """ + 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]<=0.0): + raise Exception("geometric mean requires a non-empty dataset containing positive numbers") + product *= x[i] + + return product**(1/k) +@overload def harmonic_mean(x: list[i32]) -> f64: """ Returns the harmonic mean of a data sequence of numbers @@ -134,6 +173,48 @@ def harmonic_mean(x: list[i32]) -> f64: for i in range(k): if x[i] == 0: return 0.0 + if(x[i]<0.0): + raise Exception("Harmonic mean does not support negative values") + sum += 1 / x[i] + + return float(k/sum) +@overload +def harmonic_mean(x: list[i64]) -> f64: + """ + Returns the harmonic mean of a data sequence of numbers + """ + 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: + return 0.0 + if(x[i]<0): + raise Exception("Harmonic mean does not support negative values") + sum += 1 / x[i] + return k/sum + +@overload +def harmonic_mean(x: list[f64]) -> f64: + """ + Returns the harmonic mean of a data sequence of numbers + """ + 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.0: + return 0.0 + if(x[i]<0.0): + raise Exception("Harmonic mean does not support negative values") sum += 1 / x[i] return k/sum @@ -188,3 +269,4 @@ def stdev(x: list[i32]) -> f64: Returns the standard deviation of a data sequence of numbers """ return variance(x)**0.5 + From 4c66e3b3d7401ef39df8a04a4c087837a151df06 Mon Sep 17 00:00:00 2001 From: Madhav2310 Date: Fri, 12 Aug 2022 13:10:46 +0530 Subject: [PATCH 2/7] tests added --- integration_tests/test_statistics.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/integration_tests/test_statistics.py b/integration_tests/test_statistics.py index bc037e500e..885bdd04bd 100644 --- a/integration_tests/test_statistics.py +++ b/integration_tests/test_statistics.py @@ -45,6 +45,12 @@ def test_geometric_mean(): k = geometric_mean(c) assert abs(k - 1.8171205928321397) < eps + d: list[f64] + d = [1.1, 3.4, 17.982, 11.8] + l: f64 + l = geometric_mean(d) + assert abs(l - 5.307596520524432) < eps + def test_harmonic_mean(): c: list[i32] c = [9,2,46] @@ -52,6 +58,18 @@ def test_harmonic_mean(): k = harmonic_mean(c) assert abs(k - 4.740458015267175) < eps + d: list[i32] + d = [9,0,46] + l: f64 + l = harmonic_mean(d) + assert l == 0.0 + + e: list[f64] + e = [1.1, 3.4, 17.982, 11.8] + f: f64 + f = harmonic_mean(e) + assert abs(f - 2.977152988015106) < eps + def test_variance(): a: list[i32] From 98fdd6ed64f2facf89f717ae5f0ed971706bee9c Mon Sep 17 00:00:00 2001 From: Madhav Mittal <59472922+Madhav2310@users.noreply.github.com> Date: Fri, 12 Aug 2022 14:18:31 +0530 Subject: [PATCH 3/7] Update src/runtime/statistics.py Co-authored-by: Smit Lunagariya --- src/runtime/statistics.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/runtime/statistics.py b/src/runtime/statistics.py index 25bec8d16d..17a62db03a 100644 --- a/src/runtime/statistics.py +++ b/src/runtime/statistics.py @@ -158,6 +158,7 @@ def geometric_mean(x: list[f64]) -> f64: product *= x[i] return product**(1/k) + @overload def harmonic_mean(x: list[i32]) -> f64: """ From 77a7323b40234693e180280f256a6e0fff1f834e Mon Sep 17 00:00:00 2001 From: Madhav Mittal <59472922+Madhav2310@users.noreply.github.com> Date: Fri, 12 Aug 2022 14:18:37 +0530 Subject: [PATCH 4/7] Update src/runtime/statistics.py Co-authored-by: Smit Lunagariya --- src/runtime/statistics.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/runtime/statistics.py b/src/runtime/statistics.py index 17a62db03a..cd8af655f6 100644 --- a/src/runtime/statistics.py +++ b/src/runtime/statistics.py @@ -179,6 +179,7 @@ def harmonic_mean(x: list[i32]) -> f64: sum += 1 / x[i] return float(k/sum) + @overload def harmonic_mean(x: list[i64]) -> f64: """ From fc9ae657a9ef236b9575a634835ac49c355a3557 Mon Sep 17 00:00:00 2001 From: Madhav2310 Date: Sat, 13 Aug 2022 05:01:39 +0530 Subject: [PATCH 5/7] code cleaned --- src/runtime/statistics.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/runtime/statistics.py b/src/runtime/statistics.py index 25bec8d16d..b0782bd6a5 100644 --- a/src/runtime/statistics.py +++ b/src/runtime/statistics.py @@ -115,7 +115,7 @@ def geometric_mean(x: list[i32]) -> f64: i: i32 for i in range(k): - if(x[i]<=0): + if x[i] <= 0: raise Exception("geometric mean requires a non-empty dataset containing positive numbers") product *= float(x[i]) @@ -134,7 +134,7 @@ def geometric_mean(x: list[i64]) -> f64: i: i32 for i in range(k): - if(x[i]<=0): + if x[i] <= 0: raise Exception("geometric mean requires a non-empty dataset containing positive numbers") product *= float(x[i]) @@ -153,11 +153,12 @@ def geometric_mean(x: list[f64]) -> f64: i: i32 for i in range(k): - if(x[i]<=0.0): + if x[i] <= 0.0: raise Exception("geometric mean requires a non-empty dataset containing positive numbers") product *= x[i] return product**(1/k) + @overload def harmonic_mean(x: list[i32]) -> f64: """ @@ -173,11 +174,12 @@ def harmonic_mean(x: list[i32]) -> f64: for i in range(k): if x[i] == 0: return 0.0 - if(x[i]<0.0): + if x[i] < 0.0: raise Exception("Harmonic mean does not support negative values") sum += 1 / x[i] return float(k/sum) + @overload def harmonic_mean(x: list[i64]) -> f64: """ @@ -193,7 +195,7 @@ def harmonic_mean(x: list[i64]) -> f64: for i in range(k): if x[i] == 0: return 0.0 - if(x[i]<0): + if x[i] < 0 : raise Exception("Harmonic mean does not support negative values") sum += 1 / x[i] return k/sum @@ -213,7 +215,7 @@ def harmonic_mean(x: list[f64]) -> f64: for i in range(k): if x[i] == 0.0: return 0.0 - if(x[i]<0.0): + if x[i] < 0.0: raise Exception("Harmonic mean does not support negative values") sum += 1 / x[i] @@ -234,8 +236,8 @@ def variance(x: list[f64]) -> f64: num = 0.0 i: i32 for i in range(n): - num += (x[i]-xmean)**2 - return num/(n-1) + num += (x[i] - xmean)**2 + return num / (n-1) @overload def variance(x: list[i32]) -> f64: @@ -253,7 +255,7 @@ def variance(x: list[i32]) -> f64: i: i32 for i in range(n): num += (x[i]-xmean)**2 - return num/(n-1) + return num / (n-1) @overload From bb643ca08f7fe3664c7207dc5d07cea5cd674228 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Sat, 13 Aug 2022 20:30:59 +0530 Subject: [PATCH 6/7] Apply suggestions from code review --- integration_tests/test_statistics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_tests/test_statistics.py b/integration_tests/test_statistics.py index 6a2c21e9e6..4e1aaaa589 100644 --- a/integration_tests/test_statistics.py +++ b/integration_tests/test_statistics.py @@ -59,7 +59,7 @@ def test_harmonic_mean(): assert abs(k - 4.740458015267175) < eps d: list[i32] - d = [9,0,46] + d = [9, 0, 46] l: f64 l = harmonic_mean(d) assert l == 0.0 From 715cf561f3f2892858faeadb65b95084cd81d9fe Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Sun, 14 Aug 2022 13:39:02 +0530 Subject: [PATCH 7/7] fix errors --- integration_tests/test_statistics.py | 45 +--------------------------- 1 file changed, 1 insertion(+), 44 deletions(-) diff --git a/integration_tests/test_statistics.py b/integration_tests/test_statistics.py index 4e1aaaa589..f5990ae948 100644 --- a/integration_tests/test_statistics.py +++ b/integration_tests/test_statistics.py @@ -1,5 +1,5 @@ from statistics import (mean, fmean, geometric_mean, harmonic_mean, - variance, stdev, covariance, correlation) + variance, stdev) from ltypes import i32, f64, i64 eps: f64 @@ -84,47 +84,6 @@ def test_variance(): k = variance(b) assert abs(k - 0.40924) < eps -def test_covariance(): - a: list[i32] - a = [1, 2, 3, 4, 5, 6, 7, 8, 9] - b: list[i32] - b = [1, 2, 3, 1, 2, 3, 1, 2, 3] - j: f64 - j = covariance(a,b) - assert abs(j - 0.75) < eps - - c: list[f64] - c = [2.74, 1.23, 2.63, 2.22, 3.0, 1.98] - d: list[f64] - d = [9.4, 1.23, 2.63, 22.4, 1.9, 13.98] - k: f64 - k = covariance(c,d) - assert abs(k + 0.24955999999999934) < eps - -def test_correlation(): - a: list[i32] - a = [11, 2, 7, 4, 15, 6, 10, 8, 9, 1, 11, 5, 13, 6, 15] - b: list[i32] - b = [2, 5, 17, 6, 10, 8, 13, 4, 6, 9, 11, 2, 5, 4, 7] - - j: f64 - j = correlation(a,b) - assert abs(j - 0.11521487988958108) < eps - - c: list[i32] - c = [1, 2, 3, 4, 5, 6, 7, 8, 9] - d: list[i32] - d = [9, 8, 7, 6, 5, 4, 3, 2, 1] - - k: f64 - k = correlation(c,c) - assert k == 1.0 - - l: f64 - l = correlation(c,d) - assert l == -1.0 - - def test_stdev(): a: list[i32] a = [1, 2, 3, 4, 5] @@ -146,7 +105,5 @@ def check(): test_fmean() test_variance() test_stdev() - test_covariance() - test_correlation() check()