Page 1 of 1

Loop to run equation

Posted: Mon Jun 25, 2018 7:30 pm
by fmantessof
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)))

Re: Loop to run equation

Posted: Tue Jun 26, 2018 9:51 am
by EViews Matt
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)

Re: Loop to run equation

Posted: Tue Jun 26, 2018 3:59 pm
by fmantessof
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

Re: Loop to run equation

Posted: Tue Jun 26, 2018 4:58 pm
by EViews Matt
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)

Re: Loop to run equation

Posted: Tue Jun 26, 2018 6:03 pm
by fmantessof
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)))

Re: Loop to run equation

Posted: Wed Jun 27, 2018 9:03 am
by EViews Matt
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

Re: Loop to run equation

Posted: Wed Jun 27, 2018 11:21 am
by fmantessof
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

Re: Loop to run equation

Posted: Wed Jun 27, 2018 1:36 pm
by EViews Matt
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.

Re: Loop to run equation

Posted: Thu Jun 28, 2018 5:29 pm
by fmantessof
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))

Re: Loop to run equation

Posted: Fri Jun 29, 2018 10:18 am
by EViews Matt
The formula in your Excel file is missing the parentheses around the denominator.

Re: Loop to run equation

Posted: Fri Jun 29, 2018 3:16 pm
by fmantessof
Issue solved !..Thanks Matt, sorry for the parenthesis error