標本共分散
code:cov_sample_np01.py
import numpy as np
Sxx1 = np.sum((x - np.mean(x))*(x - np.mean(x))) / x.shape0 Sxy1 = np.sum((x - np.mean(x))*(y - np.mean(y))) / x.shape0 Syx1 = np.sum((y - np.mean(y))*(x - np.mean(x))) / x.shape0 Syy1 = np.sum((y - np.mean(y))*(y - np.mean(y))) / x.shape0 print('定義式で求めた(標本)共分散')
print('Sxx :', Sxx1)
print('Sxy :', Sxy1)
print('Syx :', Syx1)
print('Syy :', Syy1)
print('cov()で求めた(標本)分散共分散行列 :\n', np.cov(x, y, bias=True))
実行結果:
code:sh
定義式で求めた(標本)共分散
Sxx : 256.0
Sxy : 126.0
Syx : 126.0
Syy : 176.0
cov()で求めた(標本)分散共分散行列
共分散を両方の標準偏差で割ることで、相関係数が得られる。 参考