Introductory Financial Econometrics Programs

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

CharlieEVIEWS
Posts: 202
Joined: Tue Jul 17, 2012 9:47 am

Introductory Financial Econometrics Programs

Postby CharlieEVIEWS » Mon Dec 08, 2014 12:48 pm

Dear all,

I'm writing some very basic programs for an introductory financial econometrics class and thought I'd make the programs available in case any one found them useful, interesting, or wanted to give comments. I'll post them all in this thread. No claims are made to the theroetical/empirical validity as to the approaches taken, as the examples are designed to give an exposition of how to do various tasks which may be useful. The first is a simple program to get some monthly stock price data using getstocks and then identify an AR(p) model using an information criteria and then forecast it H=1,...,.12 steps ahead:

Code: Select all

getstocks(a,freq=3,i=2, start=2000/01/01, end=2014/12/01) gs jpm
graph serieslevels.line gs_adjclose jpm_adjclose
graph seriesdlog.line dlog(gs_adjclose) dlog(jpm_adjclose)
!sig=1
!maxp=20
for %series gs_adjclose jpm_adjclose
   smpl @first 2014m12
   freeze(temptb) log({%series}).uroot(kpss)
   !lm = @val(temptb(7,5))
   !crit = @val(temptb(8+!sig,5))
   delete temptb
   if !lm>!crit then
      !diff=1
   else
      !diff=0
   endif
   !aic=999999999999999999999999999999999
   vector(!maxp) {%series}_storeaic
   for !p = 1 to !maxp
      equation eq{!p}.ls dlog({%series},!diff) c ar(1 to !p)
      {%series}_storeaic(!p)=eq{!p}.@aic
      if eq{!p}.@aic<!aic then
         !aic =eq{!p}.@aic
         !bestp=!p
      endif
      delete eq{!p}
   next
   equation besteq_{%series}.ls dlog({%series},!diff) c ar(1 to !bestp)
   pagestruct(freq=m,start=2000m1,end=2015m12)
   smpl 2015m1 2015m12
   besteq_{%series}.forecast(f=na) {%series}_f
next



CharlieEVIEWS
Posts: 202
Joined: Tue Jul 17, 2012 9:47 am

Re: Introductory Financial Econometrics Programs

Postby CharlieEVIEWS » Mon Dec 08, 2014 12:57 pm

The second program (simpler) gets a couple of series from FRED (the US GDP deflater and the number of new car registrations in New Zealand), plots them, and uses nested for loops to freeze a series of different unit root tests and correlograms, and then gives a potential example of a spurious regression (high R^2 with non stationary series, low DW, etc):

Code: Select all

wfcreate m 2000m1 2014m9
copy fred::gdpdef untitled::gdpdef
copy fred::nzlsacrmismei untitled::nzcarreg
graph linegraph1.line log(gdpdef) log(nzcarreg)
linegraph1.setelem(2) axis(right)
graph linegraph2.line dlog(gdpdef) dlog(nzcarreg)
linegraph2.setelem(2) axis(right)
for %series gdpdef nzcarreg
   for %test adf kpss
      for %det const trend
         freeze({%series}_{%det}_{%test}_d0)   {%series}.uroot({%test},{%det},dif=0)
         freeze({%series}_{%det}_{%test}_d1)   {%series}.uroot({%test},{%det},dif=1)
         freeze({%series}_{%det}_{%test}_d2)   {%series}.uroot({%test},{%det},dif=2)
      next
   next
   freeze({%series}_d0) {%series}.correl
   freeze({%series}_d1) d({%series},1).correl
   freeze({%series}_d2) d({%series},2).correl
next
equation eq1.ls log(gdpdef) c @trend log(nzcarreg)
equation eq2.ls dlog(gdpdef) c @trend dlog(nzcarreg)

CharlieEVIEWS
Posts: 202
Joined: Tue Jul 17, 2012 9:47 am

Re: Introductory Financial Econometrics Programs

Postby CharlieEVIEWS » Wed Dec 10, 2014 9:13 am

A third program to solidify what we did in the previous two programs (determine stationarity and identify a suitable VAR(p) model), using Lutkepols wgmacro file http://www.eviews.com/EViews8/data/wgmacro.wf1, then do a couple of basic commands on the var object before turning it into a system to show the equivalence of wald tests against the .testexog command:

Code: Select all


import c:\wgmacro
!maxvarlags=4
matrix(3,!maxvarlags) ic_mat
graph tsplot1.line consumption income investment
graph tsplot2.line lconsumption lincome linvestment
graph tsplot3.line dlconsumption dlincome dlinvestment
rename dlconsumption dlcons
rename dlincome dlinc
rename dlinvestment dlinv 'rename due to EViews 24 character limit
for %series lcons linc linv   
   for %test adf kpss
      for %det const trend
         freeze({%series}_{%det}_{%test}_d0)   {%series}.uroot({%test},{%det},dif=0)
         freeze({%series}_{%det}_{%test}_d1)   {%series}.uroot({%test},{%det},dif=1)
         freeze({%series}_{%det}_{%test}_d2)   {%series}.uroot({%test},{%det},dif=2)
      next
   next
   freeze({%series}_d0) {%series}.correl
   freeze({%series}_d1) d({%series},1).correl
   freeze({%series}_d2) d({%series},2).correl
next
!aic = 999999999999999999999
!schwarz=99999999999999999
!hq=9999999999999999999999
for !p = 1 to !maxvarlags   
   var var{!p}.ls 1 !p dlcons dlinc dlinv
   !iccounter=1
   for %IC aic schwarz hq
      ic_mat(!iccounter,!p)=var1.@{%IC}
      if var{!p}.@{%IC}<!{%IC} then
         !bestlag{%IC}=!p
         !{%IC}=var{!p}.@{%IC}
      endif
   !iccounter=+!iccounter+1
   next   
next
var varaic.ls 1 !bestlagaic dlcons dlinc dlinv
var varschwarz.ls 1 !bestlagschwarz dlcons dlinc dlinv
var varhq.ls 1 !bestlaghq dlcons dlinc dlinv
varaic.laglen(8)
varaic.testexog
varaic.makesystem(n=varsystem)
varsystem.ls
varsystem.wald c(3)=c(4)=0   'granger causality test
varsystem.wald c(3)=c(4)=c(5)=c(6)=0 'block exogeneity test
varaic.impulse(10,m,a,se=a) dlcons dlinc dlinv @ dlcons dlinc dlinv



Last edited by CharlieEVIEWS on Sun Dec 14, 2014 1:40 pm, edited 1 time in total.

CharlieEVIEWS
Posts: 202
Joined: Tue Jul 17, 2012 9:47 am

Re: Introductory Financial Econometrics Programs

Postby CharlieEVIEWS » Sun Dec 14, 2014 1:20 pm

And a program to again consolidate earlier programs and then use rolling windows to estimate the cointegrating rank (and plot the rank across time) then impose a restriction on the VECM:

Code: Select all


wfcreate m 1959m1 2014m09
fetch fred::indpro
fetch fred::m2real
fetch fred::pce
genr lindpro=log(indpro)
genr lpce=log(pce)
genr lm2real=log(m2real)
graph loggedseries.line lindpro lpce lm2real
loggedseries.setelem(1) axis(right)
for %series lindpro lpce lm2real
   for %det const trend
      freeze({%series}_{%det}_d0) {%series}.uroot(adf,{%det},dif=0)
      freeze({%series}_{%det}_d1) {%series}.uroot(adf,{%det},dif=1)
      freeze({%series}_{%det}_d2) {%series}.uroot(adf,{%det},dif=2)
   next
   freeze({%series}_d0) {%series}.correl
   freeze({%series}_d1) d({%series},1).correl
   freeze({%series}_d2) d({%series},2).correl
next
group threeserieslogged lindpro lpce lm2real
vector(240) ranks
for !j = 1 to 240
   smpl @first+!j @last-240+!j
   var varfirstdifferences.ls 1 4 d(lpce) d(lm2real) d(lindpro)
   varfirstdifferences.laglen(12,mname=matrixlags{!j})
   !lags=matrixlags{!j}(14,5)
   freeze(vecmtable!j) threeserieslogged.coint(s,!lags)
   ranks(!j)=vecmtable!j(13,4)
next
freeze(cointegratingranks) ranks.line
smpl @all
var firstvecm
firstvecm.append(coint) b(1,3)=0
firstvecm.ec(c,1,restrict) 1 1 lpce lindpro lm2real



CharlieEVIEWS
Posts: 202
Joined: Tue Jul 17, 2012 9:47 am

Re: Introductory Financial Econometrics Programs

Postby CharlieEVIEWS » Mon Dec 15, 2014 9:38 am

Finally, a short program to do some GARCH(1,1) stuff, such as testing for ARCH effects, making and plotting conditional variances and then forecasting etc on five GBP/FX rates (from http://www.oanda.com/currency/historical-rates/):

Code: Select all

wfopen C:\<insertfilepath>\fxdata.csv
for %series belize kazahk kenya mexico usd
   genr pc_{%series}=@pc({%series})
   graph {%series}_g.line pc_{%series}
next
graph all_g.merge belize_g kazahk_g kenya_g mexico_g usd_g
for %series belize kazahk kenya mexico usd
   !schwarz=999999999999999999999999
   for !p = 1 to 10
      equation {%series}_eq{!p}.ls pc_{%series} c ar(1 to !p)
      if {%series}_eq{!p}.@schwarz<!schwarz then
         !schwarz={%series}_eq{!p}.@schwarz
         !bestlag=!p
      endif
   next
   equation {%series}_eq_bestlag.ls pc_{%series} c ar(1 to !bestlag)
   freeze(archtest_{%series}) {%series}_eq_bestlag.archtest(4)
   equation garch_{%series}.arch pc_{%series} c ar(1 to !bestlag)
   freeze(garch_{%series}_cond_std_g) garch_{%series}.garch
   freeze(garch_{%series}_cond_var_g) garch_{%series}.garch(v)
   garch_{%series}.makegarch garch_{%series}_cond_v
   graph garch_{%series}_cond_var_g1.line garch_{%series}_cond_v
   freeze(garch_{%series}_hist) garch_{%series}.hist
   freeze(garch_{%series}_archtest) garch_{%series}.archtest(4)
   garch_{%series}.forecast garch_{%series}_fm garch_{%series}_fse garch_{%series}_fgarch
   equation   {%series}_eq_egarch.arch(1,1,egarch) pc_{%series} c ar(1 to !bestlag)
next


Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 5 guests