How can I compute linear correlation coefficient for my input faster than CORR using Statistics Toolbox 7.3 (R2010a)?
2 ビュー (過去 30 日間)
古いコメントを表示
I am executing a code which makes hundreds of calls to CORR. I call CORR once on a large matrix to compute Pearson's linear correlation coefficient (default) as follows:
load returns
tic
ret_corr1 = corr(returns,'type','Pearson');
toc
Elapsed time is 0.081229 seconds.
When I execute the function hundreds of times, the overall time taken is extremely large.
採用された回答
MathWorks Support Team
2011 年 6 月 28 日
This enhancement has been incorporated in Release 2011a (R2011a). For previous product releases, read below for any possible workarounds:
The following two workarounds allow you to compute the Pearson's linear correlation coefficient faster than CORR.
Workaround 1:
tic
ret_cov = cov(returns);
[~,ret_corr2] = cov2corr(ret_cov);
toc
Elapsed time is 0.009861 seconds.
Workaround 2:
tic
n = size(returns,2);
N = size(returns,1);
ret_corr3 = zeros(n,n);
for ix = 2:n
for iy = 1:ix-1
xm = returns(:,ix);
xmsx = (xm-mean(xm))./std(xm);
ym = returns(:,iy);
ymsy = (ym-mean(ym))./std(ym);
ret_corr3(ix,iy) = (1/(N-1))*sum(xmsx.*ymsy);
end
end
ret_corr3 = ret_corr3 + ret_corr3' + eye(n);
toc
Elapsed time is 0.016819 seconds.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!