为mle提供R中的梯度

为mle提供R中的梯度,r,gradient,derivative,estimation,mle,R,Gradient,Derivative,Estimation,Mle,我的目标是将我的目标对数(密度)函数的梯度提供给mle(),因为这显然加快了收敛过程并使其更加稳定(根据) 我的简化LL、少量数据和标准mle如下所示: library(stats4) LL <- function(n,s) { V = (u(z1,n)-u(z2,n))*p + u(z2,n) res = zce - u.inv(V,n) ll = dnorm(x=res, mean=0, sd=s,log=T) return(-sum(ll)) } ### Func

我的目标是将我的目标对数(密度)函数的梯度提供给mle(),因为这显然加快了收敛过程并使其更加稳定(根据)

我的简化LL、少量数据和标准mle如下所示:

library(stats4)
LL <- function(n,s)
{
  V = (u(z1,n)-u(z2,n))*p + u(z2,n) 
  res = zce - u.inv(V,n)
  ll = dnorm(x=res, mean=0, sd=s,log=T)
  return(-sum(ll))
}

### Functions:
u <- function(x,n) 
{
  ifelse(n!=1, util <- x^(1-n)/(1-n), util <- log(x))
  return(util)
}
u.inv <- function(x,n)
{
  ifelse(n !=1, inv.util <- ((1-n)*(x))^(1/(1-n)), inv.util <- exp(x))
  return(inv.util)
}

### Data 
z1 <- c(0.1111111, 0.1037037, 0.1222222, 0.1111111, 0.1074074, 0.1666667, 0.1333333, 0.2000000, 0.1333333, 0.1074074,
        0.1037037, 0.1111111, 0.1333333, 0.2000000, 0.1222222, 0.1111111, 0.1666667, 0.1333333, 0.1111111, 0.1333333,
        0.1111111, 0.1666667, 0.1074074, 0.1333333, 0.1222222, 0.2000000, 0.1037037)

z2 <- c(0.08888889, 0.06666667, 0.07777778, 0.00000000, 0.03333333, 0.09259259, 0.09629630, 0.08888889, 0.06666667,
        0.03333333, 0.06666667, 0.08888889, 0.06666667, 0.08888889, 0.07777778, 0.00000000, 0.09259259, 0.09629630,
        0.00000000, 0.09629630, 0.08888889, 0.09259259, 0.03333333, 0.06666667, 0.07777778, 0.08888889, 0.06666667)

p <-  c(0.5, 0.9, 0.5, 0.9, 0.9, 0.1, 0.1, 0.1, 0.5, 0.9, 0.9, 0.5, 0.5, 0.1, 0.5, 0.9, 0.1, 0.1, 0.9, 0.1, 0.5, 0.1, 0.9, 0.5, 0.5, 0.1, 0.9)

zce <- c(0.11055556, 0.10277778, 0.11000000, 0.10833333, 0.10185185, 0.11666667, 0.13240741, 0.14166667, 0.13166667,
         0.07222222, 0.08796296, 0.09944444, 0.09500000,0.10833333, 0.09444444, 0.05277778, 0.10925926, 0.11759259,
         0.05833333, 0.10277778, 0.09277778, 0.10925926, 0.06111111, 0.08833333, 0.09222222, 0.12500000, 0.09166667)

### mle()
fit <- mle(LL,
           start = list(n = 0.1,s=0.1),
           method = "L-BFGS-B",
           lower = list(n=-Inf,s=0.0001),
           upper = list(n=0.9999,s=Inf),
           control = list(maxit = 500, ndeps = rep(0.000001,2),trace= 6),
           nobs=length(z1))
谢谢你的帮助

更新1

现在我相信我通过梯度的方式确实是向mle()提供梯度的正确方式,这回答了我文章的主要问题

我仍然不太明白的是如何将潜在参数值,即优化过程中任何迭代的参数值传递给梯度函数。我考虑将潜在参数值放入变量“par”中,以便将其传递给梯度函数:

LL <- function(n,s)
{
  V = (u(z1,n)-u(z2,n))*p + u(z2,n) 
  res = zce - u.inv(V,n)
  ll = dnorm(x=res, mean=0, sd=s,log=T)
  par <- c(n,s)
  return(-sum(ll))
}

LL.gr <- function(par, zz1=z1 ,zz2=z2,pp=p,zcee=zce)
{
  n <- par[1]
  s <- par[2]
  V = (u(zz1,n)-u(zz2,n))*pp + u(zz2,n) 
  res = zcee - u.inv(V,n)
  print(s)
  c(sum(-1/(2*s^2)*2*res*-d.u.inv.n(V,n)*( (d.u.n(zz1,n)-d.u.n(zz2,n))*pp + d.u.n(zz2,n))),
    sum(-1/s + res^2*s^(-3))
  )
}

有人知道我为什么要把起始值取出来吗?我传递潜在变量的方法正确吗?再次感谢。

在ll.gr中,n和s都应该从PAR[1]中提取吗?不,编辑了,谢谢。老实说,我没有这种提供PAR的方法,我发现它非常不直观(这也是我喜欢mle而不是optim的原因之一)。对于这篇文章,我只是从链接中复制粘贴技术。
N = 2, M = 5 machine precision = 2.22045e-16
L = -inf 0.001 
X0 = 0.1 0.1 
U = 0.9999 inf 
At X0, 0 variables are exactly at the bounds
At iterate     0  f=      -36.749  |proj g|=       257.81
Iteration     0

---------------- CAUCHY entered-------------------

There are 0  breakpoints

GCP found in this segment
Piece      1 f1, f2 at start point         nan         nan
Distance to the stationary point =          nan
Cauchy X =  nan nan 

---------------- exit CAUCHY----------------------

2  variables are free at GCP on iteration 1
Error in optim(start, f, method = method, hessian = TRUE, ...) : 
  non-finite value supplied by optim
LL <- function(n,s)
{
  V = (u(z1,n)-u(z2,n))*p + u(z2,n) 
  res = zce - u.inv(V,n)
  ll = dnorm(x=res, mean=0, sd=s,log=T)
  par <- c(n,s)
  return(-sum(ll))
}

LL.gr <- function(par, zz1=z1 ,zz2=z2,pp=p,zcee=zce)
{
  n <- par[1]
  s <- par[2]
  V = (u(zz1,n)-u(zz2,n))*pp + u(zz2,n) 
  res = zcee - u.inv(V,n)
  print(s)
  c(sum(-1/(2*s^2)*2*res*-d.u.inv.n(V,n)*( (d.u.n(zz1,n)-d.u.n(zz2,n))*pp + d.u.n(zz2,n))),
    sum(-1/s + res^2*s^(-3))
  )
}
z2 <- c(0.08888889, 0.06666667, 0.07777778, 0.0001, 0.03333333, 0.09259259, 0.09629630, 0.08888889, 0.06666667,
            0.03333333, 0.06666667, 0.08888889, 0.06666667, 0.08888889, 0.07777778, 0.0001, 0.09259259, 0.09629630,
            0.0001, 0.09629630, 0.08888889, 0.09259259, 0.03333333, 0.06666667, 0.07777778, 0.08888889, 0.06666667)

mle(LL,
           start = list(n = 0.1,s=0.1),
           method = "L-BFGS-B",
           lower = list(n=-Inf,s=0.001),
           upper = list(n=0.9999,s=Inf),
           control = list(maxit = 500, ndeps = rep(0.0001,2),trace= 6),
           nobs=length(z1),
           gr = LL.gr)

mle(minuslogl = LL, start = list(n = 0.1, s = 0.1), method = "L-BFGS-B", 
    nobs = length(z1), lower = list(n = -Inf, s = 0.001), upper = list(n = 0.9999, 
        s = Inf), control = list(maxit = 500, ndeps = rep(1e-04, 
        2), trace = 6), gr = LL.gr)

Coefficients:
  n   s 
0.1 0.1