CS代写 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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;

% Uploading the data
d = xlsread(‘portf_me_bm_quarterly.xlsx’); % We use 15 portfolio returns sorted based on size and book-to-market
d1 = xlsread(‘Real_consumption.xlsx’); % We use inflation-adjusted consumption data (the data set includes consumption
% of nondurables and services)

con_data = d1(81:end-3,:); % Both data sets should start in 1967. So, we cut the longer
% consumption data

ret_data = [con_data(:,1),con_data(:,2),vec2mat(d(:,5),15)]; %The command vec2mat unstacks the returns and puts the 15 portfolio returns
%on one row for every quarter

cons = con_data(:,3)+con_data(:,4); % We sum nondurables consumption and service consumption

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ESTIMATION: We do it in 2 stages
% First stage: using the identity matrix
% Second stage: using the optimal weighting matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%
% FIRST stage
%%%%%%%%%%%%%%%%%%

num_port = 15;
W = eye(num_port); % This is the first-stage (identity) matrix

% —————————————————————————–
% Note: In order to compute (1) the moment conditions and (2) the criterion
% we write a “gmm” function below (see the end of the code)
% By changing the “flag” we are switching from moments to criterion
% NOTE: The less efficient version of the code writes 2 functions rather than 1 with a flag
% —————————————————————————–

para = [0.2;10]; % Parameters used to initialize the optimization

% The command “fminsearch” finds the first-stage parameter estimates, i.e., para_opt,
% given inputs. The inputs are the criterion to minimize (given by the function “gmm” with “flag = 1” – see code at the end),
% the initial parameters (in “para”), options (listed below), data (cons and ret_data),
% and the first-stage weight matrix (W)

options = optimset(‘Display’,’none’,’TolFun’,1e-5,’TolX’,1e-5);
% @ is the “handle” which is used to pass the function “gmm” into another function

% Next, we evaluate the moment conditions at the optimal (first-stage) parameter values
% This is done by using the function “gmm” with “flag = 2” (see, again, the
% function “gmm” defined below)

m_opt = gmm(para_opt,cons,ret_data,W,2);

%%%%%%%%%%%%%%%%%%%
% SECOND stage
%%%%%%%%%%%%%%%%%%

% The following loop calculates the optimal weight matrix

H = length(m_opt);
sum = zeros(num_port);

for i = 1:H
sum = sum + (1/H)*m_opt(i,:)’*m_opt(i,:);

W_opt = inv(sum); %Optimal weight matrix based on the first-stage estimates

options = optimset(‘Display’,’iter’,’TolFun’,1e-5,’TolX’,1e-5);
%Second stage optimization

% Next, we compare the first and second stage 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

m_opt2 = gmm(para_opt2,cons,ret_data,W,2); % Moment conditions evaluated at the optimal second-stage estimates

sum2 = zeros(num_port);

for i = 1:H
sum2 = sum2 + (1/H)*m_opt2(i,:)’*m_opt2(i,:);

W_opt2 = inv(sum2); % Optimal weight matrix based on the optimal second-stage estimates

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Second: estimate matrix Gamma_0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

param = [a;b];

m = zeros(H,num_port,’sym’);

for col = 1:num_port

m(:,col) = param(1)*(cons(2:end)./cons(1:end-1)).^… % Step (1): Define symbolic characters and the moments in symbolic terms
(-param(2)).*(1+ret_data(2:end,col+2)/100) -1; % NOTE: The less efficient version of the code computes these objects one by one without a loop

for grad_num = 1:num_port

temp_grad = zeros(2,size(m,1),’sym’);
for j=1:size(m,1)
temp_grad(:,j)=gradient(m(j,grad_num),[a b]); % Step (2): Compute the gradients – one-by-one – in symbolic terms
temp_grad=mean(temp_grad,2);
temp_grad=temp_grad’;
temp_grad=subs(temp_grad, [a b], para_opt2′); % Step (3): Replace the “symbolic” parameters with the optimal ones
temp_grad=double(temp_grad); % Step (4): Go from symbolic gradients to numerical
gra = [gra;temp_grad]; % NOTE: the less efficient version of the code does this in several steps

var = (1/H)*inv(gra’*W_opt2*gra); % This is the GMM variance in its “sandwich” format

diag = diag(var);
stderror = sqrt(diag);

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(para_opt2,cons,ret_data,W_opt2,1); % criterion evaluated at the optimal second-stage estimates
test = H*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 the GMM function
% If you select flag = 1, you obtain the criterion to be optimized.
% If you select flag = 2, you obtain the moment conditions, one by one.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function f = gmm(para,cons,ret_data,W,flag)

num_port = size(ret_data,2)-2;
m = zeros(length(cons)-1,num_port);

for col = 1:num_port

m(:,col) = para(1)*(cons(2:end)./cons(1:end-1)).^…
(-para(2)).*(1+ret_data(2:end,col+2)/100) -1; %The less efficient version of the code computes these objects one by one without a loop

mm = mean(m);
if flag ==1
f = mm*W*mm’;
elseif flag==2

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com