Gray JFE 1996 Markov Switching GARCH model

Use this forum for posting example programs or short bits of sample code.
jack
Posts: 160
Joined: Tue Sep 27, 2016 11:44 am

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by jack »

Thank you for your reply. I have learned many things from you at this forum.

I just have another question.
Distribution of my data is not normal and JB test clearly rejects the null hypothesis of normality. Can I use normal distribution for estimation of Gray's model?

In the following topic I also see that distribution of data is not normal but the normal distribution has been used for estimation of the model:
https://estima.com/forum/viewtopic.php? ... 130#p11130

It has been always a question for me: why sometimes normal distribution is used for estimation of the model while data is not normally distributed and we can also use t distribution (but we do not use it).
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by TomDoan »

The distribution of the data is never Normal when you're doing a ARCH/GARCH model. There were two main observations about typical financial data that ARCH/GARCH was designed to help explain: clustering of large residuals and fatter than Normal tails. It's quite possible for the data to be Normal conditioned on the time-varying variance but unconditionally very non-Normal.
jack
Posts: 160
Joined: Tue Sep 27, 2016 11:44 am

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by jack »

Thank you very much for your excellent explanation. I really enjoyed reading it.

Please let me ask this question clearly in this way:
When we estimate a GARCH model we have to option: using a normal distribution or t distribution. Which one is better for estimation when data is not normal?
It's quite possible for the data to be Normal conditioned on the time-varying variance but unconditionally very non-Normal.
Can we use normal distribution merely because "It's quite possible for the data to be Normal conditioned on the time-varying variance" or we have to test this possibility at first and after accepting it we can use the normal distribution?
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by TomDoan »

Most empirical work in GARCH models estimates by assuming a conditionally Normal distributions. This relies on the that fact that ML using the Normal distribution is a QMLE for the parameters.
jack
Posts: 160
Joined: Tue Sep 27, 2016 11:44 am

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by jack »

TomDoan wrote: Note, also, that the only thing that takes out of Dueker is the t-distributed errors. Other than that, they're using the Gray filter.
In this article the author says that:
msgarch.png
msgarch.png (41.07 KiB) Viewed 39948 times
https://link.springer.com/article/10.1007/s001810100100
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by TomDoan »

I'm not sure I understand your point. Yes, they used a t distribution. And yes, that's the only thing that they took out of Dueker.
jack
Posts: 160
Joined: Tue Sep 27, 2016 11:44 am

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by jack »

I thought using t- distribution may change the results. To tell the truth it has become a mental exercise for me!!!
jack
Posts: 160
Joined: Tue Sep 27, 2016 11:44 am

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by jack »

Dear Tom,

Here is the SP500 that the authors of this paper has used: Spot Market Volatility and Futures Trading: The Pitfalls of Using a Dummy Variable Approach In this paper Author try to model volatility shifts endogenously". http://onlinelibrary.wiley.com/doi/10.1 ... 3/abstract:

I want to somehow replicate their results because as you said they just use Gray's model with t distribution. When I use Gray's code for running the model I will get this error:

NO CONVERGENCE IN 5 ITERATIONS. FINAL NORMED GRADIENT 1.29843
ESTIMATION POSSIBLY HAS STALLED OR MACHINE ROUNDOFF IS MAKING FURTHER PROGRESS DIFFICULT
TRY DIFFERENT SETTING FOR EXACTLINE, DERIVES OR ALPHA ON NLPAR
RESTARTING ESTIMATION FROM LAST ESTIMATES OR DIFFERENT INITIAL GUESSES/PMETHOD OPTION MIGHT ALSO WORK

I would be grateful if you could possibly guide me about this problem.
I attached the data and code.
SP500.xlsx
(152.32 KiB) Downloaded 1052 times
SP500.RPF
(7.21 KiB) Downloaded 1260 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by TomDoan »

Gray is both a model (MS switching GARCH with switching AR(1) mean models), and an algorithm for collapsing regimes in a MS-GARCH model. The model part of that may be specific to what he was studying (interest rates), and he does an alternative model (GRS) that is very, very specifically for interest rates. You're trying to take a program for doing Gray's empirical work with interest rates and adapting it with effectively no changes to a completely different type of data. What guess values are appropriate for Gray's data may not be (and almost certainly aren't) reasonable for yours. These guess values are trying to separate the regimes based upon one regime having no persistence, and the other being "GARCH":

nonlin a01 a02 a11 a12 b01 b11 b21 b02 b12 b22 p
compute a01=%beta(1),a11=%beta(2),b01=olsvar,b11=0.0,b21=0.0
compute a02=%beta(1),a12=%beta(2),b02=%beta(3),b12=%beta(4),b22=%beta(5)
compute p=||.95,.05||

With stock market returns, the first branch is clearly not going to make sense which is why you're running into problems. You need to think about what you expect to be different about the regimes and set the guess values to try to separate them.
jack
Posts: 160
Joined: Tue Sep 27, 2016 11:44 am

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by jack »

Dear Tom,

I adjusted the Gray's code. Now, it uses t distribution instead of normal distribution for estimation of MS GARCH model.
I used several different guess values for estimation but I never got a result similar to those of authors'. For example, I used the estimated results of the paper as the initial guess values but it didn't converged.

I don't know what's wrong with the code.

Here is the code:
SP500 new .RPF
(6.65 KiB) Downloaded 1219 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by TomDoan »

First off, you sent a program with:

OPEN DATA "F:\rr.ex\brazil.xlsx"
CALENDAR(D) 1977:1:4
DATA(FORMAT=XLSX,ORG=COLUMNS,SHEET="Sheet2") 1977:01:04 *** SP500 return

which doesn't match your data set.

You also have an error in this:

compute a02=%beta(1),a12=%beta(2),b02=%beta(3),b12=%beta(4)%beta(4),b22=%beta(5)

with the repeated %beta(4)'s. So you want to get your program straightened out first.

This is the wrong adaptation to the t density:

compute RegimeGARCHF=||%tdensity((rate(t)-mu1)/sqrt(hh1),nu)/sqrt(hh1),$
%tdensity((rate(t)-mu2)/sqrt(hh2),nu)/sqrt(hh2)||

If you look at the Dueker example, it uses exp(%logtdensity(..., as that is what handles the t the way that GARCH models use the t. (With the GARCH variance giving the variance of the t distribution rather than the variance of the underlying Normal).
jack
Posts: 160
Joined: Tue Sep 27, 2016 11:44 am

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by jack »

I am sorry for those mistakes.

I tried several times for computing exp(%logtdensity(..... but I couldn't feed residuals (e) into it :( :( . I think I maybe have to adjust the whole structure of the code, so complicated. This is a sample code I used:

Code: Select all

*
* Table 3 estimates
*
* Single regime GARCH model
*
garch(p=1,q=1,reg,resids=u,hseries=h,distrib=t) / rate
# constant rate{1}
compute onestate=%beta
*
set usqr = u^2/h
@regcorrs(number=15,report) usqr
set usqr1 = u/h
@regcorrs(number=15,report) usqr1
*
* Regime-switching GARCH
*
nonlin a01 a02 a11 a12 b01 b11 b21 b02 b12 b22 mu nu p
compute a01=%beta(1),a11=%beta(2),b01=0.0685,b11=0.3955,b21=0.5452
compute a02=%beta(1),a12=%beta(2),b02=0.0014,b12=0.0193,b22=0.9836
compute nu=10
compute p=||.95,.05||
*
set uu = olsvar
set h  = olsvar
*
**************************************************************************
function GARCHRegimeResids t
type vector 	GARCHRegimeResids
type integer	t
*
* Compute the residuals for each current regime
*
local integer i
*
dim GARCHRegimeResids(nstates)
do i=1,nstates
   compute GARCHRegimeResids(i)=rate(t)-mu(i)
end do i
end
**************************************************************************
* RegimeGARCHF returns the 2 vector of densities in the two regimes.
* Again, many lines in this are specific to the problem at hand.
*
function RegimeGARCHF t
type vector RegimeGARCHF
type integer t
local real hh1 hh2 mu1 mu2 mu
local real       	e
*
* Compute state dependent variances
*
compute hh1=b01+b11*uu(t-1)+b21*h(t-1)
compute hh2=b02+b12*uu(t-1)+b22*h(t-1)
compute mu1=a01+a11*rate(t-1)
compute mu2=a02+a12*rate(t-1)
*
*Compute the residuals for each current regime
compute e=GARCHRegimeResids(t)
*
* Compute the return vector (densities in the two states)
*
compute RegimeGARCHF=||exp(%logtdensity((rate(t)-mu1)/sqrt(hh1),e,nu))/sqrt(hh1),$
                       exp(%logtdensity((rate(t)-mu2)/sqrt(hh2),e,nu))/sqrt(hh2)||
*
* Compute the values of uu (squared residual) and h (variance) to be
* used for the period following.
*
compute mu=mu1*pstar(1)+mu2*pstar(2)
compute uu(t)=(rate(t)-mu)^2
compute h(t)=pstar(1)*(mu1^2+hh1)+pstar(2)*(mu2^2+hh2)-mu^2
end
*
frml logl = f=RegimeGARCHF(t),fpt=%MSProb(t,f),log(fpt)
*
* This combination is able to locate Gray's results - the first MAXIMIZE
* works with the "p" matrix fixed at a fairly high level of persistence,
* and tries to get estimates which separate the two states. The second
* then adds the p matrix to the parmset.
*
nonlin a01 a02 a11 a12 b01 b11 b21 b02 b12 b22 nu
maximize(start=(pstar=%MSInit()),method=bfgs,iters=200,pmethod=simplex,piters=50) logl 2 ***
nonlin a01 a02 a11 a12 b01 b11 b21 b02 b12 b22 nu p
maximize(start=(pstar=%MSInit()),method=bfgs,iters=200) logl 2 ***
set usqr2 2 * = uu(t)/h(t)
@regcorrs(title="Squared Standardized Residuals from MS GARCH",$
number=15,report) usqr2
graph
# usqr2
@regcorrs(report,number=15) usqr2
@westchotest(number=5) usqr2
@westchotest(number=10) usqr2
@westchotest(number=15) usqr2
set usqr3 = usqr2^.5
@regcorrs(title="Standardized Residuals from MS GARCH",$
number=15,report) usqr3
graph
# usqr3
*
* Compute the smoothed probabilities and the ex ante probabilities
*
@MSSmoothed %regstart() %regend() psmooth
set exante %regstart() %regend() = pt_t1(t)(1)
set smooth %regstart() %regend() = psmooth(t)(1)
*
* And the conditional standard deviation
*
set condstddev = sqrt(h)
*
spgraph(vfields=2,window="Figure 5")
graph(max=1.0,header="Regime Probabilities") 2
# exante
# smooth
graph(header="Conditional Standard Deviation")
# condstddev
spgraph(done)
*
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by TomDoan »

That's not how %logtdensity works. It's designed specifically for handling a t in a GARCH context, so it takes the variance (or covariance matrix) as an argument---you don't have to rescale. Instead of

exp(%logtdensity((rate(t)-mu1)/sqrt(hh1),e,nu))/sqrt(hh1)

you want

exp(%logtdensity(hh1,e,nu))

and similarly for the other branch.
jack
Posts: 160
Joined: Tue Sep 27, 2016 11:44 am

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by jack »

Dear Tom,

I adjusted the code like following. Do I have to use two differents residulas (e) for computing RegimeGARCHF=||exp(%logtdensity(hh1,e1,nu1)),$
exp(%logtdensity(hh2,e2,nu2)) ?

I ran the model with different guess values but the model wasn't converged.

Code: Select all

*
* Regime-switching GARCH
*
nonlin a01 a02 a11 a12 b01 b11 b21 b02 b12 b22 nu1 nu2 p
compute a01=%beta(1),a11=%beta(2),b01=0.0,b11=0.0,b21=%beta(5)
compute a02=%beta(1),a12=%beta(2),b02=%beta(3),b12=%beta(4),b22=%beta(5)
compute nu1=%beta(6)
compute nu2=%beta(6)
compute p=||.95,.05||
*
set uu = olsvar
set h  = olsvar
*
* RegimeGARCHF returns the 2 vector of densities in the two regimes.
* Again, many lines in this are specific to the problem at hand.
*
function RegimeGARCHF t
type vector RegimeGARCHF
type integer t
local real hh1 hh2 mu1 mu2 mu
local real       	e1 e2
*
* Compute state dependent variances
*
compute hh1=b01+b11*uu(t-1)+b21*h(t-1)
compute hh2=b02+b12*uu(t-1)+b22*h(t-1)
compute mu1=a01+a11*rate(t-1)
compute mu2=a02+a12*rate(t-1)
compute e1=rate(t)-mu1
compute e2=rate(t)-mu2
*
*
* Compute the return vector (densities in the two states)
*
compute RegimeGARCHF=||exp(%logtdensity(hh1,e1,nu1)),$
                       exp(%logtdensity(hh2,e2,nu2))||
*
* Compute the values of uu (squared residual) and h (variance) to be
* used for the period following.
*
compute mu=mu1*pstar(1)+mu2*pstar(2)
compute uu(t)=(rate(t)-mu)^2
compute h(t)=pstar(1)*(mu1^2+hh1)+pstar(2)*(mu2^2+hh2)-mu^2
end
*
frml logl = f=RegimeGARCHF(t),fpt=%MSProb(t,f),log(fpt)
*
* This combination is able to locate Gray's results - the first MAXIMIZE
* works with the "p" matrix fixed at a fairly high level of persistence,
* and tries to get estimates which separate the two states. The second
* then adds the p matrix to the parmset.
*
nonlin a01 a02 a11 a12 b01 b11 b21 b02 b12 b22 nu1 nu2
maximize(start=(pstar=%MSInit()),method=bfgs,iters=300,pmethod=bfgs,piters=50) logl 2 ***
nonlin a01 a02 a11 a12 b01 b11 b21 b02 b12 b22 nu1 nu2 p
maximize(start=(pstar=%MSInit()),method=bfgs,iters=300) logl 2 ***
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Gray JFE 1996 Markov Switching GARCH model

Unread post by TomDoan »

Yes, the model has different means in the two regimes.

Again, you're using guess values that may have been appropriate for the interest rates in Gray's paper, but won't be for stock market returns. Also, wasn't their result that the main difference was in the degrees of freedom? Your guesses have the same degrees of freedom and so won't steer the estimates in the desired direction. With everything switching, this almost certainly has multiple modes.
Post Reply