%Problem Set 7 Due Wednesday Oct. 17, 2007 close all; %closes all figures clc; %clears the screen clear; %clears the memory %set the normal and unit density seeds randn('seed',1234); rand('seed',0); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %1. a. %m x n matrix m = 100; n = 1; %mean and variance mu = 2; sigma2 = 5; %generate the random draws x = mu + sqrt(sigma2)*randn(m,n); %plot the vector %zero line z = zeros(m,n); figure; plot(x,'k'); hold on; plot(z,'k'); title('100 draws from N(2,5)'); xbar = mean(x); %2.265487480783972 xmedian = median(x); % 2.310105346957458 s = std(x); %2.183299902847172 s2 = var(x); %4.766798465772472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %1. b. %m x n matrix m = 100; n = 3; x = zeros(m,n); %mean and variance mu = [0.98 ; -1.44 ; 0.99]; sigma = [1, 0.5, -0.1 ; 0.5, 2, 0.3; -0.1, 0.3, 1.5]; p = chol(sigma)'; testsigma = p*p'; %generate the random draws for j = 1:1:100 x(j,1:3) = (mu + p*randn(3,1))'; end %plot the vector figure; plot(x(:,1),'k'); hold on; plot(x(:,2),'k-o'); plot(x(:,3),'k-+'); plot(z,'k'); title('100 draws from N(\mu,\Sigma)'); %xbar is a 1x3 mean and median of the columns xbar = mean(x); % [0.7866,-1.4172,1.2386;] xmedian = median(x); % [0.7794,-1.3346,1.2339;] s = std(x); % [0.9997,1.4362,1.1181;] %covariance of the second and third vectors % note this is a 2x2, 1st and 2nd rows % are divided by ";" cov23 = cov(x(:,2),x(:,3)); % [2.0628,0.2907; 0.2907,1.2502;] % but we only care about the off-diagonal elements because the variances % are on the diagonal. %pearson's correlation coefficient is: p23 = cov23/(s(1,2)*s(1,3)); %2x2 matrix [1.2845,0.181;0.181,0.7785;] % and we only car about "0.181" %alternatively we could use the built in function to calculate pearson's % but we get the same result. p23builtin = corr(x(:,2),x(:,3)); % 0.181034440554344 %Here are the correlation coefficients among all the series. R = corrcoef(x); % 3x3 matrix [1,0.3619,-0.0678;0.3619,1,0.181;-0.0678,0.181,1;]