[英]How to find the second derivative in R and while using newton's method with numerical derivation
The log-likelihood of the gamma distribution with scale parameter 1 can be written as: 标度参数为1的伽马分布的对数似然可写为:
(α−1)s−nlogΓ(α)
where alpha is the shape parameter and s=∑logXi
is the sufficient statistic. (α−1)s−nlogΓ(α)
其中,α是形状参数,而s=∑logXi
是足够的统计量。
Randomly draw a sample of n = 30 with a shape parameter of alpha = 4.5. 随机绘制形状参数为alpha = 4.5的n = 30的样本。 Using newton_search
and make_derivative
, find the maximum likelihood estimate of alpha. 使用newton_search
和make_derivative
,找到alpha的最大似然估计。 Use the moment estimator of alpha, ie, mean of x as the initial guess. 使用alpha的矩估计量,即x的平均值作为初始猜测。 The log-likelihood function in R is: R中的对数似然函数为:
x <- rgamma(n=30, shape=4.5)
gllik <- function() {
s <- sum(log(x))
n <- length(x)
function(a) {
(a - 1) * s - n * lgamma(a)
}
}
I have created the make_derivative function as follows: 我创建了make_derivative函数,如下所示:
make_derivative <- function(f, h) {
(f(x + h) - f(x - h)) / (2*h)
}
I also have created a newton_search
function that incorporates the make_derivative
function; 我还创建了一个newton_search
结合了功能make_derivative
功能; However, I need to use newton_search
on the second derivative of the log-likelihood function and I'm not sure how to fix the following code in order for it to do that: 但是,我需要在对数似然函数的二阶导数上使用newton_search
,但我不确定如何修复以下代码才能做到这一点:
newton_search2 <- function(f, h, guess, conv=0.001) {
set.seed(2)
y0 <- guess
N = 1000
i <- 1; y1 <- y0
p <- numeric(N)
while (i <= N) {
make_derivative <- function(f, h) {
(f(y0 + h) - f(y0 - h)) / (2*h)
}
y1 <- (y0 - (f(y0)/make_derivative(f, h)))
p[i] <- y1
i <- i + 1
if (abs(y1 - y0) < conv) break
y0 <- y1
}
return (p[(i-1)])
}
Hint: You must apply newton_search
to the first and second derivatives (derived numerically using make_derivative
) of the log-likelihood. 提示:您必须将newton_search
应用于对数似然的一阶和二阶导数(使用make_derivative
通过数字make_derivative
)。 Your answer should be near 4.5. 您的答案应该在4.5附近。
when I run newton_search2(gllik(), 0.0001, mean(x), conv = 0.001)
, I get double what the answer should be. 当我运行newton_search2(gllik(), 0.0001, mean(x), conv = 0.001)
,得到的答案应该是newton_search2(gllik(), 0.0001, mean(x), conv = 0.001)
两倍。
I re-wrote the code and it works perfectly now (even better than what I had originally wrote). 我重新编写了代码,现在它可以完美运行(甚至比我最初写的还要好)。 Thanks to all who helped. 感谢所有的帮助。 :-) :-)
newton_search <- function(f, df, guess, conv=0.001) {
set.seed(1)
y0 <- guess
N = 100
i <- 1; y1 <- y0
p <- numeric(N)
while (i <= N) {
y1 <- (y0 - (f(y0)/df(y0)))
p[i] <- y1
i <- i + 1
if (abs(y1 - y0) < conv) break
y0 <- y1
}
return (p[(i-1)])
}
make_derivative <- function(f, h) {
function(x){(f(x + h) - f(x - h)) / (2*h)
}
}
df1 <- make_derivative(gllik(), 0.0001)
df2 <- make_derivative(df1, 0.0001)
newton_search(df1, df2, mean(x), conv = 0.001)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.