Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: performance benchmarking stata (and mata) programs and improving program performance

From   "Brent McSharry (ADHB)" <>
To   "" <>
Subject   RE: st: performance benchmarking stata (and mata) programs and improving program performance
Date   Wed, 24 Jul 2013 14:50:23 +1200

Thank you very much to both Mike and Sergiy

Using the timer functions, I was able to ascertain that no optimizations would make any difference away from the nested loop (ie over 99% of the time was spent in the nested part of the loop). Nonetheless the _st_data function was used as it can't hurt. Using the link to

gave the hint to use matrix multiplication (along with the range subscript notation (vertical bar within square brackets)[| ... |]), rather than a nested loop where possible.

Using the code below sped up execution on 6500 records from over 6.5 seconds to 250 milliseconds.

As per Sergiy erudite comments, the power calculation will approach zero as N approaches infinity. In reality, the scalar where 0 can be safely used as an approximation will be related to the resolution of the screen or print media. The program is sufficiently fast, that I have not implemented this at this stage, but worth thinking about if average run lengths were to get higher still.

Thank you all

	for (j=1;j<=usedNobs;j++) 
		currObs = ewmaObs[j,1] = _st_data(j,oIndx)*smooth + currObs *oneMinusSm
		expectJ = _st_data(j,eIndx)
		currExp = ewmaExp[j,1] = expectJ*smooth + currExp *oneMinusSm
		cumSeCalc = expectVar[j] = expectJ*(1-expectJ)
		ewmaSe[j,1] = smooth*sqrt(smoothPow[|1,(usedNobs-j+1) \ 1,usedNobs|] * expectVar[|1,1 \ j,1|])

//build lookup for power calculations (which tend to be computationally expensive)
real rowvector function getSmoothPow(real scalar oneMinusSm, real scalar length)
	real scalar i, oneMinusSm2, currPow
	real rowvector returnVar
	oneMinusSm2 = oneMinusSm * oneMinusSm
	returnVar = J(1,length,.)
	currPow = returnVar[1,length] = 1
	for (i=length-1;i>0;i--) 
		returnVar[1,i] = currPow = oneMinusSm2 * currPow

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index