Automatic ARMA selection

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11378
Joined: Tue Sep 16, 2008 5:38 pm

Automatic ARMA selection

Postby EViews Gareth » Wed Jul 29, 2009 12:20 pm

Code: Select all

'Create some data.  Delete these first few lines if you want to run on your existing workfile.

create u 1000
series y=nrnd






'Program that calculates the "best" ARMA specification for an equation.  Edit the variables below to match your particular data/setup.

%eqname = "EQ01"   'name of equation object that will be used.
%maxAR = "2"            'maximum number of AR terms
%maxMA = "3"   'maximum number of MA terms
%dep = "Y"   'dependent variable
%regs = "C "   'independent variables
%criterion = "@AIC"    'which criterion to use  enter "@AIC" for Akaike, "@schwarz" for Schwarz, and @HQ for Hannan-Quinn

!maxAR = @val(%maxAR)
!maxMA = @val(%maxMA)


close {%eqname}


'create table for storing critical values.
%matname = "crits"
if @isobject(%matname) then
   %matname = "__crits"
   if @isobject(%matname) then
      delete {%matname}
   endif
endif
table(!maxar+2,!maxma+2) {%matname}
{%matname}(1,1) = "AR /  MA"
{%matname}.setlines(1) +b
{%matname}.setlines(a) +r

'set sample
smpl @first+!maxAR @last

!mincrit = 1e12  'set the minimum to an artificially large value to begin

'estimate the models
%arstring = ""
for !i=0 to !maxar
   'build up string for AR terms.
   if !i>0 then
      %arstring = %arstring + " ar(" + @str(!i) + ")"
   endif
   %mastring = ""
   for !j=0 to !maxma
      'build up string for MA terms
      if !j>0 then
         %mastring = %mastring + " ma(" + @str(!j) + ")"
      endif
      'estimate equation
      equation {%eqname}.ls {%dep} {%regs} {%arstring} {%mastring}
      'capture criterion
      if @upper(%criterion) = "@AIC" then
         !crit = {%eqname}.@aic
      endif
      if @upper(%criterion) = "@SCHWARZ" then
         !crit = {%eqname}.@schwarz
      endif
      if @upper(%criterion) = "@HQ" then
         !crit = {%eqname}.@hq
      endif
      'compare criterion
      if !crit < !mincrit then
         !mincrit = !crit
         !bestAR = !i
         !bestMA = !j
         %bestARstr = %arstring   'store the best ar string
         %bestMAstr = %mastring   'store the best ma string
         {%matname}.settextcolor(@all) black   'table formatting.
         !ii=!i+2
         !jj=!j+2
         {%matname}.settextcolor(!ii,!jj) red
      endif
      {%matname}(!i+2,!j+2) = !crit
      {%matname}(!i+2,1) = !i
      {%matname}(1,!j+2) = !j
   next
next

equation {%eqname}.ls {%dep} {%regs} {%bestARstr} {%bestMAstr}

show {%eqname}
show {%matname}
Follow us on Twitter @IHSEViews

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11378
Joined: Tue Sep 16, 2008 5:38 pm

Re: Automatic ARMA selection

Postby EViews Gareth » Wed Jul 29, 2009 2:04 pm

Note that this program was posted as a guide as to how such a program could be written, and is in no way a recommendation of the use of such a program as a method of calculating the best ARMA structure for your data :D

In practice it seems that such AIC/BIC/HQ based selection routines tend to over-specify the ARMA terms by a fair bit.
Follow us on Twitter @IHSEViews

micmic
Posts: 5
Joined: Mon Jan 25, 2010 9:59 pm

Re: Automatic ARMA selection

Postby micmic » Thu Feb 18, 2010 8:59 pm

I tried it but it said

@UPPER is an illegal or reserved name in "IF @UPPER("@AIC")="@AIC" THEN

Is there an alternative word for " UPPER " I can use? Does this code work in EVIEWS 5?

micmic
Posts: 5
Joined: Mon Jan 25, 2010 9:59 pm

Re: Automatic ARMA selection

Postby micmic » Mon Feb 22, 2010 12:06 pm

This code only works in EVIEWS 6.0

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11378
Joined: Tue Sep 16, 2008 5:38 pm

Re: Automatic ARMA selection

Postby EViews Gareth » Mon Feb 22, 2010 12:17 pm

True, although you could always change the code to remove the @upper parts. Change the line:

Code: Select all

if @upper(%criterion) = "@AIC" then

to be:

Code: Select all

if %criterion = "@AIC" then



Of course you then lose the ability to specify the IC you want in lower case letters.
Follow us on Twitter @IHSEViews

micmic
Posts: 5
Joined: Mon Jan 25, 2010 9:59 pm

Re: Automatic ARMA selection

Postby micmic » Mon Feb 22, 2010 2:17 pm

Thanks Gareth. This is very helpful.

micmic
Posts: 5
Joined: Mon Jan 25, 2010 9:59 pm

Re: Automatic ARMA selection

Postby micmic » Mon Feb 22, 2010 3:36 pm

I noticed that using this code EVIEWS 6.0 cannot handle ARMA terms greater than 11 (i.e. ARMA(11,11)). I have no trouble running ARMA(1,11) but when I increase it I get warning "Too Many ARMA Terms".

Is there a way to increase EVIEWS memory size to accomodate higher order ARMA terms?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11378
Joined: Tue Sep 16, 2008 5:38 pm

Re: Automatic ARMA selection

Postby EViews Gareth » Mon Feb 22, 2010 4:27 pm

It isn't really a case of memory size. There's just a limit on how many ARMA terms we let you estimate.
Follow us on Twitter @IHSEViews

kanonathena
Posts: 14
Joined: Thu Jul 15, 2010 2:24 am

Re: Automatic ARMA selection

Postby kanonathena » Wed Sep 01, 2010 11:25 am

Hi,

I modified this code to regress inflation on lags of inflation (0-4 lags) and lags of indicator x (inl: long term interest rate) (1-4 lags) . Using AIC to automatically select lags, I got two tables, one is

_Crits
lags of inflation / lags of x 1.000000 2.000000 3.000000 4.000000
0.000000 3.162417 3.042816 2.999997 2.976678
1.000000 3.162417 3.042816 2.999997 2.976678
2.000000 3.162417 3.042816 2.999997 2.976678
3.000000 3.162417 3.042816 2.999997 2.976678
4.000000 3.162417 3.042816 2.999997 2.976678

Why is AIC is the same for different lags of inflation?

The other table
Crits
AR / MA 1.000000 2.000000 3.000000 4.000000
0.000000 2.995104 2.916265 2.904264 2.860316


I don't understand the reason for two tables. Can someone explain to me this part of the original code:

'create table for storing critical values.
%matname = "crits"
if @isobject(%matname) then
%matname = "__crits"
if @isobject(%matname) then
delete {%matname}
endif
endif

What is @isobject? What does mat from %matname refer to?

and also this part:

'estimate the models
%arstring = ""
for !i=0 to !maxar
'build up string for AR terms.
if !i>0 then
%arstring = %arstring + " ar(" + @str(!i) + ")"
endif
%mastring = ""
for !j=0 to !maxma
'build up string for MA terms
if !j>0 then
%mastring = %mastring + " ma(" + @str(!j) + ")"
endif

What does %arstring and %mastring equal to when !i and !j are 0? It seems undefined from the code?


Lastly, this is modified code I used:


'Program that calculates the "best" ARMA specification for an equation. Edit the variables below to match your particular data/setup.

%eqname = "EQ01" 'name of equation object that will be used.
%maxINF= "4" 'maximum number of AR terms
%maxX = "4" 'maximum number of MA terms
%dep = "INF" 'dependent variable
%regs = "INL" 'independent variables
%criterion = "@AIC" 'which criterion to use enter "@AIC" for Akaike, "@schwarz" for Schwarz, and @HQ for Hannan-Quinn

!maxINF = @val(%maxINF)
!maxX = @val(%maxX)


close {%eqname}

'create table for storing critical values.
%matname = "crits"
if @isobject(%matname) then
%matname = "__crits"
if @isobject(%matname) then
delete {%matname}
endif
endif
table(!maxINF+2,!maxX+2) {%matname}
{%matname}(1,1) = "lags of inflation / lags of x"
{%matname}.setlines(1) +b
{%matname}.setlines(a) +r

!mincrit = 10000 'set the minimum to an artificially large value to begin

'estimate the models
%infstring = ""
for !i=0 to !maxINF
'build up string for AR terms.
if !i>0 then
%infstring = %infstring + " inf(-" + @str(!i) + ")"
endif
%xstring = ""
for !j=1 to !maxX
'build up string for indicator
if !j>1 then
%xstring = %xstring + " inl(-" + @str(!j) + ")"
endif
'estimate equation
equation {%eqname}.ls {%dep} c {%arstring} {%xstring}
'capture criterion
if @upper(%criterion) = "@AIC" then
!crit = {%eqname}.@aic
endif
if @upper(%criterion) = "@SCHWARZ" then
!crit = {%eqname}.@schwarz
endif
if @upper(%criterion) = "@HQ" then
!crit = {%eqname}.@hq
endif
'compare criterion
if !crit < !mincrit then
!mincrit = !crit
!bestINF = !i
!bestX = !j
%bestINFstr = %arstring 'store the best ar string
%bestXstr = %mastring 'store the best ma string
{%matname}.settextcolor(@all) black 'table formatting.
!ii=!i+2
!jj=!j+2
{%matname}.settextcolor(!ii,!jj) red
endif
{%matname}(!i+2,!j+2) = !crit
{%matname}(!i+2,1) = !i
{%matname}(1,!j+2) = !j
next
next

equation {%eqname}.ls {%dep} c {%bestINFstr} {%bestXstr}


The best model is:

Variable Coefficient Std. Error t-Statistic Prob.

C 1.454179 0.093277 15.58990 0.0000

Only a constant term, can anyone tell me what is wrong with my code?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11378
Joined: Tue Sep 16, 2008 5:38 pm

Re: Automatic ARMA selection

Postby EViews Gareth » Wed Sep 01, 2010 12:06 pm

Seems a lot of your questions could be answered by reading through the manual. I'll have a go at some of them though.


kanonathena wrote:

I don't understand the reason for two tables. Can someone explain to me this part of the original code:

'create table for storing critical values.
%matname = "crits"
if @isobject(%matname) then
%matname = "__crits"
if @isobject(%matname) then
delete {%matname}
endif
endif

What is @isobject? What does mat from %matname refer to?


The individual questions I'll leave up to the manual to describe. However all that code is doing is making sure that if there is already an item called CRITS, then we don't use that name, rather we use the name __CRITS.


'estimate the models
%arstring = ""
for !i=0 to !maxar
'build up string for AR terms.
if !i>0 then
%arstring = %arstring + " ar(" + @str(!i) + ")"
endif
%mastring = ""
for !j=0 to !maxma
'build up string for MA terms
if !j>0 then
%mastring = %mastring + " ma(" + @str(!j) + ")"
endif

What does %arstring and %mastring equal to when !i and !j are 0? It seems undefined from the code?

Both strings are initialised as "" (i.e. empty strings).

Lastly, this is modified code I used:


'Program that calculates the "best" ARMA specification for an equation. Edit the variables below to match your particular data/setup.

%eqname = "EQ01" 'name of equation object that will be used.
%maxINF= "4" 'maximum number of AR terms
%maxX = "4" 'maximum number of MA terms
%dep = "INF" 'dependent variable
%regs = "INL" 'independent variables
%criterion = "@AIC" 'which criterion to use enter "@AIC" for Akaike, "@schwarz" for Schwarz, and @HQ for Hannan-Quinn

!maxINF = @val(%maxINF)
!maxX = @val(%maxX)


close {%eqname}

'create table for storing critical values.
%matname = "crits"
if @isobject(%matname) then
%matname = "__crits"
if @isobject(%matname) then
delete {%matname}
endif
endif
table(!maxINF+2,!maxX+2) {%matname}
{%matname}(1,1) = "lags of inflation / lags of x"
{%matname}.setlines(1) +b
{%matname}.setlines(a) +r

!mincrit = 10000 'set the minimum to an artificially large value to begin

'estimate the models
%infstring = ""
for !i=0 to !maxINF
'build up string for AR terms.
if !i>0 then
%infstring = %infstring + " inf(-" + @str(!i) + ")"
endif
%xstring = ""
for !j=1 to !maxX
'build up string for indicator
if !j>1 then
%xstring = %xstring + " inl(-" + @str(!j) + ")"
endif
'estimate equation
equation {%eqname}.ls {%dep} c {%arstring} {%xstring}
'capture criterion
if @upper(%criterion) = "@AIC" then
!crit = {%eqname}.@aic
endif
if @upper(%criterion) = "@SCHWARZ" then
!crit = {%eqname}.@schwarz
endif
if @upper(%criterion) = "@HQ" then
!crit = {%eqname}.@hq
endif
'compare criterion
if !crit < !mincrit then
!mincrit = !crit
!bestINF = !i
!bestX = !j
%bestINFstr = %arstring 'store the best ar string
%bestXstr = %mastring 'store the best ma string
{%matname}.settextcolor(@all) black 'table formatting.
!ii=!i+2
!jj=!j+2
{%matname}.settextcolor(!ii,!jj) red
endif
{%matname}(!i+2,!j+2) = !crit
{%matname}(!i+2,1) = !i
{%matname}(1,!j+2) = !j
next
next

equation {%eqname}.ls {%dep} c {%bestINFstr} {%bestXstr}


You're estimating the equation:

Code: Select all

equation {%eqname}.ls {%dep} c {%arstring} {%xstring}

Yet nowhere in your program have you defined what %arstring is, so that bit is meaningless.
Follow us on Twitter @IHSEViews

kanonathena
Posts: 14
Joined: Thu Jul 15, 2010 2:24 am

Re: Automatic ARMA selection

Postby kanonathena » Thu Sep 02, 2010 5:54 am

After finding the model with the right lags for inflation and indicator x, I want to use recursive estimation to find all the forecast errors from one-step ahead static regression. Here is the code I modified from the rolling window thread based on a test model(.ls inf c inl(-1) ):



' get size of workfile
!length = @obsrange

' declare equation for estimation
equation eq1

'create table for storing critical values.
%matname = "forecast errors"
table(!length-40,1) {%matname}

' move sample !step obs at a time
for !i = 0 to !length-40-1

' set sample to estimation period
smpl @first @first+40+!i

' estimate equation
equation eqtest.ls inf c inl(-1)

'calculate forecast error
smpl @first+40+!i+1 @first+40+!i+1
eqtest.forecast inffit
!error=@mean(inffit)- @first+40+!i+1 'calculate forecast error

'store forecast errors
{%matname}(!i,1)=!error
next

show {%matname}

Questions:

1. Regarding this part:

'calculate forecast error
smpl @first+40+!i+1 @first+40+!i+1
eqtest.forecast inffit
!error=@mean(inffit)- @first+40+!i+1 'calculate forecast error

What is the function to extract the fitted value of inflation(I used @mean here since there is only one fitted value) and the corresponding actual value(@first+40+!i+1)?
Is there any other ways to calculate forecast error? I know these must be somewhere in the manual I just can't find it.


2. Regarding this part:
' estimate equation
equation eqtest.ls inf c inl(-1)

How do I replace this with the model selected by AIC before:

equation {%eqname}.ls inf c {%bestINFstr} {%bestXstr}

How do I quote the result from a program before or must I nest it?

If there is any other problem you see please tell me.

tony
Posts: 50
Joined: Wed Jan 07, 2009 7:59 am

Re: Automatic ARMA selection

Postby tony » Mon Nov 01, 2010 7:29 am

if we make a MLE method to estimate the coefficients, rather than OLS, how to do the automatic arma selection?

pablete
Posts: 7
Joined: Wed Mar 02, 2011 5:10 am

Re: Automatic ARMA selection

Postby pablete » Tue Mar 08, 2011 4:35 am

Hey Gareth, I saw you mention that there is a limit on the ARMA terms, I assume there is a reason for that. I'm asking because I'm working with hourly data that are clearly autocorrelated up to 24 hours, I'm trying to stimate a model with AR terms AR(1) ... AR(24), Am I doing something essentially wrong? Or just impossible to do in EVIEWS?

EViews Gareth wrote:It isn't really a case of memory size. There's just a limit on how many ARMA terms we let you estimate.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11378
Joined: Tue Sep 16, 2008 5:38 pm

Re: Automatic ARMA selection

Postby EViews Gareth » Tue Mar 08, 2011 9:00 am

Currently impossible to have all 24 AR terms.

I will point out though that your model would probably be quite unreasonable with all 24 AR terms.
Follow us on Twitter @IHSEViews

peter.t
Posts: 1
Joined: Sun Mar 27, 2011 6:50 am

Re: Automatic ARMA selection

Postby peter.t » Sun Mar 27, 2011 7:08 am

Dear EViews Gareth,

I tried your program for an automatic ARMA selection and it worked.
Am I allowed to use your program for writing a paper, in which I have to estimate an ARMA model?
Of course, I would name you in the references section.

Thank you in advance for a reply!

Peter


Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 2 guests