You are on page 1of 5

%Data Description

%Data saved in the file Data.mat, obtained from French Library


%2 Matrices:
%Factors Matrix contains data of three factors 196001:201212,
%Order is RM,SMB,HML,Rf,operation is needed to create excess return.
%Return matrix has 25 columns representing 25 portforlios sorted by
%market cap and book to market ratio.
%Order is Small size(low to high) to Big size(low to high)
clc;clear;load Data.mat;
%%Problem 1
Str = {'intercept';'smb';'hml';'excess'};
[m,n] = size(Return);
%create excess return
R_excess = Factors(:,1);
%create OLS's X
X = [ones(m,1),Factors(:,2:3),R_excess];
%Do regression on 25 portforlios
%initializations
beta = zeros(n,4);
tScore = zeros(n,4);
rsquare = zeros(n,1);
std_return = zeros(n,1);
Y = zeros(m,n);
resid = zeros(m,n);
u = zeros(m,n);
%beta is a coefficient matrix. order is intercept,smb,hml,excess return,
%top to bottom: Small size(low to high) to Big size(low to high)
for i = 1:n
Y(:,i) = Return(:,i) - Factors(:,4);
beta(i,1:4) = (X'*X)^(-1)*X'*Y(:,i);
inver = (X'*X)^(-1);
resid(:,i) = Y(:,i)- X * beta(i,:)';
u(:,i) = resid(:,i);
rsquare(i) = 1 - var(resid(:,i))/var(Y(:,i));
std_return(i) = std(resid(:,i));
for j = 1: 4
tScore(i,j) = beta(i,j)/sqrt(1/m*u(:,i)'*u(:,i)*inver(j,j));
end
end
% for j = 2:4
% disp(Str(j))
% fprintf('%.4f %.4f %.4f %.4f %.4f\n',beta(:,j))
% fprintf(' t-value\n\n')
% fprintf('%.4f %.4f %.4f %.4f %.4f\n',tScore(:,j))
% end
% disp('R square')
% fprintf('%.4f %.4f %.4f %.4f %.4f\n',rsquare)
% disp('standard error')
% fprintf('%.4f %.4f %.4f %.4f %.4f\n',rsquare)
%Comment
%Everything stays the same;
%% Problem 2
%construct the covariance matrix
covResid = resid'*resid/(m-3-1);
%construct the factor portfolio matrix
fmean(1,1:3) = mean(X(:,2:4));
factorMean = zeros(m,3);
for i = 1:m
factorMean(i,1:3) = fmean;
end
factorPo = X(:,2:4);
sigma = 1/m * ((factorPo - factorMean)' * (factorPo - factorMean));
%it follows F distribution F(25,608)
GRS = (m/25)*(m-25-3)/(m-3-1)*beta(:,1)'*covResid^(-1)*beta(:,1)/(1+fmean*sigma^
(-1)*fmean');
%Comment
%GRS score is 3.0136, for .05 significant value is slightly greater 1, so
%the GRS score is greater significantly which says we will reject the model
%and say alpha is not jointly zero
%% Problem 3: HAC newey west
%Comment: the problem asks to do t test, however, for a standard t test, a
%square root of chi square distribution is needed for denominator and
%I can't prove (HAC standard error)/(sigma square)/(n-k) follows chi square
%distribution, I will just assume it's true here.
tScore_HAC = zeros(25,1);
for i = 1:n
% Initialization
S = zeros(1,1);
% q Specifies the Maximum Number of Lags to Use
q = 10;

% This is Just the Variance-Covariance Matrix
S = S + autocov(0,u(:,i),m);
% This Following is the Adjustment for Autocorrelation and
% Heteroskedasticy
for v = 1:q
Gv = autocov(v,u(:,i),m); % Call function 'autoc' that computes k-th ord
er covariance matrix
S = S + (1-(v/(q+1)))*(Gv+Gv');
clear Gv
end
inver = (X'*X)^(-1);
for j = 1:4
tScore_HAC(i,j) = beta(i,j)/sqrt(S*inver(j,j));
end
end
%%
%Problem 4
mr = X(:,2:4); % market return
f1 = X(:,2);
f2 = X(:,3);
f3 = X(:,4);
T = length(mr);
const = ones(T,1);
x = [const,mr]; % RHS variables
[m,k] = size(x);
e_store = []; % Stores OLS residuals
ef_store = []; % Stores residuals from second moment condition E(fe)=0
ef2_store = [];
ef3_store = [];
b_store = []; % Stores estimates
for j = 1:25
% Time-Series Regressions, Decile-by-Decile
y = Y(:,j);
b = x\y; % OLS estimates
e = y - x*b; % Calculates residuals
ef = u(:,j).*mr(:,1); % For second moment matrix
ef2 = u(:,j).*mr(:,2);
ef3 = u(:,j).*mr(:,3);
% Stores estimates, residuals, etc. in a BIG matrix for all regressions
e_store = [e_store,e];
ef_store = [ef_store,ef];
ef2_store = [ef2_store,ef2];
ef3_store = [ef3_store,ef3];
b_store = [b_store,b];
% Calculates Standard Errors
[se, sigma] = leastsquares_se(e,x,m,k);
% Calculates t-statistics and stores in BIG matrix
stat = b(1)/se(1);
statvec(j) = stat;
% Displays Estimates and Standard Errors
end
u = [e_store,ef_store,ef2_store,ef3_store];
Sig = zeros(100,100);
q = 5;
Sig = Sig + autocov(0,u,T);
for i = 1:q
Gv = autocov(i,u,T);
Sig = Sig + (1-(i/(q+1)))*(Gv+Gv');
clear Gv
end
A = [1, mean(f1),mean(f2),mean(f3);
mean(f1),mean(f1.^2),mean(f1.*f2),mean(f1.*f3);
mean(f2),mean(f2.*f1),mean(f2.^2),mean(f2.*f3);
mean(f3),mean(f3.*f1),mean(f3.*f2),mean(f3.*f3)];
I = eye(25,25);
d = -kron(A,I);
V = inv(T)*inv(d)*Sig*(inv(d))';
stat = b_store(1,:)*inv(V(1:25,1:25))*b_store(1,:)';
%% Problem 5
%Plot 2 plots
%actual excess return
actual = mean(Y);
%predicted excess return
pred = mean(X(:,2:4)) * beta(:,2:4)';
type = {'-x','-o','-+','-*','-s'};
ltype = num2str(cell2mat(type));
figure
hold on
for k = 1:5
plot(pred([k,k+5,k+10,k+15,k+20]),actual([k,k+5,k+10,k+15,k+20]),strcat(ltyp
e(2*k-1),ltype(2*k)))
end
%do the same thing for period 2007:08 to 2010:01
actual2 = mean(Y(572:601,:));
pred2 = mean(X(572:601,2:4)) * beta(:,2:4)';
type2 = {'-x','-o','-+','-*','-s'};
ltype2 = num2str(cell2mat(type2));
figure
hold on
for k = 1:5
plot(pred2([k,k+5,k+10,k+15,k+20]),actual2([k,k+5,k+10,k+15,k+20]),strcat(lt
ype2(2*k-1),ltype2(2*k)))
end
hold off
%Now the plot is a total mess
%% Problem 6
% First, when we use excess returns, the model should predict a zero
% intercept, but even after we use the joint test on alphas, still it is not
% zero. So the model fails.
% Second, the model is unstable across the time, for the period from 2007 to
% 2010, the prediction can't match the actual data. This suggests that for a
% relatively long(4 years) period, the model fails to predict. So using this
% model could hurt investors.
% Third, although r square is mathmatically big, above 0.9, this cannot
% explain the variation well and did a poor job in predicting.
%% Problem 7
actual2 = mean(Y(577:588,:));
pred2 = mean(X(577:588,2:4)) * beta(:,2:4)';
type2 = {'-x','-o','-+','-*','-s'};
ltype2 = num2str(cell2mat(type2));
% figure
% hold on
% for k = 1:5
% plot(pred2([k,k+5,k+10,k+15,k+20]),actual2([k,k+5,k+10,k+15,k+20]),strcat(
ltype2(2*k-1),ltype2(2*k)))
% end
% hold off
YY = zeros(60,n);
XX = X(577:636,:);
for i = 1:n
YY(:,i) = Y(577:636,i);
betaB(i,1:4) = (XX'*XX)^(-1)*XX'*YY(:,i);
inver = (XX'*XX)^(-1);
residR(:,i) = YY(:,i)- XX * betaB(i,:)';
uu(:,i) = residR(:,i);
rsquareS(i) = 1 - var(residR(:,i))/var(YY(:,i));
std_return(i) = std(residR(:,i));
for j = 1: 4
tScoreT(i,j) = betaB(i,j)/sqrt(1/m*uu(:,i)'*uu(:,i)*inver(j,j));
end
end
%Running the test with data from 1980:01 to 2012:12 shows that size effect
%is significant for 24/25 portfolios. Only one beta on SMB is not
%significant. Since what Fama-French(1992) did was a regression on ME and
%book/ME, showing that it's weaker for the time period of 1977 to 1991.
%However, I think the result is not very convincing due to the
%multicolinearity problem. And our regression shows SMB still performs well
%in explaining.

You might also like