本文介绍了将多个起始值传递给nlminb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过do.call和apply,也有一个类似的nlminb答案,它使用了plyr软件包,但仍然没有效果.因此,我向大家寻求任何建议.

I've tried do.call and apply, and there was a similar nlminb answer that used the plyr package but still no good. So I turn to you all for any suggestions.

我创建了以下功能:

calloptim <- function( under,strike, rf, ttoe,par) {(-(under*par[1]
  -strike*exp(-rf*ttoe)*par[2]))^2}

然后使用nlminb来估计par,同时保持其他参数不变:

and then used nlminb to estimate par while holding the other arguments constant:

nlminb(c(2,2), calloptim, under= 90, strike = 100, rf =0.05, ttoe=3)

产生:

$par
[1] 1.953851 2.043045

$objective
[1] 1.335531e-17

$convergence
[1] 0

$iterations
[1] 4

$evaluations
function gradient
       6       10

$message
[1] "X-convergence (3)"

例如,当我输入另一个起始值时

when I put in another starting value, for example

nlminb(c(5,5), calloptim, under= 90, strike = 100, rf =0.05, ttoe=3)

我得到不同的估计:

$par
[1] 4.885987 5.109036

$objective
[1] 2.464145e-14

$convergence
[1] 1

$iterations
[1] 2

$evaluations
function gradient
      33        4

$message
[1] "false convergence (8)"

那就可以了!我从数学上了解发生了什么.实际上,我想使用不同的起始值.

And thats ok! I understand mathematically what's happening. In fact I want to use different starting values.

当我尝试将多个初始值传递给nlminb时,就会出现问题.

My problem arises when I try to pass multiple starting values to nlminb.

我创建一个矩阵:

f<- c(2,5,2,5)
dim(f) <- c(2,2)

> f
     [,1] [,2]
[1,]    2    2
[2,]    5    5

但是当我将f传递给nlminb的起始值时

But when I pass f to nlminb's starting value

nlminb(f, calloptim, under= 90, strike = 100, rf =0.05, ttoe=3)

我得到:

$par
[1] 3.452902 3.610530 2.000000 5.000000

$objective
[1] 3.010198e-19

$convergence
[1] 0

$iterations
[1] 4

$evaluations
function gradient
      22       24

$message
[1] "X-convergence (3)"

所以我的问题是如何将多行起始值传递给nlminb?

So my question is how can I pass multiple rows of starting values to nlminb?

谢谢您的建议!

黑麦

推荐答案

由于?nlminb说它的第一个参数应该是数字矢量,因此需要apply到矩阵f的每一行.

Since ?nlminb says its first argument should be a numeric vector, you need apply it to each row of your matrix f.

out <- apply(f, 1, nlminb, objective=calloptim, under=90, strike=100, rf=0.05, ttoe=3)

str(out)

List of 2
 $ :List of 6
  ..$ par        : num [1:2] 1.95 2.04
  ..$ objective  : num 1.34e-17
  ..$ convergence: int 0
  ..$ iterations : int 4
  ..$ evaluations: Named int [1:2] 6 10
  .. ..- attr(*, "names")= chr [1:2] "function" "gradient"
  ..$ message    : chr "X-convergence (3)"
 $ :List of 6
  ..$ par        : num [1:2] 4.89 5.11
  ..$ objective  : num 2.46e-14
  ..$ convergence: int 1
  ..$ iterations : int 2
  ..$ evaluations: Named int [1:2] 33 4
  .. ..- attr(*, "names")= chr [1:2] "function" "gradient"
  ..$ message    : chr "false convergence (8)"

这篇关于将多个起始值传递给nlminb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 16:51