CS代考 QUESTION 10

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This code computes GMM estimates of the Consumption-CAPM model
% We use data on 10 risky assets, 1 risk-free asset and consumption growth.
% The data are monthly observations.

Copyright By PowCoder代写 加微信 powcoder

% We estimate the parameters using GMM.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% clean the workspace
clear variables;
close all;

% load the data from excel file
data = readtable(‘ccapmmonthlydata.xls’);

% return data
returns = table2array(data(:,3:12));

% consumption growth data (c_{t+1} / c_{t})
cons_growth = table2array(data(:,2));

%%%%%%%%%%%%%%%%%
% QUESTION 3: first stage
%%%%%%%%%%%%%%%%%

% number of observations
T = length(cons_growth);
% number of assets
% number of parameters

% first-stage weigthing matrix (identity matrix)
W = eye(N);

% parameters used to initialize the optimization
para = [1;5];

% set options for fminsearch – notice that the display is off
options = optimset(‘Display’,’off’,’TolFun’,1e-5,’TolX’,1e-5);

% minimize the gmm criterion to find the parameters estimates

%%%%%%%%%%%%%%%%%%%%%
% QUESTION 4
%%%%%%%%%%%%%%%%%%%%

% evaluate the moment conditions at the optimal (first-stage) parameter values
m_opt = gmm_moment(para_opt,cons_growth,returns);

% compute the optimal weight matrix, using first-stage estimates
Phi_hat = zeros(N,N);
for i = 1:T
Phi_hat = Phi_hat + m_opt(i,:)’*m_opt(i,:)/T;
W_opt = inv(Phi_hat);

% set options for fminsearch – notice that the display is off
options = optimset(‘Display’,’off’,’TolFun’,1e-5,’TolX’,1e-5);

% Second-stage estimation

%%%%%%%%%%%%%%%%%%
% QUESTION 5
%%%%%%%%%%%%%%%%%%

% see pdf file

%%%%%%%%%%%%%%%%%%
% QUESTION 6
%%%%%%%%%%%%%%%%%%

% moment conditions evaluated at the second-stage estimates
m_opt2 = gmm_moment(para_opt2,cons_growth,returns);

% Compute optimal weight matrix using second-stage estimates
Phi_hat2 = zeros(N,N);
for i = 1:T
Phi_hat2 = Phi_hat2 + m_opt2(i,:)’*m_opt2(i,:)/T;

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ESTIMATING the LAMBDA_HAT matrix of derivatives
% method 1: use matlab symbolic functions
% method 2: take derivative by hand and plug into the matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%
% METHOD 1
%%%%%%%%%%%%

% step (1): Define symbolic characters and the moments in symbolic terms
syms beta gamma
param = [beta;gamma];

m = zeros(T,N, ‘sym’);

% compute pricing errors for each asset and time period
m(:,i) = param(1)*(cons_growth(1:T)).^(-param(2)).*(1+returns(1:T,i)) -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 Lambda_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 gradients
Gamma_hat = double(Gamma_hat);

% compute variance-covariance matrix for parameter estimates
var = (1/T)*inv(Gamma_hat’*W_opt2*Gamma_hat);

% variance is on the diagonal
var_diag = diag(var);
stderror = sqrt(var_diag);

% t-statistics
t_stats = para_opt2./stderror;

%%%%%%%%%%%%%%
% METHOD 2 (Notice how much easier this is …)
%%%%%%%%%%%%%%

% use derivatives directly in the loop
Lambda_hat_h = zeros(N,d);
Lambda_hat_h(i,1) = mean( (cons_growth(1:T).^(-para_opt2(2))).*(1+returns(1:T,i)) );
Lambda_hat_h(i,2) = mean(-para_opt2(1).*log(cons_growth(1:T)).*(cons_growth(1:T).^(-para_opt2(2))).*(1+returns(1:T,i)) );

% compute variance-covariance matrix for the parameter estimates
var_h = (1/T)*inv(Lambda_hat_h’*W_opt2*Lambda_hat_h);

% variance is on the diagonal
var_diag_h = diag(var_h);
stderror_h = sqrt(var_diag_h);

% t-statistics
t_stats_h = para_opt2./stderror_h;

% table for parameters and inference
table_estimates = table(para_opt,para_opt2,stderror,t_stats,stderror_h,t_stats_h,’VariableNames’,…
[“Stage1” “Stage2” “StdError” “t_stats”, “StdError_h”, “t_stats_h”],…
‘RowNames’,[“beta” “gamma”]);
disp(‘table of estimates’);
disp(table_estimates);

%%%%%%%%%%%%%%%%%
% QUESTION 7
%%%%%%%%%%%%%%%%%%%%

% test of single restriction
t_test = (para_opt2(1) – .9)./stderror(1); % test statistic
p_value_t = 2.*(1 – cdf(‘T’, abs(t_test), T-2)); % p-value using t distribution
p_value_N = 2.*(1 – cdf(‘Normal’, abs(t_test), 0,1)); % p-value using the Normal distribution

fprintf(‘—– Question 7 ———\n’);
fprintf(‘The value of the t statistic is %4.4f \n’, t_test);
fprintf(‘The value of the normal p value is %4.4f \n’, p_value_N);
fprintf(‘The value of the t p value is %4.4f \n’, p_value_t);

%%%%%%%%%%%%%%%%%%%
% QUESTION 8
%%%%%%%%%%%%%%%%%%%%

% test of multiple restriction
R = eye(2);
r = [0.9; 4];

Wald = (R*para_opt2 – r)’*inv(R*var*R’)*(R*para_opt2 – r);
p_value_wald = 1 – cdf(‘chi2’, Wald, 2);

fprintf(‘—– Question 8 ———\n’);
fprintf(‘The value of the Wald statistic is %4.4f \n’, Wald);
fprintf(‘The value of the p value is %4.4f \n’, p_value_wald);

%%%%%%%%%%%%%%%%%%
% QUESTION 9
%%%%%%%%%%%%%%%%%%%

R = [50 -1];

Wald_linear = (R*para_opt2 – r)’*inv(R*var*R’)*(R*para_opt2 – r);
p_value_wald_linear = 1 – cdf(‘chi2’, Wald_linear, 1);

fprintf(‘—– Question 9 ———\n’);
fprintf(‘The value of the Wald statistic is %4.4f \n’, Wald_linear);
fprintf(‘The value of the p value is %4.4f \n’, p_value_wald_linear);

%%%%%%%%%%%%%%%%%
% QUESTION 10
%%%%%%%%%%%%%%%%%%%%%

% Test of over-identifying restrictions
Overid = gmm_criterion(para_opt2,cons_growth,returns,W_opt2); % criterion evaluated at the optimal second-stage estimates
test = T*Overid; % Hansen’s test
Pvalue_overid = 1 – chi2cdf(test,N-d); % compute p-value according to the Chi-square

fprintf(‘—– Question 10 ———\n’);
fprintf(‘The value of the test statistic is %4.4f \n’, test);
fprintf(‘The value of the p value is %4.4f \n’, Pvalue_overid);

function f = gmm_criterion(para,consumption,returns,W)

% para vector of parameters
% consumption consumption growth vector
% returns returns data matrix
% W weigthing matrix

% f value of the gmm criterion

data_size = size(returns);
N = data_size(2);
T = data_size(1);

% parameters to use for computation
para = [para(1); para(2)];

% initialize the matrix of pricing errors
m = zeros(T,N);

% compute pricing errors
m(:,i) = para(1)*(consumption(1:end)).^(-para(2)).*(1+returns(1:end,i)) -1;

% average pricing errors across time (for each asset)
mm = mean(m)’;

% compute gmm criterion
f = mm’*W*mm;

function f = gmm_moment(para,consumption,returns)

% para vector of parameters
% consumption consumption growth vector
% returns returns data matrix

% f pricing error matrix (for each observation and each asset)

data_size = size(returns);
N = data_size(2);
T = data_size(1);

% parameters to use for computation
para = [para(1); para(2)];

% initialize the matrix of pricing errors
m = zeros(T,N);

% compute pricing errors
m(:,i) = para(1)*(consumption(1:end)).^(-para(2)).*(1+returns(1:end,i)) -1;

f = m; %observations are in rows, assets are in columns

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