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/code/correlationcoefficient.m
2014-11-12 18:39:02 +01:00

30 lines
1011 B
Matlab

set( 0, 'DefaultTextFontSize', 22.0 );
set( 0, 'DefaultAxesFontSize', 22.0 );
n = 10000;
x = 1.0*randn( n, 1 );
z = 1.0*randn( n, 1 );
for r=-1:0.2:1
y = r*x + sqrt(1.0-r^2.0)*z;
cv = cov( x, y );
cr = corrcoef( x, y );
scatter( x, y, 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'x' );
ylabel( 'y' );
text( 0.05, 0.9, sprintf( 'r = %.2f', r ), 'Units', 'normalized' )
text( 0.05, 0.8, sprintf( 'cor = %.2f %.2f', cr(1,1), cr(1,2) ), 'Units', 'normalized' )
text( 0.05, 0.7, sprintf( 'cov = %.2f %.2f', cv(1,1), cv(1,2) ), 'Units', 'normalized' )
waitforbuttonpress;
end
y = x.^2.0 + 0.5*z;
cv = cov( x, y );
cr = corrcoef( x, y );
scatter( x, y, 'filled', 'MarkerEdgeColor', 'white' );
xlabel( 'x' );
ylabel( 'y' );
text( 0.05, 0.9, sprintf( 'x^2', r ), 'Units', 'normalized' )
text( 0.05, 0.8, sprintf( 'cor = %.2f %.2f', cr(1,1), cr(1,2) ), 'Units', 'normalized' )
text( 0.05, 0.7, sprintf( 'cov = %.2f %.2f', cv(1,1), cv(1,2) ), 'Units', 'normalized' )