程序代写 SP500daily_level.xlsx’);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% We evaluate the maximum likelihood estimator of a GARCH(1,1) process
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Copyright By PowCoder代写 加微信 powcoder

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Let us clean the environment
close all; clear all; clc;

% We upload the data
data=xlsread(‘SP500daily_level.xlsx’);

% We go from prices to log (or continuously-compounded) returns

prices=data(2:end,2);
r = log(prices(2:end)./prices(1:end-1));
T = length(r);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Always plot the data (when you can): why do we need a GARCH model? Let’s see …
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

title(‘SP 500 returns in percentage terms’)
ylabel(‘returns’)
xlabel(‘time’)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Let us now maximize the log-likelihood, find the parameter estimates, and do inference
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Estimation, first
parameter_names = [“mu” “delta” “phi”]’;

theta_initial=[0,0.5,0.5]; %Initial conditions (I am just choosing reasonable values, but you can of course change them and see what happens)

options = optimset(‘Display’,’none’,’TolFun’,1e-20,’TolX’,1e-20);

%Now, inference

% Below, we compute the gradient and, with the gradient, the asymptotic
% variance/covariance matrix directly (by taking derivatives “by hand”).

sum4 = zeros(3,3);
h = var(r)*ones(T,1);
grad1_h = zeros(T,1);
grad2_h = zeros(T,1);
grad3_h = zeros(T,1);

h(t) = theta(1)+theta(2)*h(t-1)+theta(3)*r(t-1)*r(t-1);

grad1_h(t) = 1 + theta(2)*grad1_h(t-1);
grad2_h(t) = h(t-1) + theta(2)*grad2_h(t-1);
grad3_h(t) = r(t-1)*r(t-1) + theta(2)*grad3_h(t-1);

grad_h = [grad1_h(t); grad2_h(t); grad3_h(t)];

grad = -(1/2)*1/h(t)+(1/2)*r(t)*r(t)/(h(t)^2);

grad = grad.*grad_h;

sum4=sum4+grad*grad’;

asy_var = (1/T)*inv((1/T)*sum4);
t_stats = theta’./sqrt(diag(asy_var));

outcome = table(parameter_names,theta’,t_stats,…
‘VariableNames’,{‘parameters’ ‘Estimates_MLE’ ‘t_statistics’});
disp(outcome);

%%%%%%%%%%%%%%%%%%%%%%
% Value-at-Risk
%%%%%%%%%%%%%%%%%%%%%%%

h_next = theta(1)+theta(2)*h(end)+theta(3)*r(end)*r(end);
VaR = 1000000*2.33*sqrt(h_next);

fprintf(‘The 1%% Value-at-Risk for an investor holding a “market-like” portfolio is %5.2f \n’,VaR)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is the function which obtains the log-likelihood given an assumption
% of normality on the error terms
% As always, the function is defined at the bottom of the code and then called
% in the fminsearch routine
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function y=garchloglik(theta,data)

h1=var(data); % Recall h needs to be initiated (we loop over h)
% Initial value of h: we use the unconditional
% empirical variance from the data

T = length(data);

mu=theta(1);
delta=theta(2);
phi=theta(3);

sum1=-T/2*log(2*pi);
sum2=-0.5*log(h1);
sum3=-data(1)*data(1)/(2*h1);

h = mu+delta*h+phi*data(t-1)*data(t-1);
sum2=sum2-0.5*log(h);
stand_res=data(t)*data(t)/(2*h);
sum3=sum3-stand_res;
y= -(1/T)*(sum1+sum2+sum3); % Note that I changed the sign of the likelihood. We are maximizing, not minimizing.
% The function garchloglik can now be fed into the
% fminsearch function

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