% File mrsgarchestfor_con_all.m
% File to estimate Markov Regime-Switching GARCH
% Written by Juri Marcucci
% PhD student in Economics @
% Dept of Economics - UCSD
% Constrained optimization: fmincon
% The model is an M-state Markov Switching model with GARCH effects
% Main references: Gray (1996, Journal of Financial Economics)
% Klaassen (2002, Empirical Economics)
% Author: Juri Marcucci
% Dept. of Economics - UCSD
%
% Revised version: January 2011
% This program has been tested on new versions of Matlab (2008a, 2009a, 2010a).
clc
clear all
format compact
warning off
seconds=clock;
%Loading data
[y]=textread('sugar.txt');
N = size(y,1);
T=2380; % In-Sample: 1/1/1988-9/28/2001
dataf = 100*(log(y(2:N))-log(y(1:N-1))); % data to be forecast
N = N - 1;
clear y;
%ERRORS
datafm = mean(dataf); %mean of all the sample for the forecast error
errors = dataf - datafm; % errors
data = dataf(1:T); % In-Sample data
% ERRORTYPE -> || 1 = NORMAL || 2 = t-DF || 4 = t 1df || 5 = GED 1df ||
% In the following loop, just put 'errortype = [j]', j=(1,2,4,5) to estimate only one model reducing
% the computational burden. You can leave it as it is, but it would take around 4/5 days to run everything.
for errortype = [1 2 4 5];
data = dataf(1:T); % In-Sample data
% Initialization of the parameters
% Starting values
% x0(1,1) = a01; x0(2,1) = a02; x0(3,1) = b01; x0(4,1) = b02; x0(5,1) = b11;
% x0(6,1) = b12; x0(7,1) = b21; x0(8,1) = b22; x0(9,1) = p; x0(10,1) = q;
% x0(11,1) = \nu1; x0(12,1) = \nu2;
x0=[1.5*datafm; %a01
.5*datafm; %a02
.0005; %b01
.0002; %b02
.15; %b11
.25; %b12
.6; %b21
.7; %b22
.9; %p
.9 %q
4.5; %\nu1
4.5 %\nu2
];
switch errortype
case 1 %Normal
x0=x0(1:10,1);
case {2,3} % t & GED with 2DoF
x0 = x0(1:12,1) ;
case {4,5} % t and GED with 1DoF
x0 = x0(1:11,1);
otherwise
error('Variable errortype must be integer in (1,5)')
end
original_param = zeros(size(x0,1),N-T+1);
VF = [];
Loglike = zeros(size(x0,1),N-T+1);
Exitflag = zeros(size(x0,1),N-T+1);
Parameters = zeros(size(x0,1),N-T+1,11);
% Parameters for the conditional mean: a01, a02, i.e. x0(1 through 2)
% Parameters for the conditional variance: b01, b02, b11, b12, b21, b22, i.e. x0(3 through 8)
% Parameters for the P: p, q i.e. x0(9 through 10)
% Parameters for the DoF: \nu_j, j=1,2, i.e. x0(11 through 12)
% Print all results in a file
modelname = ['MRSGARCH_'];
switch errortype
case 1
distr_name = ['N'];
case 2
distr_name = ['t_2DoF'];
case 4
distr_name = ['t_1DoF'];
case 5
distr_name = ['GED_1DoF'];
otherwise
end
% Mac/Linux
filename=strcat('junk/',modelname,distr_name,'.txt')
% Windows
% filename=strcat('junk\',modelname,distr_name,'.txt')
fid = fopen(filename,'w');
tit1 = strcat('Model: \n',char(32),modelname,distr_name,' \n\n');
fprintf(fid,'Results from optimization In Sample \n');
fprintf(fid,tit1);