Skip to content

Instantly share code, notes, and snippets.

@trialsolution
Last active December 14, 2015 16:29
Show Gist options
  • Save trialsolution/5115711 to your computer and use it in GitHub Desktop.
Save trialsolution/5115711 to your computer and use it in GitHub Desktop.
$title A Macro to Compute Exponential Growth Rates
$ontext
originally posted by T. Rutherford on the gams mailing list
http://www.mpsge.org/exprate.html
extended by me with a nested macro solution (probably easier to follow)
$offtext
$ontext
Anybody knows a command to compute the exponencial annual growth rate
or a time series?
This formula should follow the equation
ln(y)=a+mx
y=exp(a+mx)
y=exp(a)*exp(mx)
y=aÂ’ * exp(m)^x
Therefore, the actual annual growth rate r = (dy/y) dx is exp(m)-1
This is calculated in EXCEL by using the following formula:
EXP(LINEST(LN(b1:b10),a1:a10,TRUE,TRUE))-1
$offtext
set t Observations /a1*a10/;
parameter y1(t) First series (y values) /
a1 19789, a2 18740, a3 19368, a4 18109, a5 25059,
a6 19456, a7 26870, a8 26968, a9 18978, a10 28019/
parameter y2(t) Second series (y values) /
a1 9005, a2 7713, a3 6916, a4 6445, a5 9532,
a6 9707, a7 10876, a8 11739, a9 9366, a10 9213 /;
parameter x(t) Known x values;
x(t) = ord(t);
parameter r1 Exponential growth rate -- series 1,
r2 Exponential growth rate -- series 2;
* The exprate macro takes three arguments:
* - known y
* - known x
* - set corresponding to observations
$macro exprate(y,x,t) \
(exp( sum(t, (x(t)-sum(t.local,x(t))/card(t)) * \
(log(y(t))-sum(t.local,log(y(t)))/card(t))) \
/ sum(t, sqr(x(t)-sum(t.local,x(t))/card(t))) ) - 1);
r1 = exprate(y1,x,t);
r2 = exprate(y2,x,t);
option decimals=8;
display r1,r2;
$ontext
The above formula from Rutherford is very elegant but somewhat hard to follow
The idea is to use a simple regression to derive 'm'; ln(y) = m * x + const.
Then calculate the annual growth rate as r = exp(m) - 1
Let's do it in two steps below (2 nested macros)
Remark: the '.local' attribute is eqivalent with using ALIAS {use it in macros only}
$offtext
* linear regression macro, calculates slope
$macro slope(y,x,t) \
({sum[t, x(t) * log(y(t))] - [sum(t, x(t)) * sum(t, log(y(t)))] / card(t)} \
/ { sum(t, sqr(x(t))) - sqr(sum(t, x(t))) / card(t) } )
* annual growth rate in case the time serie follows exponential growth
$macro erate(y,x,t) \
[exp (slope (y,x,t)) - 1] ;
r1 = erate(y1,x,t);
r2 = erate(y2,x,t);
display r1,r2;
$exit
---- 55 PARAMETER r1 = 0.03580187 Exponential growth ra
te -- series 1
PARAMETER r2 = 0.03576484 Exponential growth ra
te -- series 2
---- 83 PARAMETER r1 = 0.03580187 Exponential growth ra
te -- series 1
PARAMETER r2 = 0.03576484 Exponential growth ra
te -- series 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment