* FDARFC
******************************************************************
* This procedure implements multi-period-ahead dynamic forecasts from
* a fractionally differenced series with an AR(p) representation
* The series is observed over range n1 to n2, and forecast for range
* n2+1 to n3
*
* series: the series to be forecast
* fcser : series for forecast series
* d     : fractional differencing parameter
* p     : order of the AR polynomial
* term0 : constant term of the AR polynomial
* theta : vector of p AR parameters
* n1    : first observation of observed series
* n2    : last observation of observed series; first obs in forecast
*         horizon is (n2+1)
* n3    : last observation in forecast horizon
*
* cfb/jb 6729 mod 6815 for term0
******************************************************************
procedure fdarfc series fcser d p term0 theta n1 n2 n3
type series series *fcser
type real d term0
type vec theta
type integer n1 n2 n3 p

option switch print 0

local series temp
local vector weightxx qwt
local integer ii kk ip jp

dim theta(p)
dim weightxx(n3) qwt(n3)

*** Calculates binomial expansion terms ****
comp weightxx(1) = d

do ii=2,n3
comp weightxx(ii) = weightxx(ii-1)*(ii-1-d) / float(ii)
end do ii

*** Calculate leading terms ****
comp qwt(1)=weightxx(1)+theta(1)
if p>1
{
do ip=2,p
comp qwt(ip)=weightxx(ip)+theta(ip)
   do jp=1,ip-1
   comp qwt(ip)=qwt(ip)-theta(jp)*weightxx(ip-jp)
   enddo jp
enddo ip
}
*
*** Calculate quasi-difference weight terms, given thetas ***
do ii=p+1,n3
comp qwt(ii)=weightxx(ii)
   do jj=1,p
   comp qwt(ii)=qwt(ii)-theta(jj)*weightxx(ii-jj)
   enddo jj
enddo ii
*
*** Place series into temp to facilitate dynamic forecasts ***
set temp n1 n2 = %if(%valid(series),series,0.0)

*** Calculate one-period ahead forecast series ***
set fcser n2+1 n3 = term0
do ii=n2+1,n3
do kk=1,ii-n1
set fcser ii ii = fcser(ii) + temp(ii-kk)*qwt(kk)
end do kk
set temp ii ii = fcser
end do ii

if print
{
dis 'forecast series is defined over' n2+1 n3
dis ' '
pri n2+1 n3 fcser
}

end
