%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This code computes GMM estimates of the Consumption-CAPM model with
% CRRA utility
% We use data from 15 assets and aggregate consumption.
Copyright By PowCoder代写 加微信 powcoder
% We estimate the parameters using GMM in two stages.
% We compute the standard errors using the symbolic math tool.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Cleaning the workspace and preliminary setup
clear variables;
close all;
%%%%%%%%%%%%%%%%%%%
%Loading the data
%%%%%%%%%%%%%%%%%%%
% We use 15 portfolio returns sorted based on size and book-to-market
portfolio_data = xlsread(‘portf_me_bm_quarterly.xlsx’);
% We use inflation-adjusted consumption data (the data set includes consumption of nondurables and services)
consumption_data = xlsread(‘Real_consumption.xlsx’);
% Both data sets should start in 1967. So, we cut the longer consumption data
consumption = consumption_data(81:end-3,:);
% Reshape return data
% The command vec2mat unstacks the returns and puts the 15 portfolio returns on one row for every quarter
returns = [consumption(:,1),consumption(:,2),vec2mat(portfolio_data(:,5),15)];
% We sum nondurables consumption and service consumption
cons = consumption(:,3)+consumption(:,4);
% number of observations
T = length(cons);
% number of assets
% number of parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ESTIMATION: We do it in 2 stages
% First stage: using the identity matrix
% Second stage: using the optimal weighting matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%
% FIRST stage
%%%%%%%%%%%%%%%%%%
%First-stage weigthing matrix (identity matrix)
W = eye(15);
% —————————————————————————–
% Note: In order to compute (1) the moment conditions and (2) the criterion
% we write two “gmm” functions below (see the end of the code)
% —————————————————————————–
% Parameters used to initialize the optimization (it is always a good idea to change these parameters to evaluate robustness)
para = [0.2;10];
% The command “fminsearch” finds the parameter estimates,
% i.e., para_opt, given inputs. The inputs are the criterion to minimize
% (given by the function “gmm_criterion”),
% the initial parameters (in “para”), options (listed below),
% data (cons and returns), and the first-stage weight matrix (W)
% set options for fminsearch (first stage)
options = optimset(‘Display’,’iter’,’TolFun’,1e-5,’TolX’,1e-5);
% first stage: minimize gmm criterion to find the parameters estimates
disp(‘First stage’);
%%%%%%%%%%%%%%%%%
% SECOND stage
%%%%%%%%%%%%%%%%%
% We use the optimal variance/covariance matrix (See LECTURE 4)
% We evaluate the moment conditions at the optimal (first-stage) parameter values
m_opt = gmm_moment(para_opt,cons,returns);
%The following loop calculates the variance/covariance amtrix
%We initialize the matrix Phi_hat to zero first
Phi_hat = zeros(15,15);
for i = 1:T-1
Phi_hat = Phi_hat + m_opt(i,:)’*m_opt(i,:)/T;
% invert Phi_hat to obtain the optimal weight matrix
W_opt = inv(Phi_hat);
% set options for fminsearch (second stage)
options = optimset(‘Display’,’iter’,’TolFun’,1e-5,’TolX’,1e-5);
% second stage estimation
disp(‘Second stage’);
%Next, we compare the first and second stage point estimates
table_estimates = table(para_opt,para_opt2,’VariableNames’,…
[“Stage1 estimates” “Stage2 estimates”],…
‘RowNames’,[“beta” “gamma”]);
disp(table_estimates);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INFERENCE: computing the asymptotic variance matrix
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% First: estimate \Phi_0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Moment conditions evaluated at the second-stage estimates
m_opt2 = gmm_moment(para_opt2,cons,returns);
% compute the optimal weight matrix using second stage estimates
Phi_hat2 = zeros(15,15);
for i = 1:T-1
Phi_hat2 = Phi_hat2 + m_opt2(i,:)’*m_opt2(i,:)/T;
% Optimal weight matrix based on the optimal second-stage estimates
W_opt2 = inv(Phi_hat2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Second: estimate matrix Gamma_0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Step (1): Define symbolic characters and the moments in symbolic terms
syms beta gamma
param = [beta; gamma];
%Compute symbolic moment conditions
m = zeros(T-1, N,’sym’);
m(:,i) = param(1)*(cons(2:T)./cons(1:T-1)).^(-param(2)).*(1+returns(2:T,2+i)/100) -1;
%Step (2): Compute the gradients in symbolic terms
gra = zeros(length(param), length(m(:,1)), N , ‘sym’);
for j=1:length(m(:,1))
gra(:,j,i)=gradient(m(j,i),[beta gamma]);
% Initialize and compute matrix Gamma_hat
Gamma_hat = zeros(d, N, ‘sym’);
Gamma_hat(:,i) = mean(gra(:,:,i),2);
% Transpose to have a N x d matrix
Gamma_hat = Gamma_hat’;
% Step (3): Replace the “symbolic” parameters with the second stage estimates
Gamma_hat(i,:) = subs(Gamma_hat(i,:), [beta gamma], [para_opt2(1), para_opt2(2)]);
% Step (4): Go from symbolic gradients to numerical
Gamma_hat = double(Gamma_hat);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% THIRD: Putting everything together
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute variance-covariance matrix for parameter estimates
var = (1/T)*inv(Gamma_hat’*W_opt2*Gamma_hat);
% Variances are on the diagonal
var_diag = diag(var);
stderror = sqrt(var_diag);
% Displaying the parameter estimates and the t-statistics
t_stats = para_opt2./stderror;
%Table for parameters and inference
table_estimates = table(para_opt,para_opt2,stderror,t_stats,’VariableNames’,…
[“Stage1” “Stage2” “StdError” “t_stats”],…
‘RowNames’,[“beta” “gamma”]);
disp(‘table of estimates’);
disp(table_estimates);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Test of over-identifying restrictions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
criterion = gmm_criterion(para_opt2,cons,returns,W_opt2); % criterion evaluated at the optimal second-stage estimates
test = T*criterion; % Hansen’s test
Pvalue = 1 – chi2cdf(test,15-2);
fprintf(‘The p-value of the test is %2.6f. \n’,Pvalue);
fprintf(‘Hence, the test rejects the null of zero pricing errors at the 0.05 level. \n’)
fprintf(‘——————————————– \n’);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Defining important functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f = gmm_moment(para,consumption,returns)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% para vector of parameters to evaluate the function
% consumption consumption vector
% returns return data matrix
% f pricing errors (for each observation – rows – and each asset – columns)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% parameters to use for computation
para = [para(1); para(2)];
% initialize the matrix of moments
m = zeros(length(consumption)-1,15);
% compute moment for each observation
m(:,1) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,3)/100) -1;
m(:,2) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,4)/100) -1;
m(:,3) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,5)/100) -1;
m(:,4) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,6)/100) -1;
m(:,5) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,7)/100) -1;
m(:,6) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,8)/100) -1;
m(:,7) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,9)/100) -1;
m(:,8) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,10)/100) -1;
m(:,9) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,11)/100) -1;
m(:,10) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,12)/100) -1;
m(:,11) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,13)/100) -1;
m(:,12) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,14)/100) -1;
m(:,13) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,15)/100) -1;
m(:,14) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,16)/100) -1;
m(:,15) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,17)/100) -1;
% Note that this computes the pricing errors for each observation and asset
% Observations are in rows and each column corresponds to a different asset
function f = gmm_criterion(para,consumption,returns,W)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% para vector of parameters to evaluate the function
% consumption consumption vector
% returns return data matrix
% W weigthing matrix
% f value of the gmm criterion
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% parameters to use for computation
para = [para(1); para(2)];
% initialize the matrix of moments
m = zeros(length(consumption)-1,15);
% compute moment for each observation
m(:,1) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,3)/100) -1;
m(:,2) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,4)/100) -1;
m(:,3) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,5)/100) -1;
m(:,4) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,6)/100) -1;
m(:,5) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,7)/100) -1;
m(:,6) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,8)/100) -1;
m(:,7) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,9)/100) -1;
m(:,8) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,10)/100) -1;
m(:,9) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,11)/100) -1;
m(:,10) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,12)/100) -1;
m(:,11) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,13)/100) -1;
m(:,12) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,14)/100) -1;
m(:,13) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,15)/100) -1;
m(:,14) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,16)/100) -1;
m(:,15) = para(1)*(consumption(2:end)./consumption(1:end-1)).^(-para(2)).*(1+returns(2:end,17)/100) -1;
% compute the (15 times 1) moment vector
mm = mean(m)’;
% compute the gmm criterion
f = mm’*W*mm;
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com