-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcovariance_cal.py
More file actions
28 lines (26 loc) · 809 Bytes
/
Copy pathcovariance_cal.py
File metadata and controls
28 lines (26 loc) · 809 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np
def calculate_covariance_matrix(vectors: list[list[float]]) -> list[list[float]]:
arr = np.array(vectors)
print(arr)
print('--------------------------------')
cov_matrix = np.cov(arr, rowvar=True)
print(cov_matrix)
print('--------------------------------')
return cov_matrix.tolist()
# mean = []
# m = len(vectors) # of observation
# res = []
# for i in range(len(vectors)):
# m = sum(vectors[i])/len(vectors)
# mean.append(m)
# s = 0
# for j in range(len(vectors[0])):
# s+=(vectors[i][j]-mean[i])**2
# mini = []
# for k in range(len(vectors)):
# co = np.cov(vectors[i],vectors[k])
# mini.append(co)
# res.append(mini)
# return res
print(calculate_covariance_matrix([[1,2,3],[2,3,4],[5,6,7]]))
print(calculate_covariance_matrix([[1, 2, 3], [4, 5, 6]]))