'' --------------------------------------------------------------------------------------------------
'' Loading dataset
'' --------------------------------------------------------------------------------------------------
wfcreate M 2002m01 2012m12
import "X:\Camilogonzalez\Macro View\2012\2012-10-30 (FCI)\fci_chile.xls" range="Chile" colhead="4" namepos="last"

'' --------------------------------------------------------------------------------------------------
'' Variable Transformation and Standarization
'' --------------------------------------------------------------------------------------------------
smpl @all
!crit=1000000000
!r2=0
!ss=1000000000
genr dum1=@recode(@date=@dateval("2010m03"),1,0)
genr dum2=@recode(@date=@dateval("2011m03"),1,0)
%regressors= "@movav(polrate,1) @movav(inf,1) @movav(g,1) dum1 dum2"
series compfci
series g_fcst
matrix(2,2) Lfci
group varfci
group varpurgedfci
equation regfci
equation eqthresh

''Growth variable
genr g=(@pcy(imacec)-@mean(@pcy(imacec)))/@stdevp(@pcy(imacec))

'' Inflation Variable
genr inf=(@pcy(cpi)-@mean(@pcy(cpi)))/@stdevp(@pcy(cpi))

'' Policy Rate Variable
genr polrate=(pr-@mean(pr))/@stdevp(pr)

''IPEC Variables
for !i= 1 to 6
genr ipec{!i}=(ipec{!i}_-@mean(ipec{!i}_))/@stdevp(ipec{!i}_)
delete ipec{!i}_
equation ipecreg
ipecreg.ls ipec{!i} {%regressors}
ipecreg.makeresids uipec{!i}
next

'' Interest Rate Variables
genr i1_=iconsloans-idep
genr i2_=icorploans-idep
genr i3_=iconsloans-@pcy(cpi)
genr i4_=icorploans-@pcy(cpi)
delete iconsloans idep icorploans
for !i= 1 to 4
genr i{!i}=(i{!i}_-@mean(i{!i}_))/@stdevp(i{!i}_)
delete i{!i}_
equation ireg
ireg.ls i{!i} {%regressors}
ireg.makeresids ui{!i}
next

'' Credit Disbursement Variables
for !i=1 to 3
genr col{!i}r=col{!i}_-@pcy(cpi)
genr col{!i}=(col{!i}r-@mean(col{!i}r))/@stdevp(col{!i}r)
delete col{!i}_ col{!i}r
equation colreg
colreg.ls col{!i} {%regressors}
colreg.makeresids ucol{!i}
next

'' Money Variables
for !i=1 to 3
genr m{!i}=(m{!i}_-@mean(m{!i}_))/@stdevp(m{!i}_)
delete m{!i}_
equation mreg
mreg.ls m{!i} {%regressors}
mreg.makeresids um{!i}
next

'' Sovereign Risk Variables
for !i=1 to 2
genr sovrisk{!i}=(sovrisk{!i}_-@mean(sovrisk{!i}_))/@stdevp(sovrisk{!i}_)
delete sovrisk{!i}_
equation sovriskreg
sovriskreg.ls sovrisk{!i} {%regressors}
sovriskreg.makeresids usovrisk{!i}
next

'' External Risk Variables
for !i=1 to 2
genr risk{!i}=(risk{!i}_-@mean(risk{!i}_))/@stdevp(risk{!i}_)
delete risk{!i}_
equation riskreg
riskreg.ls risk{!i} {%regressors}
riskreg.makeresids urisk{!i}
next

'' YC Slope Variables
genr slope2=swap2y-pr
genr slope5=swap5y-pr
genr s1=(slope2-@mean(slope2))/@stdevp(slope2)
genr s2=(slope5-@mean(slope5))/@stdevp(slope5)
delete swap2y swap5y slope2 slope5
for !i=1 to 2 
equation slopereg
slopereg.ls s{!i} {%regressors}
slopereg.makeresids us{!i}
next

'' Inflation Expectations Variable
genr expinf=(expinf_-@mean(expinf_))/@stdevp(expinf_)
delete expinf_
equation expinfreg
expinfreg.ls expinf {%regressors}
expinfreg.makeresids uexpinf

'' Real Exchange Rate Variable
genr tcr=(tcr_-@mean(tcr_))/@stdevp(tcr_)
delete tcr_
equation tcrreg
tcrreg.ls tcr {%regressors}
tcrreg.makeresids utcr
smpl 2003m06 @last

'' --------------------------------------------------------------------------------------------------
'' Estimation
'' --------------------------------------------------------------------------------------------------

for !j=1 to 6
for !k=1 to 4
for !l=1 to 3
for !m=1 to 3
for !n=1 to 2
for !o=1 to 2
for !p=1 to 2

'' Estimation of Principal Components
group v{!j}{!k}{!l}{!m}{!n}{!o}{!p}
v{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add ipec{!j}
v{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add i{!k}
v{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add col{!l}
v{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add m{!m}
v{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add sovrisk{!n}
v{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add risk{!o}
v{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add s{!p}
v{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add expinf tcr

group u{!j}{!k}{!l}{!m}{!n}{!o}{!p}
u{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add uipec{!j}
u{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add ui{!k}
u{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add ucol{!l}
u{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add um{!m}
u{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add usovrisk{!n}
u{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add urisk{!o}
u{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add us{!p}
u{!j}{!k}{!l}{!m}{!n}{!o}{!p}.add uexpinf utcr


v{!j}{!k}{!l}{!m}{!n}{!o}{!p}.makepcomp(cov=corr, eigval=eigv, eigvec=L{!j}{!k}{!l}{!m}{!n}{!o}{!p}) comp{!j}{!k}{!l}{!m}{!n}{!o}{!p}
vector sums
sums=@csum(L{!j}{!k}{!l}{!m}{!n}{!o}{!p})
scalar a1=sums(1,1)
genr fci{!j}{!k}{!l}{!m}{!n}{!o}{!p}=-(comp{!j}{!k}{!l}{!m}{!n}{!o}{!p}/a1-@mean(comp{!j}{!k}{!l}{!m}{!n}{!o}{!p}/a1))/@stdevp(comp{!j}{!k}{!l}{!m}{!n}{!o}{!p}/a1)
scalar b1=L{!j}{!k}{!l}{!m}{!n}{!o}{!p}(1,1)/a1   'Weight Related to IPEC
scalar b2=L{!j}{!k}{!l}{!m}{!n}{!o}{!p}(2,1)/a1   'Weight related to Interest Rates
scalar b3=L{!j}{!k}{!l}{!m}{!n}{!o}{!p}(3,1)/a1   'Weight related to Credit Disbursements
scalar b4=L{!j}{!k}{!l}{!m}{!n}{!o}{!p}(4,1)/a1   'Weight related to Money Growth
scalar b5=L{!j}{!k}{!l}{!m}{!n}{!o}{!p}(5,1)/a1   'Weight related to Sovereign Risk
scalar b6=L{!j}{!k}{!l}{!m}{!n}{!o}{!p}(6,1)/a1   'Weight related to External Risk
scalar b7=L{!j}{!k}{!l}{!m}{!n}{!o}{!p}(7,1)/a1   'Weight related to Slope 
scalar b8=L{!j}{!k}{!l}{!m}{!n}{!o}{!p}(8,1)/a1   'Weight related to Inflation Expectations
scalar b9=L{!j}{!k}{!l}{!m}{!n}{!o}{!p}(9,1)/a1   'Weight related to Real Exchange Rate

if b1>0 and b2<0 and b3>0 and b4>0 and b5<0 and b6<0 and b7<0 or b1<0 and b2>0 and b3<0 and b4<0 and b5>0 and b6>0 and b7>0  then

'' Estimation of GDP Growth Regression
smpl 2003m06 @last-12
!ARlags=0

%arstring = " "
for !i=0 to !ARlags
if !i>0 then
%arstring= %arstring + " ar(" + @str({!i}) + ")"
endif
next

%fcistring= " "
%fcibest=" "
for !i=0 to 12
if !i=0 then %fcistring= "fci"+@str({!j}{!k}{!l}{!m}{!n}{!o}{!p})
else %fcistring= "fci"+@str({!j}{!k}{!l}{!m}{!n}{!o}{!p})+ "("+@str(-{!i})+")"
endif
smpl 2003m06 @last-12
equation r{!j}{!k}{!l}{!m}{!n}{!o}{!p}
r{!j}{!k}{!l}{!m}{!n}{!o}{!p}.ls g c {%arstring} {%fcistring} dum1 dum2
if r{!j}{!k}{!l}{!m}{!n}{!o}{!p}.@rbar2>!r2 then 
!r2=r{!j}{!k}{!l}{!m}{!n}{!o}{!p}.@rbar2
%fcibest=%fcistring
endif
next
smpl 2003m06 @last-12
r{!j}{!k}{!l}{!m}{!n}{!o}{!p}.ls g c {%arstring} {%fcibest} dum1 dum2
smpl @last-11 @last
r{!j}{!k}{!l}{!m}{!n}{!o}{!p}.fit(f=na) g_s{!j}{!k}{!l}{!m}{!n}{!o}{!p}
if @rmse(g_s{!j}{!k}{!l}{!m}{!n}{!o}{!p},g)<=!crit then
!crit=@rmse(g_s{!j}{!k}{!l}{!m}{!n}{!o}{!p},g)
delete regfci Lfci compfci varfci g_fcst varpurgedfci
rename r{!j}{!k}{!l}{!m}{!n}{!o}{!p} regfci
rename L{!j}{!k}{!l}{!m}{!n}{!o}{!p} Lfci
rename comp{!j}{!k}{!l}{!m}{!n}{!o}{!p} compfci
rename v{!j}{!k}{!l}{!m}{!n}{!o}{!p} varfci
rename u{!j}{!k}{!l}{!m}{!n}{!o}{!p} varpurgedfci
rename g_s{!j}{!k}{!l}{!m}{!n}{!o}{!p} g_fcst
smpl 2003m06 @last
genr fcipurged=-(Lfci(1,1)*uipec{!j}+Lfci(2,1)*ui{!k}+Lfci(3,1)*ucol{!l}+Lfci(4,1)*um{!m}+Lfci(5,1)*usovrisk{!n}+Lfci(6,1)*urisk{!o}+Lfci(7,1)*us{!p}+Lfci(8,1)*uexpinf +Lfci(9,1)*utcr)/a1
genr fcipur=(fcipurged-@mean(fcipurged))/@stdevp(fcipurged)
delete fcipurged
else
delete fci{!j}{!k}{!l}{!m}{!n}{!o}{!p} v{!j}{!k}{!l}{!m}{!n}{!o}{!p} L{!j}{!k}{!l}{!m}{!n}{!o}{!p} comp{!j}{!k}{!l}{!m}{!n}{!o}{!p} r{!j}{!k}{!l}{!m}{!n}{!o}{!p} g_s{!j}{!k}{!l}{!m}{!n}{!o}{!p} u{!j}{!k}{!l}{!m}{!n}{!o}{!p}
smpl 2003m06 @last
endif 
else delete fci{!j}{!k}{!l}{!m}{!n}{!o}{!p} v{!j}{!k}{!l}{!m}{!n}{!o}{!p} L{!j}{!k}{!l}{!m}{!n}{!o}{!p} comp{!j}{!k}{!l}{!m}{!n}{!o}{!p} u{!j}{!k}{!l}{!m}{!n}{!o}{!p}
endif 
next
next
next
next
next
next 
next


'' --------------------------------------------------------------------------------------------------
'' Threshhold Regression
'' --------------------------------------------------------------------------------------------------

smpl 2003m06 @last
genr thresh=fci1122222
genr count=@ranks(fci1122222)
vector(count.@obs) ssr
for !i=1 to @floor(count.@obs*0.15)
ssr({!i})=na
next
for !i=count.@obs-@floor(count.@obs*0.15) to count.@obs
ssr({!i})=na
next

for !j=@floor(count.@obs*0.15)+1 to  (count.@obs-@floor(count.@obs*0.15)-1)
genr dum=@recode(count<=!j,1,0)
equation eq{!j}
eq{!j}.ls g c dum1 dum2 fci1122222(-1)*dum fci1122222(-1)*(1-dum) 
ssr({!j})=eq{!j}.@ssr
if eq{!j}.@ssr<!ss then
!ss=eq{!j}.@ssr
delete eqthresh
rename eq{!j} eqthresh
else delete eq{!j}
endif
next


vector v1=@cimin(ssr)
scalar m=v1(1)
show count thresh fci1122222
show eqthresh
show ssr 







'' --------------------------------------------------------------------------------------------------
'' Save work
'' --------------------------------------------------------------------------------------------------
smpl @all
wfsave "X:\Camilogonzalez\Macro View\2012\2012-10-30 (FCI)\fci"
