Page 1 of 1

Gray Regime Switching GARCH model

Posted: Tue Nov 30, 2010 7:46 pm
by joannytan
Hi,

I would like to implement the Gray's (1996) generalized regime switching GARCH model. I have tested with a sample series by changing the original codes to suit my data, it runs ok. However, I would like to run thousand of series using the same procedure to get the variance residuals (H) for each series. I have attached the selected sample data and sample modified programs below.

I have no idea on where to put the dofor and end dofor and how to stack the output of H in a matrix. Any ideas on this?

p/s: as in the code you will notice that the dependent variables is "satn", which is my first series.

Re: Gray Regime Switching GARCH model

Posted: Wed Dec 01, 2010 11:26 am
by TomDoan
Is the only thing changing from one to the next the dependent variable, or are you also changing the regressors? The former is relatively easy, since you just use a generic name like "Y" for the dependent variable in the RegimeGARCHF function, and you copy the next dependent variable into Y with a SET instruction at the top of the loop.

I'm not sure how you want to "stack" the residuals. Did you want to analyze them as cross sections?

Re: Gray Regime Switching GARCH model

Posted: Wed Dec 01, 2010 2:53 pm
by joannytan
Hi Tom,

Yes. It is just the changing of the dependent variable. As for the residuals, each dependent variable will have one series of residuals so (eg: 6 dependent variables = 6 columns of residuals).

As this is the first time to use RATS, I have no idea on how RATS could identify the first valid and the last valid observations, I tried the code below. It doesnt seems right, but it give me the result. However, should I put the code below inside the RegimeFunction? The reason being for this is each dependent variables will have different ending and starting time period (with NA if it is not valid).

Code: Select all

* this is to start the data with non missing values.
     *
	do t = 1, 2008:12
	if %valid(satn(t))==0
	com first=t+1
	end do t
	*
	* this is to end the data with valid values.
	*
	do t = 1, 2008:12
	if %valid(satn(t))==1
	com last=t
	end do t

Thanks.

Re: Gray Regime Switching GARCH model

Posted: Wed Dec 01, 2010 3:59 pm
by TomDoan
Try

Code: Select all

inquire(series=satn) first last

Re: Gray Regime Switching GARCH model

Posted: Thu Dec 02, 2010 6:05 am
by joannytan
Thanks Tom. I got the program working now. However, I do not know how to use the set instructions, thus I follow Enders' manual and do "scratch" (to create the H series) and use "dofor y= satn tmil apma auhs..." (for looping).

Anyway thanks again.

Re: Gray Regime Switching GARCH model

Posted: Thu Dec 02, 2010 7:32 pm
by joannytan
Hi, I did some mistake in the program, after the correction, it doesn't work anymore.

Code: Select all

OPEN DATA "data.txt"
CALENDAR(M) 1990
DATA(FORMAT=PRN,ORG=COLUMNS) 1990:01 2008:12 SATN TMIL APMA AUHS AUVE BSAI RP SMB HML
*
*
*
source markov.src
compute nstates=2
*
dec rect p(nstates-1,nstates)
*
dec vect pstar(nstates)
*
dec series[vect] pt_t pt_t1 psmooth
gset pt_t    1 2008:12 = %zeros(nstates,1)
gset pt_t1   1 2008:12 = %zeros(nstates,1)
gset psmooth 1 2008:12 = %zeros(nstates,1)
*
*
*
	*
	dofor y = SATN TMIL APMA AUHS AUVE BSAI
	linreg(noprint,robust) y
	# constant rp smb hml
	*
	compute olsvar=%seesq
	compute olsbeta=%beta
     *
	*
	garch(noprint, p=1,q=1,reg,resids=u,hseries=h) / y
	# constant rp hml smb
	compute onestate=%beta
	*
	set usqr = u**2/h
	*
	nonlin a01 a02 a11 a12 a21 a22 a31 a32 b01 b11 b21 b02 b12 b22 p
	compute a01=%beta(1),a11=%beta(2),a21=%beta(3), a31=%beta(4), b01=olsvar,b11=0.0,b21=0.0
	compute a02=%beta(1),a12=%beta(2),a22=%beta(3), a32=%beta(4), b02=%beta(5),b12=%beta(6),b22=%beta(7)
	compute p=||.95,.05||
	*
	set uu = olsvar
	set h  = olsvar
	*
	*
	function RegimeGARCHF t
	type vector RegimeGARCHF
	type integer t
	local real hh1 hh2 mu1 mu2 mu
	*
	* 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 state dependent means
	*
	compute mu1=a01+a11*rp(t)+a21*smb(t)+a31*hml(t)
	compute mu2=a02+a12*rp(t)+a22*smb(t)+a32*hml(t)
	*
	* Compute the return vector (densities in the two states)
	*
	compute RegimeGARCHF=||%density((y(t)-mu1)/sqrt(hh1))/sqrt(hh1),$
                       %density((y(t)-mu2)/sqrt(hh2))/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)=(y(t)-mu)**2
	compute h(t)=pstar(1)*(mu1**2+hh1)+pstar(2)*(mu2**2+hh2)-mu**2
	end
     *
	inquire(series=y) first last
     *
	frml logl = f=RegimeGARCHF(t),$
     pt_t1=%mcstate(p,pstar),pt_t=pstar=%msupdate(f,pt_t1,fpt),log(fpt)
	*
	nonlin a01 a02 a11 a12 a21 a22 a31 a32 b01 b11 b21 b02 b12 b22
	maximize(start=(pstar=%mcergodic(p)),method=bfgs,iters=100,pmethod=simplex,piters=50) logl first last
 	end dofor
I got the error of "PROCEDURE/FUNCTION Must be Initial Statement in a Compiled Section". I also have tried to put the RegimeGarch Function before I start the loop, however, the error is "SX17. Missing Operator or Adjacent Operands >>>>RCHF=||%density((y(<<<<".
Your help is much appreciated.

Re: Gray Regime Switching GARCH model

Posted: Thu Dec 02, 2010 9:58 pm
by TomDoan
Pull the function definition outside the loop.

Code: Select all

OPEN DATA "data.txt"
CALENDAR(M) 1990
DATA(FORMAT=PRN,ORG=COLUMNS) 1990:01 2008:12 SATN TMIL APMA AUHS AUVE BSAI RP SMB HML
*
*
*
source markov.src
compute nstates=2
*
dec rect p(nstates-1,nstates)
*
dec vect pstar(nstates)
*
dec series[vect] pt_t pt_t1 psmooth
gset pt_t    1 2008:12 = %zeros(nstates,1)
gset pt_t1   1 2008:12 = %zeros(nstates,1)
gset psmooth 1 2008:12 = %zeros(nstates,1)
*
dec series y

   function RegimeGARCHF t
   type vector RegimeGARCHF
   type integer t
   local real hh1 hh2 mu1 mu2 mu
   *
   * 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 state dependent means
   *
   compute mu1=a01+a11*rp(t)+a21*smb(t)+a31*hml(t)
   compute mu2=a02+a12*rp(t)+a22*smb(t)+a32*hml(t)
   *
   * Compute the return vector (densities in the two states)
   *
   compute RegimeGARCHF=||%density((y(t)-mu1)/sqrt(hh1))/sqrt(hh1),$
                       %density((y(t)-mu2)/sqrt(hh2))/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)=(tmil(t)-mu)**2
   compute h(t)=pstar(1)*(mu1**2+hh1)+pstar(2)*(mu2**2+hh2)-mu**2
   end
*
*
   *
   dofor y = SATN TMIL APMA AUHS AUVE BSAI
   linreg(noprint,robust) y
   # constant rp smb hml
   *
   compute olsvar=%seesq
   compute olsbeta=%beta
     *
   *
   garch(noprint, p=1,q=1,reg,resids=u,hseries=h) / y
   # constant rp hml smb
   compute onestate=%beta
   *
   set usqr = u**2/h
   *
   nonlin a01 a02 a11 a12 a21 a22 a31 a32 b01 b11 b21 b02 b12 b22 p
   compute a01=%beta(1),a11=%beta(2),a21=%beta(3), a31=%beta(4), b01=olsvar,b11=0.0,b21=0.0
   compute a02=%beta(1),a12=%beta(2),a22=%beta(3), a32=%beta(4), b02=%beta(5),b12=%beta(6),b22=%beta(7)
   compute p=||.95,.05||
   *
   set uu = olsvar
   set h  = olsvar
   *
   *
     *
   inquire(series=y) first last
     *
   frml logl = f=RegimeGARCHF(t),$
     pt_t1=%mcstate(p,pstar),pt_t=pstar=%msupdate(f,pt_t1,fpt),log(fpt)
   *
   nonlin a01 a02 a11 a12 a21 a22 a31 a32 b01 b11 b21 b02 b12 b22
   maximize(start=(pstar=%mcergodic(p)),method=bfgs,iters=100,pmethod=simplex,piters=50) logl first last
   end dofor

Re: Gray Regime Switching GARCH model

Posted: Fri Dec 03, 2010 2:10 am
by joannytan
Hi Tom,

Yes, I did tried this before with putting the function outside the loop but it seems like the function is not standalone, as this error appears "## SX11. Identifier B01 is Not Recognizable. Incorrect Option Field or Parameter Order? >>>> compute hh1=b01+b<<<<".

Re: Gray Regime Switching GARCH model

Posted: Fri Dec 03, 2010 7:09 am
by TomDoan
You'll have to do some additional re-arranging. The nonlin instruction needs to be pulled up above the function definition since it defines the B01 and others. You may need to add a few DECLARE instructions just to make sure that anything needed in the function are at least known variables, even if they aren't actually given working values until inside the loop.

Re: Gray Regime Switching GARCH model

Posted: Fri Dec 03, 2010 8:47 pm
by joannytan
Hi Tom, yes it is working fine now.
Thanks.

Re: Gray Regime Switching GARCH model

Posted: Wed Aug 03, 2011 5:06 am
by sana
joannytan wrote:Hi Tom, yes it is working fine now.
Thanks.

Hi
Please can I get the final code.
Your help will be much appreciated.
Thanks