2022-08-07に更新

Pythonでピアソンの積率相関係数,Fisherの相関係数を求めるプログラム

ピアソン(Pearson)の積率相関係数

相関係数を求める.

def corre_factor(retsu):
    X=Estimate[index2,retsu]
    Y=Target[index2,retsu]
    E_xx=np.sum((X-np.mean(X))**2)
    E_yy=np.sum((Y-np.mean(Y))**2)
    E_xy=np.sum((X-np.mean(X))*(Y-np.mean(Y)))
    return E_xy/np.sqrt(E_xx)/np.sqrt(E_yy)

df=pd.DataFrame([['H1',corre_factor(0)],['λ1',corre_factor(3)],['ω1',corre_factor(4)],
               ['H2',corre_factor(5)],['λ2',corre_factor(8)],['ω2',corre_factor(9)] ])
 df

以上です.

Fisherの相関係数

方向統計では,ピアソンの積率相関係数を適用できません.
例えば,[0°, 360°)で満遍なくデータが散布していた場合,平均の方向など定義できないからです.
そこで,Fisherの相関係数を使用します.

Sxy,Sxx,Syy=0,0,0
sin_est=Estimate[index2,2]
cos_est=Estimate[index2,1]
sin_tar=Target[index2,2]
cos_tar=Target[index2,1]
for i in range(len(index2)-1):
    for j in range(i+1,len(index2)):
        Sxy +=math.sin(math.atan2(sin_est[i],cos_est[i])-math.atan2(sin_est[j],cos_est[j] ))*math.sin(math.atan2(sin_tar[i],cos_tar[i])-math.atan2(sin_tar[j],cos_tar[j]))
        Sxx += math.sin(math.atan2(sin_est[i],cos_est[i] )-math.atan2(sin_est[j],cos_est[j] ))**2
        Syy += math.sin(math.atan2(sin_tar[i],cos_tar[i] )-math.atan2(sin_tar[j],cos_tar[j] ))**2
Rc=Sxy/(math.sqrt(Sxx*Syy))
print(Rc)
ツイッターでシェア
みんなに共有、忘れないようにメモ

kawai_mizugorou

社会人2年目.自分用のメモとして使ってます.

Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。

また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!

有料記事を販売できるようになりました!

こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?

コメント