vpoptim: An interface to optim for optimisation over multiple...

Description Usage Arguments Examples

Description

vpoptim uses vpack to let you call optim on cost functions involving multiple arguments. See examples for usage.

Usage

1
vpoptim(guess, cfun, ...)

Arguments

guess

list of initial parameters

cfun

a cost function (the names of the arguments should be the *same ones* as in the "guess" argument)

...

passed on to optim

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#A cost function in two arguments:
cost <- function(a,b)
{
  (3*a-b+2)^2 
}
res <- vpoptim(list(a=1,b=0),cost)
res$par
##Won't work: the names in the list and the cost function need to be the same
try(res <- vpoptim(list(a=1,v=0),cost))
##Use optim for low-rank + diagonal approximation
X <- cov(USArrests)
cfun <- function(A,sigma2)
{
    lrank <- tcrossprod(A,A) + sigma2*diag(nrow(A)) 
    sum((X-lrank)^2)
}
guess <- list(A=matrix(0,4,2),sigma2=1)
opt <- vpoptim(guess,cfun)
opt$par
#Extra parameters are passed on to optim:
opt <- vpoptim(guess,cfun,method="BFGS")

dahtah/vecpack documentation built on May 14, 2019, 3:27 p.m.