% How large a correlation coefficient pure noise can produce
% Textbook: Part I, "Uni-Variate Linear Least-Squares", exercise
% "simulation: how often does noise look correlated"
%
% M independent standard normal pairs, so the true correlation is zero.
% Repeating k times shows the spread of r_xy due to sampling alone:
% std(r_xy) is close to 1/sqrt(M-1). For M = 10, |r_xy| > 0.6 happens in
% about 7% of the data sets, although nothing is correlated.

clearvars; close all
rng(0)

Ms = [10 30 100];
k = 1e4;            % repetitions
threshold = 0.6;

r = zeros(k, length(Ms));
for i = 1:length(Ms)
    M = Ms(i);
    a = randn(M, k);
    b = randn(M, k);
    a = a - mean(a);
    b = b - mean(b);
    r(:, i) = sum(a.*b)./sqrt(sum(a.^2).*sum(b.^2));   % r_xy of every column pair
    fprintf('M = %3d: std(r) = %.3f, 1/sqrt(M-1) = %.3f, Pr(|r| > %.1f) = %.4f\n', ...
        M, std(r(:, i)), 1/sqrt(M - 1), threshold, mean(abs(r(:, i)) > threshold))
end

figure
boxchart(r)
xticklabels("M = " + string(Ms))
hold on
yline([-threshold threshold], 'r:', 'LineWidth', 1.2)
hold off
ylabel('r_{xy}')
title('r_{xy} of independent data')
grid on
