
%% Create a multivariate normal distribution (k=2)



r=.999;

N=1000;
sig_x=2;
sig_y=4;
C=[sig_x^2 r*sig_x*sig_y; r*sig_x*sig_y sig_y^2]
R = chol(C);
xy = randn(N,2)*R;

figure; plot(xy(:,1),xy(:,2),'r.')

r=0;

N=1000;
sig_x=2;
sig_y=4;
C=[sig_x^2 r*sig_x*sig_y; r*sig_x*sig_y sig_y^2]
R = chol(C);
xy = randn(N,2)*R;

hold on; plot(xy(:,1),xy(:,2),'g.')

r=-.9;

N=1000;
sig_x=2;
sig_y=4;
C=[sig_x^2 r*sig_x*sig_y; r*sig_x*sig_y sig_y^2]
R = chol(C);
xy = randn(N,2)*R;

hold on; plot(xy(:,1),xy(:,2),'b.')

xlabel('X','fontsize',14);
ylabel('Y','fontsize',14);
legend('r=.999','r=0','r=-.9','fontsize',14)
title('bivariate distribution: \sigma_x=2, \sigma_y=4','fontsize',14)
axis equal




%% Create linear data and calculate the linear correlation coefficient


m=1;
b=.5;
e=.1;
x=0:.01:1;
nx = size(x,2);
y = m*x+b + e*randn(1,nx);

figure;plot(x,y,'.')
sig_x=std(x);
sig_y=std(y);
r=mean((x-mean(x)).*(y-mean(y)))/(sig_x*sig_y)
xlabel('X','fontsize',14);
ylabel('Y','fontsize',14);
s=sprintf('linear correlation coeff: %g\n',r)
legend(s)
axis equal








