% generate correlated data: n=200; a=0.2; x = randn(n, 1); y = randn(n, 1) + a*x; % correlation coefficient: rd = corr(x, y); fprintf('correlation coefficient of data r = %.2f\n', rd); % distribution of null hypothesis by permutation: nperm = 1000; rs = zeros(nperm,1); for i=1:nperm xr=x(randperm(length(x))); % shuffle x yr=y(randperm(length(y))); % shuffle y rs(i) = corr(xr, yr); end [h,b] = hist(rs, 20); h = h/sum(h)/(b(2)-b(1)); % normalization % significance: rq = quantile(rs, 0.95); fprintf('correlation coefficient of null hypothesis at 5%% significance = %.2f\n', rq); if rd >= rq fprintf('--> correlation r=%.2f is significant\n', rd); else fprintf('--> r=%.2f is not a significant correlation\n', rd); end % plot: bar(b, h, 'facecolor', 'b'); hold on; bar(b(b>=rq), h(b>=rq), 'facecolor', 'r'); plot([rd rd], [0 4], 'r', 'linewidth', 2); xlabel('Correlation coefficient'); ylabel('Probability density of H0'); hold off;