*
* SWARCH.RPF
* Example of Markov switching ARCH model
*
* RATS User's Guide, Example from Section 11.7
*
open data g10xrate.xls
data(format=xls,org=columns) 1 6237 usxjpn
*
* Convert to percent daily returns
*
set x = 100.0*log(usxjpn/usxjpn{1})
*
* These set the number of regimes, and the number of ARCH lags. Because
* the likelihood depends only upon the current regime, we don't need a
* positive value for LAGS on @MSSETUP.
*
@MSSetup(regimes=3)
compute q=2
*
* HV will be the relative variances in the regimes. This will be
* normalized to a relative variance of 1 in the first regime, so it's
* dimensioned N-1.
*
dec vect hv(nregimes-1)
*
* This will be the vector of ARCH parameters
*
dec vect a(q)
*
* We have three parts of the parameter set: the mean equation parameters
* (here, just an intercept), the ARCH model parameters, and the Markov
* switching parameters, for which we'll use the logistic indexes.
*
nonlin(parmset=meanparms) mu
nonlin(parmset=archparms) a0 a hv
nonlin(parmset=msparms) theta
*
* uu and u are used for the series of squared residuals and the series of
* residuals needed to compute the ARCH variances.
*
clear uu u
*
* ARCHRegimeF returns a vector of likelihoods for the various regimes at
* time given residual e. The likelihoods differ in the regimes based upon
* the values of hv, where the intercept in the ARCH equation is scaled
* up by hv. Again, the elements of hv are offset by 1 since they're
* normalized with the first regime at 1.
*
* This is the Cai(1994) model, with the switch on the intercept in the
* variance equation.
*
function ARCHRegimeF time e ht
type vector ARCHRegimeF
type real e
type integer time
type vector *ht
*
local integer i j
local real vi
*
dim ARCHRegimeF(nregimes) ht(nregimes)
do i=1,nregimes
compute vi=a0*%if(i>1,hv(i-1),1)
do j=1,q
compute vi=vi+a(j)*uu(time-j)
end do i
compute ht(i)=vi
compute ARCHRegimeF(i)=%if(vi>0,$
%density(e/sqrt(vi))/sqrt(vi),0.0)
end do i
end
*
* As is typically the case with Markov switching models, there is no
* global identification of the regimes. By defining a fairly wide spread
* for hv, we'll hope that we'll stay in the zone of the likelihood where
* regime 1 is low variance, regime 2 is medium and regime 3 is high.
*
compute hv=||10,100||
*
* These are our guess values for the P matrix. We have to invert that to
* get the guess values for the logistic indexes.
*
compute p=||.8,.2,.05|.15,.6,.4||
compute theta=%msplogistic(p)
*
* These are the guess values for the other parameters. The ARCH lag
* parameters are started close to zero, A0 at the series variance. UU is
* initialized to the unconditional variance.
*
stats x
compute mu=%mean,a0=%variance,a=%const(0.05)
set uu = %variance
*
* This will hold the regime-specific variances at each time period
*
dec series[vect] regimeh
gset regimeh = %zeros(nregimes,1)
*
* We need to keep series of the residual (u) and squared residual (uu).
* Because the mean function is the same across regimes, we can just
* compute the residual and send it to ARCHRegimeF, which computes the
* likelihoods for the different ARCH variances.
*
frml logl = u(t)=(x-mu),uu(t)=u(t)^2,$
f=ARCHRegimeF(t,u(t),regimeh(t)),fpt=%MSProb(t,f),log(fpt)
*
@MSFilterInit
*
* In this case, the 1-->3 probability is effectively zero, which causes
* problems with the estimation. This pegs the theta(1,3) element to
* prevent that. In application to a different data set, this might (and
* probably wouldn't) be necessary.
*
nonlin(parmset=pegs) theta(1,3)=-50.00
*
* Because we need 2 lags of uu, the estimation starts at 3.
*
maximize(start=%(p=%mslogisticp(theta),pstar=%msinit()),$
parmset=meanparms+archparms+msparms+pegs,$
method=bfgs,iters=400,pmethod=simplex,piters=10) logl 3 *
*
@MSSmoothed %regstart() %regend() psmooth
set p1 = psmooth(t)(1)
set p2 = psmooth(t)(2)
set p3 = psmooth(t)(3)
*
spgraph(vfields=2,samesize,window="Switching ARCH")
*
* This graphs the smoothed probabilities over a shortened range. Across
* the full 6237 data points, the graph has too much movement to be
* usable.
*
graph(style=stacked,maximum=1.0,picture="##.##",$
header="Smoothed Probabilities of Variance States",key=below,$
klabels=||"Low Variance","Medium Variance","High Variance"||) 3
# p1 400 500
# p2 400 500
# p3 400 500
graph(picture="##.##",header="Data")
# x 400 500
spgraph(done)
*
* Compute the predictive standard deviation of the model. This weights
* the ARCH predicted variances across regimes by the predicted
* probabilities.
*
set predstddev = sqrt(%dot(pt_t1(t),regimeh(t)))
graph(footer="Predictive standard deviations")
# predstddev 400 500