不偏共分散
code:cov_unbiased_np01.py
import numpy as np
Sxx1 = np.sum((x - np.mean(x))*(x - np.mean(x))) / (x.shape0 - 1) Sxy1 = np.sum((x - np.mean(x))*(y - np.mean(y))) / (x.shape0 - 1) Syx1 = np.sum((y - np.mean(y))*(x - np.mean(x))) / (x.shape0 - 1) Syy1 = np.sum((y - np.mean(y))*(y - np.mean(y))) / (x.shape0 - 1) print('定義式で求めた(不偏)共分散')
print('Sxx :', Sxx1)
print('Sxy :', Sxy1)
print('Syx :', Syx1)
print('Syy :', Syy1)
print('cov()で求めた(不偏)分散共分散行列 :\n', np.cov(x, y))
cov【NumPy】は、デフォルトで不偏共分散を求める。
実行結果:
code:result.txt
定義式で求めた(不偏)共分散
Sxx : 320.0
Sxy : 157.5
Syx : 157.5
Syy : 220.0
cov()で求めた(不偏)分散共分散行列 :