我正在使用 pracma
包,特别是 Brent-Dekker 寻根算法,来搜索一个函数的根,该函数将观测值的变异系数 (CV obs) 设置为等于模拟值 (CV模拟)。
虽然在个人基础上我可以为算法提供初始间隔的良好估计(例如 brent(f, a, b)
中的 [a,b]),但我正在处理大量的年份/地区/等。我正在为此找到根,并且间隔范围不是那么宽容 - 就我遇到的而言,它需要非常窄。这是我的代码的一般要点:
library(pracma)
CV <- function(x){ #coefficient of variation
sd(x)/mean(x)
}
fb <- function(b){ #this is my function
CV(obs)-CV(sim^b)
}
for (i in ...) { #regions
for (j in ...) { #years... etc.
obs <- some.dataframe
sim <- some.df.2
z1 <- brent(fb, 0.5,2) #where z1$root is the solution
}
}
我想知道是否有相关的包或方法来确定我的 for 循环中的初始间隔,或者是否有修改以便我可以将此间隔留空(我不使用 Python,但这里例如 https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.brent.html间隔是可选的)。
我希望这是有道理的,很乐意提供更多背景信息。
最佳答案
我不知道这是否符合您的要求,但是 stats::uniroot
使用(某种形式的)布伦特方法:archive pointed to by ?stats::uniroot
说:
This file contains Brent's univariate minimizer and zero finder.
C realization (with adaptation) of the algorithm
G.Forsythe, M.Malcolm, C.Moler, Computer methods for
mathematical computations.
uniroot()
有一个 extendInt
参数,默认为“no”但可以设置为“yes”:
extendInt
: character string specifying if the interval ‘c(lower,upper)’ should be extended or directly produce an error when ‘f()’ does not have differing signs at the endpoints.
因此您不能将间隔留空,但算法会尝试优雅地调整“坏”选择。
关于r - Brent-Dekker 间隔 (pracma),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41884292/