R 的 {pracma} 和 {numDeriv} 库中的 grad 函数给出了错误的结果
The grad function in both the {pracma} and the {numDeriv} libraries of R gives erroneous results
我对自定义函数 pTgh_y(q,g,h)
关于 q 的一阶数值导数感兴趣。对于特殊情况,pTgh_y(q,0,0) = pnorm(q)。换句话说,当 g=h=0 时,pTgh_y(q,g,h)
被简化为标准法线的 CDF(见下图)。
这意味着d pTgh_y(0,0,0)/dq应该等于下面的
dnorm(0)
0.3989423
grad(pnorm,0)
0.3989423
以下是我对 {pracma} 库中的 grad 函数的一些尝试。
library(pracma)
# load pTgh and all relevant functions
grad(function(x){pTgh_y(x,0,0)},0)
0
grad(function(x){pTgh_y(x,0,0)},0,heps=1e-10)
0
以下是我对 {numDeriv} 库中的 grad 函数的一些尝试。
library(numDeriv)
# load pTgh and all relevant functions
grad(function(x){pTgh_y(x,0,0)},0,method='simple')
0.3274016
grad(function(x){pTgh_y(x,0,0)},0,method='Richardson')
-0.02505431
grad(function(x){pTgh_y(x,0,0)},0,method='complex')
Error in pmin(x, .Machine$double.xmax) : invalid input type
Error in grad.default(function(x) { :
function does not accept complex argument as required by method 'complex'.
None 这些函数给出了正确的结果。
我的pTgh_y(q,g,h)
函数定义如下
qTgh_y = function(p,g,h){
zp = qnorm(p)
if(g==0) q = zp
else q = (exp(g*zp)-1)*exp(0.5*h*zp^2)/g
q[p==0] = -Inf
q[p==1] = Inf
return(q)
}
pTgh_y = function(q,g,h){
if (q==-Inf) return(0)
else if (q==Inf) return(1)
else {
p = uniroot(function(t){qTgh_y(t,g,h)-q},interval=c(0,1))
return(p$root)
}
}
你的函数在 0 附近是平坦的,因此计算 0 的导数是正确的:
f = function(x){pTgh_y(x,0,0)}
h = 0.00001; f(0+h); f(0-h)
# [1] 0.5
# [1] 0.5
library(pracma)
grad(f, 0, heps=1e-02); grad(f, 0, heps=1e-03);
grad(f, 0, heps=1e-04); grad(f, 0, heps=1e-05)
# [1] 0.3989059
# [1] 0.399012
# [1] 0.4688766
# [1] 0
您需要提高函数的准确性pTgh_y
:
pTgh_y = function(q,g,h){
if (q==-Inf) return(0)
else if (q==Inf) return(1)
else {
p = uniroot(function(t){qTgh_y(t,g,h)-q},interval=c(0,1),
tol = .Machine$double.eps)
return(p$root)
}
}
现在您得到了想要的结果:
f = function(x){pTgh_y(x,0,0)}
grad(f, 0)
[1] 0.3989423
我对自定义函数 pTgh_y(q,g,h)
关于 q 的一阶数值导数感兴趣。对于特殊情况,pTgh_y(q,0,0) = pnorm(q)。换句话说,当 g=h=0 时,pTgh_y(q,g,h)
被简化为标准法线的 CDF(见下图)。
这意味着d pTgh_y(0,0,0)/dq应该等于下面的
dnorm(0)
0.3989423
grad(pnorm,0)
0.3989423
以下是我对 {pracma} 库中的 grad 函数的一些尝试。
library(pracma)
# load pTgh and all relevant functions
grad(function(x){pTgh_y(x,0,0)},0)
0
grad(function(x){pTgh_y(x,0,0)},0,heps=1e-10)
0
以下是我对 {numDeriv} 库中的 grad 函数的一些尝试。
library(numDeriv)
# load pTgh and all relevant functions
grad(function(x){pTgh_y(x,0,0)},0,method='simple')
0.3274016
grad(function(x){pTgh_y(x,0,0)},0,method='Richardson')
-0.02505431
grad(function(x){pTgh_y(x,0,0)},0,method='complex')
Error in pmin(x, .Machine$double.xmax) : invalid input type Error in grad.default(function(x) { : function does not accept complex argument as required by method 'complex'.
None 这些函数给出了正确的结果。
我的pTgh_y(q,g,h)
函数定义如下
qTgh_y = function(p,g,h){
zp = qnorm(p)
if(g==0) q = zp
else q = (exp(g*zp)-1)*exp(0.5*h*zp^2)/g
q[p==0] = -Inf
q[p==1] = Inf
return(q)
}
pTgh_y = function(q,g,h){
if (q==-Inf) return(0)
else if (q==Inf) return(1)
else {
p = uniroot(function(t){qTgh_y(t,g,h)-q},interval=c(0,1))
return(p$root)
}
}
你的函数在 0 附近是平坦的,因此计算 0 的导数是正确的:
f = function(x){pTgh_y(x,0,0)}
h = 0.00001; f(0+h); f(0-h)
# [1] 0.5
# [1] 0.5
library(pracma)
grad(f, 0, heps=1e-02); grad(f, 0, heps=1e-03);
grad(f, 0, heps=1e-04); grad(f, 0, heps=1e-05)
# [1] 0.3989059
# [1] 0.399012
# [1] 0.4688766
# [1] 0
您需要提高函数的准确性pTgh_y
:
pTgh_y = function(q,g,h){
if (q==-Inf) return(0)
else if (q==Inf) return(1)
else {
p = uniroot(function(t){qTgh_y(t,g,h)-q},interval=c(0,1),
tol = .Machine$double.eps)
return(p$root)
}
}
现在您得到了想要的结果:
f = function(x){pTgh_y(x,0,0)}
grad(f, 0)
[1] 0.3989423