clear all;
clc;
%========= fix seed ===============================
%in Matlab R2011b
s = RandStream('mt19937ar','Seed',0);
RandStream.setGlobalStream(s);
% rng(1111);
% stream = RandStream.getGlobalStream;
% reset(stream,0);
%-----------------------------LOAD DATA------------------------------------
Y = xlsread('experiment.xlsx');
% Number of observations and dimension of X and Y
t = size(Y,1);
M = size(Y,2);
% training sample | initialize Kalman filter
tau = 40; % size of training sample, CAN BE ALTERED according to the size of data
p = M;
plag = 2;
ylag = mlag2(Y,plag); % Y is [T x m]. ylag is [T x (nk)]
ylag = ylag(plag+1:t,:);
m = p + plag*(p^2); % dimension of parameters
% Create Z_t matrix
Z = zeros((t-tau-plag)*p,m);
for i = tau+1:t-plag
ztemp = eye(p);
for j = 1:plag
xtemp = ylag(i,(j-1)*p+1:j*p);
xtemp = kron(eye(p),xtemp);
ztemp = [ztemp xtemp]; %#ok
end
Z((i-tau-1)*p+1:(i-tau)*p,:) = ztemp;
end
y = Y(tau+plag+1:t,:)'; % sample used for estimation
t=size(y,2); % size of the sample used for estimation
%----------------------------PRELIMINARIES---------------------------------
% Set some Gibbs - related preliminaries
nrep = 50; % Number of replications
nburn = 0; % Number of burn-in-draws
apart = 1; % save every apart-th draw
it_print = 10; %Print in the screen every "it_print"-th iteration
%========= PRIORS:====================================
%========= PRIORS ON TRANSISION PROBS (Beta)
a_prob = .5;
b_prob = .5;
ap_0 = a_prob*ones(3,1);
bp_0 = b_prob*ones(3,1);
ap=zeros(3,1);
bp=zeros(3,1); %parameters for Beta distribution: alpha, beta
%=========PRIORS ON TIME-VARYING PARAMETERS AND THEIR COVARIANCES
[B_OLS,VB_OLS,A_OLS,sigma_OLS,VA_OLS]= ts_prior(Y,ylag(1:tau,:),tau,p,plag);
% Set some hyperparameters here (see page 831, end of section 4.1)
k_Q = 0.01;
%-------- Now set prior means and variances (_prmean / _prvar)
% B_0 ~ N(B_OLS, 4Var(B_OLS))
B_0_prmean = B_OLS;
B_0_prvar = 4*VB_OLS;
% Note that for IW distribution: scale and shape parameters...
% Q ~ IW(k2_Q*size(subsample)*Var(B_OLS),size(subsample))
Q_prmean = ((k_Q).^2)*tau*VB_OLS;
Q_prvar = tau;
%========= INITIALIZE MATRICES=======================
% Specify covariance matrices for measurement and state equations
consQ = 0.0001;
consH = 0.01;
Qdraw = consQ*eye(m);
Qchol = sqrt(consQ)*eye(m);
Ht = kron(ones(t,1),consH*eye(p));
Htsd = kron(ones(t,1),sqrt(consH)*eye(p));
Bdraw = zeros(m,t);
Zs = kron(ones(t,1),eye(p));
kdraw = 1*ones(t,3);
pdraw = .5*ones(1,