Basic univariate root solving implementation

For questions regarding programming in the EViews programming language.

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

Fifis
Posts: 23
Joined: Thu Jan 06, 2022 11:48 pm

Basic univariate root solving implementation

Postby Fifis » Fri Nov 18, 2022 12:39 am

Dear all,

I was wondering if EViews has root-finding capabilities for a univariate function (Brent's method, bisecetion, secant, or something similar).

Simple example: there is an indicator (monthly sales). There are forecast monthly logarithmic growth rates (pre-defined). There is a forecast annual figure for the total monthly sales. The goal is to find such a constant that, when added to all monthly growth rates, would produce a series of levels that add up to the annual figure. In other words, we want to make an additive adjustment of a series and find the correct adjustment factor. This is the quick working example in R:

Code: Select all

r <- c(-0.063, 0.018, -0.084, 0.160, 0.033, -0.082, 0.049, 0.074, 0.058, -0.031, 0.151, 0.039) y.ann.sum.desired <- 15 y <- exp(cumsum(r)) # Levels from log-growth rates y.ann.sum <- sum(y) # 13.02 -- lower than the desired 15
Of course, one can manually fiddle with the values and arrive at an approximate answer.

Code: Select all

sumCorr <- function(x = 0) sum(exp(cumsum(r + x))) sumCorr(0.01) # = 13.96, closer to the truth sumCorr(x = 0.0201) # = 15.0007, overshoot sumCorr(x = 0.020093) # = 14.9999, almost correct
However, there is an immensely helpful function in R and other packages -- a univariate root solver to find the root of the equation `f(x) = 0` -- in our case, `sum(exp(cumsum(r + x))) - 15 = 0`:

Code: Select all

uniroot(function(x) sumCorr(x) - y.ann.sum.desired, interval = c(0.01, 0.03), tol = 1e-10) # $root = 0.02009369 sum(exp(cumsum(r + 0.0200936879))) # 15.000000005, great!
I need to implement multiple such corrections in multiple projects in EViews, and am asking what the good way to start would be. (This is a simplified example, which is why analytical solution won't cut it -- the real expressions do not have closed-form solutions.) Any ideas about how to avoid hard-coding the bisection routines and rely on the built-in EViews capabilities?

Many thanks in advance!

Yours sincerly,
Andreï V. Kostyrka

Return to “Programming”

Who is online

Users browsing this forum: Baidu [Spider] and 2 guests