r中的最优:非有限差分误差

时间:2015-12-18 18:27:24

标签: r optimization

我有一个简单的似然函数(来自普通的dist,其中均值= 0),我想要最大化。 optim一直给我这个错误: 优化错误(par = phi,fn = loglike,估计=估计,NULL,hessian = TRUE,:非有限有限差分值[1]

这是我的数据和似然函数:

apm uninstall \
hyperclick \
nuclide-arcanist \
nuclide-blame \
nuclide-blame-provider-hg \
nuclide-blame-ui \
nuclide-buck-files \
nuclide-busy-signal \
nuclide-clang-atom \
nuclide-clipboard-path \
nuclide-code-format \
nuclide-code-highlight \
nuclide-debugger-atom \
nuclide-debugger-hhvm \
nuclide-debugger-lldb \
nuclide-diagnostics-store \
nuclide-diagnostics-ui \
nuclide-diff-view \
nuclide-file-tree \
nuclide-file-watcher \
nuclide-find-references \
nuclide-flow \
nuclide-format-js \
nuclide-fuzzy-filename-provider \
nuclide-hack \
nuclide-hack-symbol-provider \
nuclide-health \
nuclide-hg-repository \
nuclide-home \
nuclide-installer \
nuclide-language-hack \
nuclide-move-pane \
nuclide-objc \
nuclide-ocaml \
nuclide-open-filenames-provider \
nuclide-quick-open \
nuclide-react-native-inspector \
nuclide-recent-files-provider \
nuclide-recent-files-service \
nuclide-remote-projects \
nuclide-service-monitor \
nuclide-test-runner \
nuclide-toolbar \
nuclide-type-hint \
nuclide-url-hyperclick

我的对数似然函数是: lglkl = -.5 *(log(det(v))+(t(y)%%vi%%y))`其中v = phi * I + C和vi = inverse(v )和I = 5 * 5单位矩阵。

我试图得到“phi”的mle估计值。我认为这将是一个简单的优化问题,但我正在努力。非常感谢任何帮助。提前致谢。我的代码如下:

y =  [ -0.01472  0.03942  0.03592 0.02776 -0.00090 ]

C = a varcov matrix:

  1.66e-03 -0.000120 -6.780e-06  0.000102 -4.000e-05

 -1.20e-04  0.001387  7.900e-05 -0.000140 -8.000e-05

 -6.78e-06  0.000079  1.416e-03 -0.000070  8.761e-06

  1.02e-04 -0.000140 -7.000e-05  0.001339 -6.000e-05

 -4.00e-05 -0.000080  8.761e-06 -0.000060  1.291e-03
  

1 个答案:

答案 0 :(得分:2)

您遇到的错误是因为φ在超过一定次数的迭代后变为负值(这表示算法未正确应用约束)。此外,该解决方案不会收敛到单个值,而是在达到更新的协方差矩阵不再是正定的情况之前在几个小值之间跳转。在那个阶段你得到det(v)< 0和log [det(v)]未定义。 optim算法在那个阶段纾困。

要查看正在发生的情况,请使用以下代码中的maxitndeps参数。

require("matrixcalc")

#-------------------------------------------------
# Log-likelihood function
#-------------------------------------------------
loglike <- function(phi, y) {

  # Shift the covariance matrix
  print(paste("phi = ", phi))
  #v = phi*I + (1 - phi)*C
  v = phi*I + C
  stopifnot(is.positive.definite(v))

  # Invert shifted matrix
  vi = solve(v)

  # Compute log likelihood
  loglike = -.5*(log(det(v)) + (t(y) %*% vi %*% y))
  print(paste("L = ", loglike))

  return(-loglike)
}

#-------------------------------------------------
# Data
#-------------------------------------------------
y = c(-0.01472, 0.03942, 0.03592, 0.02776, -9e-04) 
C =  structure(c(0.00166, -0.00012, -6.78e-06, 0.000102, -4e-05, -0.00012, 
                 0.001387, 7.9e-05, -0.00014, -8e-05, -6.78e-06, 7.9e-05, 
                 0.001416, -7e-05, 8.761e-06, 0.000102, -0.00014, -7e-05, 
                 0.001339, -6e-05, -4e-05, -8e-05, 8.761e-06, -6e-05, 0.001291), 
                 .Dim = c(5L, 5L ))

#--------
# Initial parameter
#--------
I = diag(5)
phi = 50

#--------
# Minimize
#--------
parm <- optim(par = phi, fn = loglike, y = y, NULL, hessian = TRUE, 
              method = "L-BFGS-B", lower = 0.0001, upper = 1000,
              control = list(trace = 3,
                             maxit = 1000,
                             ndeps = 1e-4) )