*
*  @BJTRANS  series  start  end
*
*  BJTrans helps choose a preliminary transformation from
*  among none, square root and log. It graphs rescaled versions
*  of each transformation on a single graph, and includes the
*  log likelihoods of one lag autoregressions on the various
*  power transformations.
*
*  Revision schedule:
*    03/2003 Written by Tom Doan, Estima
*
proc BJTrans series start end
type series series
type integer start end
*
local vect[series] transform
local vect[string] labels
local vect[real]   l
local vect[real]   value
local real         logy range
local integer      i s nbeg nend
local string       header
local string       format
local vect[string] klabels
*
dim transform(3) labels(3) l(3) value(3) klabels(3)
compute l=||1.0,0.5,0.0||
compute labels=||"None","Sqrt","Log "||
*
inquire(series=series) nbeg<<start nend<<end
*
*  Compute the sum of the logs of the series. This is needed
*  later, and lets us check for non-positive values
*
sstats nbeg nend log(series)>>logy
if .not.%valid(logy) {
   disp "Series has non-positive values. BJTRANS can't analyze"
   return
}
*
*  Find the max and min. We use this in rescaling the series
*
ext(noprint) series nbeg nend
do i=1,3
   if (i==1)
      set transform(i) nbeg nend = series
   else
   if (i==3)
      set transform(i) nbeg nend = log(series)
   else
      set transform(i) nbeg nend = series**l(i)/l(i)
*
*     Compute the log likelihood of the data assuming that
*     an autoregression on power transformed data yields
*     (i.i.d) Normal residuals
*
   linreg(noprint) transform(i) nbeg+1 nend
   # constant transform(i){1}
   compute value(i)=(l(i)-1)*logy - (%nobs*.5*log(%seesq/%nobs))
*
*     Rescale the data so that the first series runs from 0-2,
*     the second from 1-3 and the third from 2-4. This allows us
*     to graph all three on a single graph without as much
*     vertical squeezing. We should only run into overlap problems
*     if the series has achieves both its highest and lowest values
*     in a relatively short span of time, which is highly unlikely
*     for strictly positive data.
*
   compute range = transform(i)(%maxent)-transform(i)(%minent)
   set(scratch) transform(i) nbeg nend = (i-1)+2.0*(transform(i)-transform(i)(%minent))/range
end do i
*
display(store=header) 'Transformations of' %label(series)
*
*  We use the key labels to show the likelihood values.
*
do i=1,3
   disp(store=klabels(i)) labels(i)+'\\' *.### value(i)
end do i
*
list s = transform
spgraph(window='Transformations')
graph(scale=none,header=header,key=below,noksample,klabels=klabels) 3
cards s nbeg nend 1
do i=1,3
   grtext(entry=nbeg,align=right,$
     y=%if(transform(i)(nbeg)>(i-1)+.50,transform(i)(nbeg)-.25,transform(i)(nbeg)+.25)) labels(i)
end do i
*
spgraph(done)
end

