% Lab 7 Solutions %------------------------------------------------------------------------- % Question 1 % Q1(a) load S_rio.txt % Plot adjusted share price: figure(1) len=length(S_rio); plot(len:-1:1,S_rio) xlabel('Trading Day') ylabel('Rio') % Q1(b) % Percentage daily returns: R_rio = 100*(S_rio(1:len-1)-S_rio(2:len))./S_rio(2:len); % Plot histogram: figure(2) hist(R_rio,20) xlabel('Rio % Daily Return') ylabel('Frequency') % Mean, standard deviation, variance: mu_rio=mean(R_rio) sigma_rio=std(R_rio) var_rio=sigma_rio^2 % Plot normal distribution: x=linspace(mu_rio-3*sigma_rio,mu_rio+3*sigma_rio,101); y=exp(-(x-mu_rio).^2/(2*var_rio))/(sqrt(2*pi)*sigma_rio); hold on plot(x,(len-1)*y,'m') % Annualised values: mu_rio_yr=365*mu_rio sigma_rio_yr=365*sigma_rio var_rio_yr=sigma_rio_yr^2 % Q1(c) load S_nab.txt figure(3) len=length(S_nab); plot(len:-1:1,S_nab) xlabel('Trading Day') ylabel('NAB') % Percentage daily returns: R_nab = 100*(S_nab(1:len-1)-S_nab(2:len))./S_nab(2:len); % Plot histogram: figure(4) hist(R_nab,20) xlabel('NAB % Daily Return') ylabel('Frequency') % Mean, standard deviation, variance: mu_nab=mean(R_nab) sigma_nab=std(R_nab) var_nab=sigma_nab^2 % Plot normal distribution: x=linspace(mu_nab-3*sigma_nab,mu_nab+3*sigma_nab,101); y=exp(-(x-mu_nab).^2/(2*var_nab))/(sqrt(2*pi)*sigma_nab); hold on plot(x,(len-1)*y,'m') % Annualised values: mu_nab_yr=365*mu_nab sigma_nab_yr=365*sigma_nab var_nab_yr=sigma_nab_yr^2 % Q1(d) %Covariance matrix of Rio and NAB: cov_rio_nab=cov(R_rio,R_nab) %Correlation matrix of Rio and NAB: rho_rio_nab=corrcoef(R_rio,R_nab) %------------------------------------------------------------------------- % Question 2 % Run the following commands in Matlab: % (a) n=50; p=0.3; k=0:n; for i=0:n P(i+1)=nchoosek(n,i)*p^i*(1-p)^(n-i);end figure(5) subplot(3,2,1) plot(k,P,'.') hold on p=0.5; k=0:n; for i=0:n P(i+1)=nchoosek(n,i)*p^i*(1-p)^(n-i);end plot(k,P,'r.') title('Q2(a)') hold off %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (b) m=3; r=[0:10]; p=exp(m)*m.^r./gamma(r+1); subplot(3,2,2) plot(r,p,'.') hold on m=5; p=exp(m)*m.^r./gamma(r+1); plot(r,p,'r.') title('Q2(b)') hold off %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (c) k=[1:10]; p=0.3; q=1-p; P=q.^(k-1)*p; subplot(3,2,3) plot(k,P,'.') hold on p=0.7; q=1-p; P=q.^(k-1)*p; plot(k,P,'r.') title('Q2(c)') hold off %------------------------------------------------------------------------- % Question 3 % Run the following commands in Matlab: % (a) a=2; b=5; % Make f in three pieces: x1=[2*a-b:0.01:a]; y1=zeros(size(x1)); x2=[a:0.01:b]; y2=ones(size(x2)); x3=[b:0.01:2*b-a]; y3=zeros(size(x3)); x=[x1 x2 x3]; f=[y1 y2 y3]/(b-a); subplot(3,2,4) plot(x,f) title('Q3(a)') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (b) lambda=2; % Make f in two pieces: x1=[-lambda:0.01:0]; y1=zeros(size(x1)); x2=[0:0.01:4*lambda]; y2=exp(-x2/lambda)/lambda; x=[x1 x2]; f=[y1 y2]; subplot(3,2,5) plot(x,f) title('Q3(b)') axis([-lambda 4*lambda 0 1]) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (c) mu=0; sigma=1; x=-3*sigma:0.01:3*sigma; f=exp(-(x-mu).^2/(2*sigma^2))/(sqrt(2*pi)*sigma); subplot(3,2,6) plot(x,f) title('Q3 Normal Distribution')