Programming for a large number of permutations

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.
Masafumi
Posts: 9
Joined: Wed May 09, 2012 12:48 pm

Programming for a large number of permutations

Unread post by Masafumi »

Hi there,

I am trying to estimate time-varying variance decompositions for 7 variables over 200-week rolling sample windows, using Cholesky factorization.
I wrote a program pasted at the bottom (based on the program shared at http://www.estima.com/forum/viewtopic.p ... old+yilmaz), but am getting two problems for which I need your help.

First, running the program returns a following error message.
## MAT13. Store into Out-of-Range Matrix or Series Element
The Error Occurred At Location 213, Line 12 of loop/block
It is probably due to a following line, but I could not figure out how I can resolve this.

Code: Select all

set %s(%string(count)+"_from_"+%l(i)+"_to_"+%l(j)) end end = gfevd(j,i)(asteps)*100
Could you let me know how I should correct the program?

Second, I would like to repeat the process for 120 different orderings.
In the program below, you find I made two different orderings and a loop for them using dofor command:

Code: Select all

dec vect[int] order1
enter(varying) order1; # 1 2 3 4 5 6 7
dec vect[int] order2
enter(varying) order2; # 1 2 3 4 5 7 6
*
*
dofor order = order1 order2
*
*
However, what I need to do is a bit more complicated: among 7 variables (USA, EUR, KEN, TZA, UGA, RWA, BDI), I would like to fix USA and EUR at the first and second orders, respectively, and try all possible orderings among the other 5 variables.
It gives me 5! = 120 orderings.
It is next to impossible to establish 120 orderings as I do above.
Could you let me know how I can efficiently establish the 120 orderings and repeat the loop for the orderings?

Thank you so much for your support, and any advice is highly appreciated.

Best,

Masafumi

Code: Select all

*********************************************************************
* Replication file for Diebold and Yilmaz(2009),  "Measuring Financial
* Asset Return and Volatility Spillovers, with Application to Global
* Equity Markets," Economic Journal, vol. 119, no. 534, 158-171.
*
* Analysis of returns data
*
* If you make usegirf=1, this will use the generalized spillover
* measures proposed in Diebold and Yilmaz(2011), "Better to Give than to
* Receive: Predictive Directional Measurement of Volatility Spillovers",
* IJF, to appear. usegirf=0 does the Choleski factorization used in the
* 2009 paper.
*********************************************************************
*
compute usegirf=0
*
OPEN DATA NEER_EAC.XLS
CALENDAR(W) 2000:1:9
DATA(FORMAT=XLS,ORG=COLUMNS,LEFT=2,DATEFORM="m/d/y") 2000:01:09 2012:09:09 $
R_USA R_EUR R_KEN R_TZA R_UGA R_RWA R_BDI
*
Labels 1 2 3 4 5 6 7
# 'USA' 'EUR' 'KEN' 'TZA' 'UGA' 'RWA' 'BDI'
*
*********************************************************************
* Produce the appropriate "factor" matrix from %sigma
*
function FactorMatrix
type rect FactorMatrix
if usegirf
   compute FactorMatrix=%sigma*inv(%diag(%sqrt(%xdiag(%sigma))))
else
   compute FactorMatrix=%decomp(%sigma)
end
*********************************************************************

*********************************************************************
* Creating different orders
*********************************************************************
dec vect[int] order1
enter(varying) order1; # 1 2 3 4 5 6 7
*
dec vect[int] order2
enter(varying) order2; # 1 2 3 4 5 7 6
*
*********************************************************************
* Rolling window analysis
*********************************************************************
compute nspan=200
		* Width of rolling windows = 200 weeks
compute nsteps=10, asteps=4
		* Compute variance decompositions up to 10 step and analyze the 4 step responses
compute rstart=2000:01:09, rend=2012:09:09
*
compute count = 0
dofor order = order1 order2
	compute count=count+1
	do end=rstart+nspan-1,rend
		* Setup and estimate one lag VAR
			system(model=var)
			variables order
			lags 1
			det constant
			end(system)
			* Coefficients Restricted
				compute coeffs = %modelgetcoeffs(var)
				compute coeffs(3,1) = 0
				compute coeffs(4,1) = 0
				compute coeffs(5,1) = 0
				compute coeffs(6,1) = 0
				compute coeffs(7,1) = 0
				compute coeffs(3,2) = 0
				compute coeffs(4,2) = 0
				compute coeffs(5,2) = 0
				compute coeffs(6,2) = 0
				compute coeffs(7,2) = 0
				compute %modelsetcoeffs(var, coeffs)
      *
  		estimate(noprint) end-nspan+1 end
   	*
   	compute gfactor=FactorMatrix()
   	errors(model=var,steps=nsteps,factor=gfactor,stderrs=gstderrs,noprint,results=gfevd)
		do i=1,7
				do j=1, 7
					set %s(%string(count)+"_from_"+%l(i)+"_to_"+%l(j)) end end = gfevd(j,i)(asteps)*100
				end do j
		end do i
	end do end
end dofor
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Programming for a large number of permutations

Unread post by TomDoan »

First off, have you contacted Diebold and/or Yilmaz to see if they have any thoughts on what you're trying to do? It's possible that they've already thought about issues like this.

It's not clear why you need all orderings of the final five countries. It sounds like you want impulse vectors for those last five which have them ordered after the two large sectors, but before the other four. If that's the case, then all you need is one for each which puts them in the 3rd position. How you order 4-7 is irrelevant to the calculation.
Masafumi
Posts: 9
Joined: Wed May 09, 2012 12:48 pm

Re: Programming for a large number of permutations

Unread post by Masafumi »

Dear Tom,

Thank you for your reply.
Diebold and Yilmaz compute the spillover index for 100 different orderings using Cholesky factorization in their 2011 paper (http://www.nber.org/papers/w17490).
In my model, while the large two sectors are obviously exogenous to the other five, it is not clear how the remaining five influence each other.
So I would like to compute spillovers for all possible permutations to see how the result is affected by the orderings.
(Maybe I can show the average of the orderings as a representative result.)

Hope this is clear enough, and thank you so much for your support.

All the best,

Masafumi
Post Reply