Function inside a loop

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
Aktar
Posts: 35
Joined: Thu Apr 16, 2009 8:12 am

Function inside a loop

Unread post by Aktar »

Hi,

I estimate different specifications of a MS-TVTP model. I mean by different speficification, different set of lag (for the exogenous and transition variables). But i would like to make a loop to do it automatically. I arrived to creat a basic loop for estimation of a set of lag associated to the transition variable:

Code: Select all

************************************* Initialisation

	*** Terme autoregressif sur la variable endogene ***
compute nlags = 2

     *** Nombre de retards sur la veariable de transition ***
compute ivlags = 6

     *** Retards sur les exogenes
compute con1lags = 0

     *** Set the information variable 
set tvar = usa(t)
compute tvlabel = %label(usa)

     *** Set the dependent variable
set g = singapore(t)
compute enlabel = %label(singapore)

     *** set the exogenous variables
set con1 = malaysia(t-con1lags)
compute exlabel = %label(malaysia)

**************************************************************************************************
* Computes the vector of probabilities (levels, not logs) of the augmented states
* at period <<time>>. G (the data being analyzed), NSTATES, LAGSTATE and the
* parameters MU, PHI and SIGMA are all global variables.

function HamiltonF time
      type vector HamiltonF
      type integer time
             
      *** Declaring local variables
      local integer i j
      local real u

      *** Setting array size
      dim HamiltonF(nstates)

		*** e(t) = y(t) - mu(st=0) - phi(L)*(y(t-1)-mu(st-1)   if st = 0 ***
		*** e(t) = y(t) - mu(st=1) - phi(L)*(y(t-1)-mu(st-1)   if st = 1 ***

*** e(t) = y(t) - mu(st=0) -c1(st=0)*dfra_nlgq(t)-phi(L)*(y(t-1)-mu(st-1)   if st = 0 ***
*** e(t) = y(t) - mu(st=1) -c1(st=1)*dfra_nlgq(t)-phi(L)*(y(t-1)-mu(st-1)   if st = 1 ***

      *** with mu(st) = mu(0)+mu(1)St, St={0,1} ***

         do i=1,nstates

         compute u=g(time)-mu(lagstate(i,1))-d1(lagstate(i,1))*con1(time)

			do j=1,nlags

     	 		compute u=u-phi(lagstate(i,j+1),j)*(g(time-j)-mu(lagstate(i,j+1)) $
	     	       -d1(lagstate(i,j+1))*con1(time))

               end do j

	    compute HamiltonF(i)=%density(u/sigma)/sigma

      end do i

      end      

      *** LOGLFTP computes the log likelihood for a fixed transition probability model
      frml loglftp = f=HamiltonF(t),pt_t1=HamiltonPt_t1(),pt_t=pstar=%msupdate(f,pt_t1,fpt),log(fpt)

      *** LOGLTVTP computes the log likelihood for a time-varying transition model
      frml logltvtp = TVPProbs(t),f=HamiltonF(t),pt_t1=HamiltonPt_t1(),pt_t=pstar=%msupdate(f,pt_t1,fpt),log(fpt)


*****************************************************************************************
**** Compuation

do ivlags=1,6

		disp ' '
      disp '====================================================================================================='
      disp "                    Markov-switching FTP vs TVTP vs AR(" nlags ") for " tlabel
      disp '====================================================================================================='
      disp '====================================================================================================='
      disp "                                Variable of information: " ilabel " with " ivlags " lags"
      disp '====================================================================================================='



	 ***
      linreg g
      # constant con1 g{1 to nlags}

      compute a1=log(.70/(1-.70)), a2=log(.85/(1-.85))
      compute mu(1)=%beta(1),mu(2)=%beta(1)
      compute sigma =sqrt(%seesq)
      compute d1(1)= %beta(2), d1(2)= %beta(2)
      compute phi = %const(%beta(3))
*****************************************************************************************
*
* Maximize
*

	        *** Perform the Broyden, Fletcher, Goldfarb and Shanno (bfgs) method
                maximize(parmset=standard,start=(pstar=MarkovInit()),method=bfgs,pmethod=simplex,piters=40) loglftp 1976:01 *

		***
      compute baselogl=%funcval,baseregs=%nreg
      compute p11f=p11,p22f=p22

      *** Initialize non-linear parameters
      nonlin(parmset=tv) a11 a22
      compute a11 = 0.0, a22 = 0.0

      ***
      frml p1frml = a1+a11*tvar(t-ivlags)
      frml p2frml = a2+a22*tvar(t-ivlags)


		*** Perform the Broyden, Fletcher, Goldfarb and Shanno (bfgs) method

     maximize(parmset=standard+tv,start=(pstar=MarkovInit()),method=bfgs,pmethod=simplex,piters=40) logltvtp %regstart() *
   
end do ivlags

In my MS-TVTP, i have exogenous variables (named con1) in equation states. The lag associated is con1lags. After choosed a con1lag, i run my prog and for my model with con1lag=1, i have 6 specifications, indeed one specification for each ivlags (1 to 6) which is the lag associated to the transition variable. It is better than running 6 times manually the prog.

This is the loop in the precedent code:

Code: Select all

Do ivlags=1,6
....
end do ivlags


What i try to do, is to estimate automatically 36 specifications. And for this i must creat a loop for con1lags. In this way i will have for con1lags=0, 6 spec with different ivlags, for con1lags=2, again 6 spec different ect ect up to con1lags=5

the variable con1 is define like this:

Code: Select all

set con1 = malaysia(t-con1lags)
compute exlabel = %label(malaysia)
with con1lags = the lag of the exogenous variable

The problem is that con1 is needed to computing the function HalmiltonF. And i need to create a loop with this function inside but i think that it is impossible. Moreover, in the function it is no con1lags but con1, so i must put in the function:

Code: Select all

set con1 = malaysia(t-con1lags)
compute exlabel = %label(malaysia)
But it is impossible to set a variable in a function.

aybe the better way is to use another object than a function?
My goal is to have something like this

Code: Select all

do con1lags=0,5
....
do ivlags=1,6
.....
end do ivlags
end do con1lags
For resume, the probleme is that i don't know how to manage with the hamiltonF function for create my loop.

I can join my prg if necessary.

Thanks

Regards
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Function inside a loop

Unread post by TomDoan »

The newer way to handle that type of model is with the MSVARSETUP procedure. It's fairly easy to put that inside a loop, as in this example:

Code: Select all

*
* FRUEHP366.RPF
* Fruehwirth-Schnatter, "Finite Mixture and Markov Switching Models"
* GDP example from pp 366-370
*
open data gnp.dat
calendar(q) 1951:2
data(format=free,skip=1,org=columns) 1951:02 1984:04 gnp gnplag ggrowth
*
@msvarsetup(lags=0,switch=m)
# ggrowth
frml msvarf = log(%MSVARProb(t))
*
* Common estimation range
*
compute gstart=1952:2,gend=1984:4
nonlin(parmset=msparms) theta
nonlin(parmset=varparms) mu phi sigma
dofor parts=2 3
   dofor lags=0 1 2 3 4
      @msvarsetup(lags=lags,states=parts,switch=m)
      # ggrowth
      *
      @msvarinitial gstart gend
      *
      * Estimate the model by maximum likelihood.
      *
      maximize(parmset=varparms+msparms,$
        start=%(p=%MSLogisticP(theta),pstar=%MSVARInit()),$
        reject=%MSVARInitTransition()==0.0,$
        pmethod=simplex,piters=5,method=bfgs,iters=300) msvarf gstart gend
      disp "Switching Intercepts" lags "lags" parts "states" "SBC=" -2.0*%funcval+log(%nobs)*%nreg
   end dofor parts
end dofor lags
Post Reply