Julia中的Optim尝试导致数值误差NaN的极值

问题描述

我有一个目标函数,看起来像

function myfun(θ,x)
    logτ = θ
    s(ϵ) = 1.0/sqrt(exp(logτ)) * ϵ
    p(ϵ) = 1/(1 + 0.5 * exp((0.5 - s(ϵ))*exp(logτ)))
    integrand(ϵ) = normcdf(0.0,1.0,x + p(ϵ))*(1/sqrt(2.0*π)*exp(-(ϵ^2.0)/2.0)) 
    (expectation,err) = quadgk(ϵ -> integrand(ϵ),-4.0,4.0,rtol = 0.0001)
    return expectation
end

我使用logτ作为参数,以确保优化为τ提供正值。当我运行优化时,经过几次迭代我通常会得到

ERROR: DomainError with 0.0:
integrand produced NaN in the interval (-4.0,4.0)

这是因为在搜索过程中τ接近零时,例如logτ= -1000时,exp(logτ)恰好返回零,而不是很小的正值。在这种情况下,s(ϵ)变成Inf,而p(ϵ)变成NaN。 我试图添加

    if logτ < -100
        logτ = -100
    end

这是不希望的,因为它会大大降低优化速度。有没有更好的解决方案来解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...