Pearson Calculation using MATLAB

4 ビュー (過去 30 日間)
Abhishek
Abhishek 2011 年 4 月 28 日
コメント済み: Tahariet Sharon 2017 年 10 月 28 日
For calculating Pearson Coefficient, i am using the below formula in MATLAB:
C=cov(x,y); p=C(2)/(std(x)*std(y));
But C(2) is zero in my case if
x = [1 0 1 0 0 0]; y = [1 1 1 1 1 1];
Any idea where I am going wrong

回答 (1 件)

Matt Tearle
Matt Tearle 2011 年 4 月 28 日
Your y values are all the same. Hence y_i - mean(y) = 0 for each i. The formula for covariance is
cov(x,y) = sum_i[(x_i-mean(x))*(y_i-mean(y)]/n
So every term in your sum is 0. This makes sense in that there is only one y value, but different corresponding x values. The correlation coefficient, however, will be NaN because std(y) will also be 0.
  2 件のコメント
Oleg Komarov
Oleg Komarov 2011 年 4 月 28 日
@Abhishek: you can also use corrcoeff
Tahariet Sharon
Tahariet Sharon 2017 年 10 月 28 日
I think you should normalize by n-1.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by