version 8.0
drop _all
clear
set memory 80m
set matsize 800
set more off
capture log close

log using "O:\DATA\PR\PRECAUTIONARY\fisca.log", replace


insheet using "O:\DATA\PR\FundCredit\fco.txt"



destring gexb fcharge nexbcb tot ncf pli sra srach oiluk stdbt tdbt totsd rgdpsd gdpsd rexsd, replace float force

tsset dates, yearly
gen dp90=0
gen dp97=0
replace dp90=1 if dates>1990 
replace dp97=1 if dates>1997
gen fcoxo=fco-fcormv

gen dfco=ln(fcon/fcon[_n-1])

gen demgdp=ln((gdp*sdr)/(gdp[_n-1]*sdr[_n-1]))
gen dfcogdp=dfco-demgdp
gen dfcoa=ln((fconxea)/(fconxea[_n-1]))
gen dfcoxo=ln((fcoxo)/(fcoxo[_n-1]))
gen dfcoo=ln((fcormv)/(fcormv[_n-1]))
gen qrat=ln((quota/1000))
replace qrat=ln((quota/1000)/gdpa) if dates>2005
gen lqrat=qrat[_n-1]

gen ldfco=dfco[_n-1]
gen ldfcoa=dfcoa[_n-1]
gen ldfcoxo=dfcoxo[_n-1]
gen ldfcoo=dfcoo[_n-1]
gen l2dfco=dfco[_n-2]
gen lspr=(1+braspr[_n-1])/100

gen ltotsd=totsd[_n-1]
gen ldrexoffsd=drexoffsd[_n-1]
gen ldrexsd=drexsd[_n-1]
gen lrgdpsd=rgdpsd[_n-1]
gen lgdpsd=gdpsd[_n-1]

gen dwgdp=ln(wgdp/wgdp[_n-1])
gen dtotsd=totsd-totsd[_n-1]
gen drgdpsd=rgdpsd-rgdpsd[_n-1]
gen dgdpsd=gdpsd-gdpsd[_n-1]

gen ldtotsd=dtotsd[_n-1]
gen ldrgdpsd=drgdpsd[_n-1]
gen ldgdpsd=dgdpsd[_n-1]

gen dwtr=(wtr-wtr[_n-1])/100
gen dx=ln(x/x[_n-1])
gen lx=ln(x[_n-1])
gen lxch=ln(x[_n-2]-xch[_n-2])
gen doiluk=ln(oiluk/oiluk[_n-1])+ln(sdr/sdr[_n-1])
gen loilr=ln(oilpreal[_n-1])

gen rarch=(ra-rach)/(m)
replace rarch =(raa-rach)/m if dates>2005
gen rar=(ra)/(m)
gen rargdpxf=-(ra-raxf)/(gdp-gdpxf)




gen sdbtrch=ln(stdbta/(sra-srach))



gen tdbtr=ln(tda/gdpa) 
/*
replace sdbtrch=ln(stdbta/(sra-srach)) if dates==2006
replace sdbtrch=ln(stdbta/(sra-srach))+0.17 if dates==2007
replace sdbtrch=ln(stdbta/(sra-srach))+0.31 if dates==2008
replace sdbtrch=ln(stdbta/(sra-srach))+0.31 if dates==2009
replace sdbtrch=ln(stdbta/(sra-srach))+0.31 if dates==2010

replace tdbtr=ln(tda/gdpa) if dates==2006
replace tdbtr=ln(tda/gdpa+0.05) if dates==2007
replace tdbtr=ln(tda/gdpa+0.10) if dates==2008
replace tdbtr=ln(tda/gdpa+0.1) if dates==2009
replace tdbtr=ln(tda/gdpa+0.1) if dates==2010

replace tdbtr=ln(tda/gdpa) if dates==2006
replace tdbtr=ln(tda/gdpa+0.01) if dates==2007
replace tdbtr=ln(tda/gdpa+0.03) if dates==2008
replace tdbtr=ln(tda/gdpa+0.05) if dates==2009
replace tdbtr=ln(tda/gdpa+0.05) if dates==2010
*/
gen sdbtr=stdbta/(sra)
gen ltdbtr=tdbtr[_n-1]

gen drar=(-ra+ra[_n-1])/x[_n-1]
gen drargdp=(-ra+ra[_n-1])/gdp[_n-1]
gen demgdpl=ln(gdp[_n-1]/gdp[_n-2])

gen cabr=cab/gdp
gen cabra=caa/gdpa

gen cabrxf=(cab-cabxf)/(gdp-gdpxf)
gen dca=(cab-cab[_n-1])/gdp[_n-1]

gen gexr=gexb/gdp
gen nexcbr=nexbcb/gdp

gen npcfr=(npcf)/gdp

replace npcfr=npcfa/gdpa if dates>2005
gen npcfra=npcfa/gdpa
gen npcfchr=-(npcf-ncfch)/gdp[_n-1]
gen npcfxch=(npcf-ncfch)

gen ncfrxf=(ncf-ncfxf)/(gdp-gdpxf)
gen dxxf=ln((x-xxf)/(x[_n-1]-xxf[_n-1]))
gen trade=ln((x-xxf-m)/gdp)
replace trade=ln((x-xxf-m)/gdpa) if dates>2005
gen ltrade=trade[_n-1]
gen plir=pli/gdp


gen cabrch=(cab-cabch)/(gdp)
replace cabrch=cabrch[_n-1] if dates>2005
gen cabxch=cab-cabch
gen dcach=(cab-cabch-(cab[_n-1]-cabch[_n-1]))/gdp[_n-1]
replace dcach=(caa-cabch-(caa[_n-1]-cabch[_n-1]))/gdpa[_n-1] if dates>2005
gen ldcach=dcach[_n-1]
gen ncfrch=(ncf-ncfch)/(x-xch)
gen dxch=ln((x-xch)/(x[_n-1]-xch[_n-1]))

gen rargdpch=-(ra-rach)/(gdp-gdpch)

gen loil=ln(oiluk[_n-1])+ln(sdr[_n-1])
gen ldca=dca[_n-1]
gen lnpcfchr=npcfchr[_n-1]
gen lnpcfr=npcfr[_n-1]
gen lnpcfca=npcfca[_n-1]

gen lsdbtr=sdbtr[_n-1]

gen lsdbtrch=sdbtrch[_n-1]


gen lstdbtr=sdbtr[_n-1]/tdbtr[_n-1]

gen lwtr=wtr[_n-1]/100

***INTEREST RATES***

gen gbondr=gbond10-ln(uscpi/uscpi[_n+1])
gen dgbond10=gbond10-gbond10[_n-1]
gen cbonds=cbond10
gen lcbonds=cbond10[_n-1]-weightedfc[_n-1]
gen lgbonds=gbond10[_n-1]-weightedfc[_n-1]
gen fcdiff=cbond10-weightedfc
gen fscdiff=gbond10-fscharge
gen fchdiff=cbond10-fcharge

gen dcbonds=cbonds-lcbonds

gen lgbond10=gbond10[_n-1]
gen lgbond3=gbond3[_n-1]
gen tstruc=gbond3-gbond10
gen ltstruc=(lgbond3-lgbond10)
gen lwgdp=ln(wgdp[_n-1])
gen lemgdp=ln(gdp[_n-1])
replace lemgdp=ln(gdpa[_n-1]) if dates>2005
gen lemgdpxf=ln((gdp[_n-1]-gdpxf[_n-1])*sdr[_n-1])
gen lemrgdp=ln(ppprex[_n-1])

gen lwgdpp90=lwgdp*dp90


***DEPENDENT VARIABLE***

gen lfco=ln(fcon[_n-1]/(sdr[_n-1]*gdp[_n-1]))
gen llfco=ln(fcon/(sdr*gdp))
gen lfcoa=ln(fconxea[_n-1]/(gdp[_n-1]*sdr[_n-1]))
gen lfcoxo=ln(fcoxo[_n-1])
gen lfcoo=ln(1+fcormv[_n-1])
gen lsdr=ln(sdr[_n-1])
gen lcabr=cabr[_n-1]
gen lcabrxf=cabrxf[_n-1]
gen lgexr=gexr[_n-1]
gen ltot=ln(tot[_n-1])
gen lnexcbr=nexcbr[_n-1]

gen lncfrxf=ncfrxf[_n-1]
gen lplir=plir[_n-1]
gen lrar=rar[_n-1]
gen lrarch=rarch[_n-1]
gen lsrach=ln((sra[_n-1]-srach[_n-1]-fco[_n-1]*sdr[_n-1]/100)/gdp[_n-1])


gen lrargdpxf=rargdpxf[_n-1]

gen lcabrch=cabrch[_n-1]
gen lncfrch=ncfrch[_n-1]
gen lrargdpch=rargdpch[_n-1]
gen ldxch=dxch[_n-1]
gen lemgdpch=ln(gdp[_n-1]-gdpch[_n-1])
gen dlemgdp=lemgdp-lemgdp[_n-1]
gen dlemrgdp=lemrgdp-lemrgdp[_n-1]


***SMOOTHED SERIES***
tssmooth ma sm1=dlemrgdp, window(5 0)
tssmooth ma sm2=cabxch, window(3 0)
tssmooth ma sm3=npcfxch, window(3 0)

gen dcab=(cabxch-sm2)/gdp
gen ldcab=dcab[_n-1]

gen dnpcf=(npcfxch-sm3)/gdp
gen ldnpcf=dnpcf[_n-1]

gen dsdbtr=lsdbtr-lsdbtr[_n-1]
gen dgbondr=gbondr-gbondr[_n-1]
gen ldx=dx[_n-1]
gen ldxxf=dxxf[_n-1]
gen ldwtr=dwtr[_n-1]
gen dsterm=gbond3-gbond3[_n-1]
gen dtot=ln(tot/tot[_n-1])
gen dlsrach=lsrach-lsrach[_n-1]

****EXCHANGE RATES****

gen dpppex=ln(pppex/pppex[_n-1])
gen dppprex=ln(gdp/ppprex)-ln(gdp[_n-1]/ppprex[_n-1])
gen ldpppex=dpppex[_n-1]
gen ldppprex=dppprex[_n-1]
gen lpppex=ln(pppex[_n-1])
gen lppprex=ln(gdp[_n-1]/ppprex[_n-1])
gen ltdbt=ln(tdbta[_n-1]*sdr[_n-1])
gen ldrar=drar[_n-1]
gen ldrargdp=drargdp[_n-1]


dfgls dca 
dfgls dcach
dfgls lcbonds
dfgls ltotsd
dfgls ltstruc
dfgls ltdbt
dfgls dfco 
dfgls dwgdp 
dfgls dx 
dfgls dpppex 
dfgls dtot 
dfgls doil 
dfgls lrar
dfgls lrarch 
dfgls cabr 
dfgls ncfr 
dfgls lemgdp 
dfgls lfco 
dfgls lpppex 
dfgls lwtr



pperron cbonds 
pperron fcdiff
pperron ltotsd
pperron ltstruc
pperron tdbtr 
pperron dca
pperron dcach
pperron dfco
pperron dfcoa 
pperron dtot 
pperron doil 
pperron lrar 
pperron cabr 
pperron cabrch 
pperron npcfr
pperron npcfchr 
pperron lemgdp 
pperron lfco 
pperron lfcoa 
pperron sdbtrch
pperron qrat


reg lfco ltdbtr l.trade lcbonds lsdbtrch if dates<2011
predict lfcop


var dfco, lags(1) exog(dtot l.dcach lrarch lfco ltdbtr lsdbtrch ltrade lcbonds), if dates<2004

predict lfcop0 
gen fcofcst=fcon if dates==1983
replace fcofcst=fcofcst[_n-1]*(1+lfcop0) if dates>1983
fcast compute m1_, step(2) bsp
list dates dfco m1_dfco m1_dfco_LB m1_dfco_UB 
gen dLB=m1_dfco-m1_dfco_LB
gen dUB=m1_dfco-m1_dfco_UB

capture
end