Loop to run equation

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

fmantessof
Posts: 24
Joined: Fri Aug 14, 2015 7:54 am

Loop to run equation

Postby fmantessof » Mon Jun 25, 2018 7:30 pm

Hi, I am trying to create a same base price index for many series that starts with 100 at the most recent year and goes backwards based on the period price change. However for this to work, as it is calculated backwards I need to execute the equation many times until the first observation for each series. How can I write e loop for it that finishes at the first observation of each series ?

smpl @now-1 @now-1
aapool_reits.genr idxprice?=100
smpl @first @now-2
aapool_reits.genr idxprice?=idxprice?(1)/(1+@pch(price?(1)))

EViews Matt
EViews Developer
Posts: 557
Joined: Thu Apr 25, 2013 7:48 pm

Re: Loop to run equation

Postby EViews Matt » Tue Jun 26, 2018 9:51 am

Hello,

Mathematically, your idxprice is just a scaled version of price. There's no need to explicitly evaluate that equation backwards through your observations, just scale the data so that the target observation has a value of 100. For example,

Code: Select all

%tmp = @datestr(@now)
aapool_reits.genr idxprice? = price? * 100 / @elem(price?, %tmp)

fmantessof
Posts: 24
Joined: Fri Aug 14, 2015 7:54 am

Re: Loop to run equation

Postby fmantessof » Tue Jun 26, 2018 3:59 pm

Great ! It worked, however I got to change the second line to aapool_reits.genr idxprice? = price? * 100 / @elem(price?, @now), as the %tmp version gives me a message "illegal date %tmp". How can I do If I want to use @now-1 or @now-4 as it does not work in this script ?
Thanks

EViews Matt
EViews Developer
Posts: 557
Joined: Thu Apr 25, 2013 7:48 pm

Re: Loop to run equation

Postby EViews Matt » Tue Jun 26, 2018 4:58 pm

Curious. Perhaps the issue is that an expression such as @now-1 or @now-4 behaves differently as part of a smpl command than it does as part of a general expression. In a smpl command, the -1 or -4 are in units of your workfile's frequency, e.g., years. However, in a general expression the units match the date number returned by @now, i.e., days. In other words, in your smpl commands @now-4 means today's date minus four years, but inside @datestr @now-4 means today's data minus four days. Use @dateadd to make sure the date is changing in the units you intend. Try,

Code: Select all

%tmp = @datestr(@dateadd(@now, -1, "YY"))
aapool_reits.genr idxprice? = price? * 100 / @elem(price?, %tmp)

fmantessof
Posts: 24
Joined: Fri Aug 14, 2015 7:54 am

Re: Loop to run equation

Postby fmantessof » Tue Jun 26, 2018 6:03 pm

Illegal date too...But that's fine, I can go with @now only. However the issue in my next steps comes when I try to deflate this series to transform it in real prices (discounted by inflation). To do this you have to go backwards, how can I do it ? The math formula is this, however Eviews can't calculate:
aapool_reits.genr idxpricereal?=idxpricereal?(1)/(1+inflation?(1))*(1+@pch(idxprice?(1)))

EViews Matt
EViews Developer
Posts: 557
Joined: Thu Apr 25, 2013 7:48 pm

Re: Loop to run equation

Postby EViews Matt » Wed Jun 27, 2018 9:03 am

Very odd.

Anyway, the regular (non-pool) genr command can calculate a series backwards, so a combination of your initial code and a loop over the crossids in the pool should do the trick. For example,

Code: Select all

%tmp = aapool_reits.@crossids
for %cid {%tmp}
   smpl @now-1 @now-1
   genr(r) idxpricereal{%cid}=100
   smpl @first @now-2
   genr(r) idxpricereal{%cid}=idxpricereal{%cid}(1)/(1+inflation{%cid}(1))*(1+@pch(idxprice{%cid}(1)))
next

fmantessof
Posts: 24
Joined: Fri Aug 14, 2015 7:54 am

Re: Loop to run equation

Postby fmantessof » Wed Jun 27, 2018 11:21 am

Thanks Matt, however the results are different than the math calc. I made an excel spreadsheet with the result series calculated by Eviews and the correct one calculated by Excel, please see attached. I also adjusted your formula so it matches the previous idxprice script and the inflation (which should be divided by 100).Below the script and attached is the spreadsheet. Thanks for your help.

%tmp = aapool_reits.@crossids
for %cid {%tmp}
smpl @now @now
genr(r) idxpricereal{%cid}=100
smpl @first @now-1
genr(r) idxpricereal{%cid}=(idxpricereal{%cid}(1))/((1+@pch(idxprice{%cid}(1)))*(1+inflation{%cid}(1)/100))
next
Attachments
Eviews.xlsx
(10.36 KiB) Downloaded 255 times

EViews Matt
EViews Developer
Posts: 557
Joined: Thu Apr 25, 2013 7:48 pm

Re: Loop to run equation

Postby EViews Matt » Wed Jun 27, 2018 1:36 pm

I don't know what calculations you performed in Excel, so I can't say why you're getting different results in EViews. Chances are that either the Excel formula or the EViews formula is not calculating what you think it is.

fmantessof
Posts: 24
Joined: Fri Aug 14, 2015 7:54 am

Re: Loop to run equation

Postby fmantessof » Thu Jun 28, 2018 5:29 pm

Matt,
The equation that I want to calculate and which is shown in the Excel spreadsheet I attached in the last post is: idxpricereal = idxpricereal(1)/((1+(idxprice(1)/idxprice-1))*(1+inflation(1)/100)). However the script below does not have show the same results of this former equation.

genr(r) idxpricereal{%cid}=(idxpricereal{%cid}(1))/((1+@pch(idxprice{%cid}(1)))*(1+inflation{%cid}(1)/100))

EViews Matt
EViews Developer
Posts: 557
Joined: Thu Apr 25, 2013 7:48 pm

Re: Loop to run equation

Postby EViews Matt » Fri Jun 29, 2018 10:18 am

The formula in your Excel file is missing the parentheses around the denominator.

fmantessof
Posts: 24
Joined: Fri Aug 14, 2015 7:54 am

Re: Loop to run equation

Postby fmantessof » Fri Jun 29, 2018 3:16 pm

Issue solved !..Thanks Matt, sorry for the parenthesis error


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 10 guests