This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/linearalgebra/exercises/covariance.m

20 lines
436 B
Matlab

n = 1000;
x = 2.0*randn(n, 1);
z = 3.0*randn(n, 1);
rs = [-1.0:0.25:1.0];
for k = 1:length(rs)
r = rs(k);
y = r*x + sqrt(1.0-r^2)*z;
r
cv = cov([x y])
corrcoef([x y])
subplot(3, 3, k)
scatter(x, y, 'filled', 'MarkerEdgeColor', 'white')
title(sprintf('r=%g', r))
xlim([-10, 10])
ylim([-20, 20])
text(-8, -15, sprintf('cov(x,y)=%.2f', cv(1, 2)))
end
savefigpdf(gcf, 'covariance.pdf', 20, 20);