Skip to content

Commit 73e40ec

Browse files
committed
Covariance and Correlation functions implemented
1 parent 1e1e9b0 commit 73e40ec

File tree

1 file changed

+108
-7
lines changed

1 file changed

+108
-7
lines changed

src/runtime/statistics.py

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from ltypes import i32, f64, overload
2-
2+
from math import sqrt
33

44

55
@overload
@@ -117,7 +117,7 @@ def geometric_mean(x: list[i32]) -> f64:
117117
for i in range(k):
118118
product *= float(x[i])
119119

120-
return product**(1/k)
120+
return product ** (1 / k)
121121

122122

123123
def harmonic_mean(x: list[i32]) -> f64:
@@ -136,7 +136,7 @@ def harmonic_mean(x: list[i32]) -> f64:
136136
return 0.0
137137
sum += 1 / x[i]
138138

139-
return k/sum
139+
return k / sum
140140

141141
@overload
142142
def variance(x: list[f64]) -> f64:
@@ -153,8 +153,8 @@ def variance(x: list[f64]) -> f64:
153153
num = 0.0
154154
i: i32
155155
for i in range(n):
156-
num += (x[i]-xmean)**2
157-
return num/(n-1)
156+
num += (x[i] - xmean)**2
157+
return num / (n-1)
158158

159159
@overload
160160
def variance(x: list[i32]) -> f64:
@@ -171,8 +171,8 @@ def variance(x: list[i32]) -> f64:
171171
num = 0.0
172172
i: i32
173173
for i in range(n):
174-
num += (x[i]-xmean)**2
175-
return num/(n-1)
174+
num += (x[i] - xmean)**2
175+
return num / (n-1)
176176

177177

178178
@overload
@@ -188,3 +188,104 @@ def stdev(x: list[i32]) -> f64:
188188
Returns the standard deviation of a data sequence of numbers
189189
"""
190190
return variance(x)**0.5
191+
192+
@overload
193+
def covariance(x: list[i32], y: list[i32]) -> f64:
194+
"""
195+
Returns the covariance of a data sequence of numbers
196+
"""
197+
n: i32 = len(x)
198+
m: i32 = len(y)
199+
if (n < 2 or m < 2) or n != m:
200+
raise Exception("Both inputs must be of the same length (no less than two)")
201+
xmean: f64 = mean(x)
202+
ymean: f64 = mean(y)
203+
num: f64
204+
num = 0.0
205+
i: i32
206+
for i in range(n):
207+
num += (x[i] - xmean) * (y[i] - ymean)
208+
return num / (n-1)
209+
210+
@overload
211+
def covariance(x: list[f64], y: list[f64]) -> f64:
212+
"""
213+
Returns the covariance of a data sequence of numbers
214+
"""
215+
n: i32 = len(x)
216+
m: i32 = len(y)
217+
if (n < 2 or m < 2) or n != m:
218+
raise Exception("Both inputs must be of the same length (no less than two)")
219+
xmean: f64 = mean(x)
220+
ymean: f64 = mean(y)
221+
num: f64
222+
num = 0.0
223+
i: i32
224+
for i in range(n):
225+
num += (x[i] - xmean) * (y[i] - ymean)
226+
return num / (n-1)
227+
228+
@overload
229+
def correlation(x: list[i32], y: list[i32]) -> f64:
230+
"""
231+
Return the Pearson's correlation coefficient for two inputs.
232+
"""
233+
n: i32 = len(x)
234+
m: i32 = len(y)
235+
if n != m:
236+
raise Exception("correlation requires that both inputs have same number of data points")
237+
if n < 2:
238+
raise Exception("correlation requires at least two data points")
239+
xmean: f64 = mean(x)
240+
ymean: f64 = mean(y)
241+
242+
sxy: f64 = 0.0
243+
i: i32
244+
for i in range(n):
245+
sxy += (x[i] - xmean) * (y[i] - ymean)
246+
247+
sxx: f64 = 0.0
248+
j: i32
249+
for j in range(n):
250+
sxx += (x[j] - xmean) ** 2
251+
252+
syy: f64 = 0.0
253+
k: i32
254+
for k in range(n):
255+
syy += (y[k] - ymean) ** 2
256+
if sqrt(sxx * syy) == 0:
257+
raise Exception('at least one of the inputs is constant')
258+
return sxy / sqrt(sxx * syy)
259+
260+
@overload
261+
def correlation(x: list[f64], y: list[f64]) -> f64:
262+
"""
263+
Return the Pearson's correlation coefficient for two inputs.
264+
"""
265+
n: i32 = len(x)
266+
m: i32 = len(y)
267+
if n != m:
268+
raise Exception("correlation requires that both inputs have same number of data points")
269+
if n < 2:
270+
raise Exception("correlation requires at least two data points")
271+
xmean: f64 = mean(x)
272+
ymean: f64 = mean(y)
273+
274+
sxy: f64 = 0.0
275+
i: i32
276+
for i in range(n):
277+
sxy += (x[i] - xmean) * (y[i] - ymean)
278+
279+
sxx: f64 = 0.0
280+
j: i32
281+
for j in range(n):
282+
sxx += (x[j] - xmean) ** 2
283+
284+
syy: f64 = 0.0
285+
k: i32
286+
for k in range(n):
287+
syy += (y[k] - ymean) ** 2
288+
if sqrt(sxx * syy) == 0:
289+
raise Exception('at least one of the inputs is constant')
290+
return sxy / sqrt(sxx * syy)
291+

0 commit comments

Comments
 (0)