Error in optim: function cannot be evaluated at initial parameters [closed]

跟風遠走 提交于 2019-12-07 04:04:16

问题


So I've run into this weird error in R. I have a simple function which returns an error term when comparing real and simulated prices, called hestondifferences().

when I try to find the local minima via:

 res<-optim(fn=hestondifferences, par = c(vT=vT, rho=rho, k=k, sigma=sigma))

I get the error message:

Error in optim(fn = hestondifferences, par = c(vT = vT, rho = rho, k = k, : function cannot be evaluated at initial parameters

What confuses me is that calling the function directly with the initial parameters hestondifferences(vT, rho, k, sigma) returns the correct value.

The function hestondifferences() is written in a way that whenever the simulation is impossible for a certain set of parameters, it returns NA which is in line with what optim() expects.


回答1:


Optim expects functions to just have one argument. All further arguments should hence be passed in a vector. That is: the function must be hestondifferences(c(vT, rho, k, sigma)) instead of hestondifferences(vT, rho, k, sigma). See the documentation:

fn : A function to be minimized (or maximized), with first argument the vector of parameters over which minimization is to take place. It should return a scalar result.



来源:https://stackoverflow.com/questions/15068213/error-in-optim-function-cannot-be-evaluated-at-initial-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!