r - How can I find a maximum to this equation -
i have function find it's maximum
deposit_likelood <- function(a1,a2) { (0.5672 - 0.092 * a1 + 0.0044 * a2^2 }
how can maximize deposit_likelood while a1 should between -3 , +3
, a2 should between 0.5 , 0.9
?
i tried use optimize() function:
optimize(deposit_likelood, interval=c(-3,3,0.5,0.9), maximum=true)
but got error :
error in a2^2 : 'a2' missing
i hoping work out hints (because thats better way learn) here go.
note i've had change function because question didn't have valid function in it, , unpack arguments:
> deposit_likelood = function(a) {a1=a[1];a2=a[2];return (0.5672 - 0.092 * a1 + 0.0044 * a2^2) }
we give optim
start point (somewhere in box constraints), tell use method box constraints, , specify constraints:
> optim(c(0,.7),deposit_likelood,method="l-bfgs-b",lower=c(-3,.5), upper=c(3,.9), control=list(fnscale=-1)) $par [1] -3.0 0.9 $value [1] 0.846764 $counts function gradient 7 7 $convergence [1] 0 $message [1] "convergence: norm of projected gradient <= pgtol"
the return value has $par
values of a1
, a2
respectively. $convergence
code 0 tells worked okay.
Comments
Post a Comment