[R-SIG-Finance] Tracing gradient during optimization

Rob Steele gmain.20.phftt at xoxy.net
Tue Feb 24 16:14:16 CET 2009


Enclose your objective function in an environment that contains the
state you want to track and some way to get at it.  This lets you track
as much detail as you like.  Something like:


make.objective <- function(blah, blah, ...)
{
    log <- list()

    fn <- function(params)
    {
        log[[length(log) + 1]] <<- <state info>
        fitness <- <...>
        return(fitness)
    }

    print.log <- funciton()
    {
        ...
    }

    plot.log <- function()
    {
        ....
    }

    return(list(fn        = fn,
                print.log = print.log,
                plot.log  = plot.log))
}


objective <- make.objective(...)

o <- optim(fn = objective$fn, ...)

objective$print.log()
objective$plot.log()


By the way, it's more efficient to pre-allocate log space in a data
frame and only grow it occasionally rather than to add elements to a
list one at a time as I show here.  This works however.

Good hunting!


Shimrit Abraham wrote:
> Hi everyone,
> 
> I am currently using the function optim() to maximize/minimize functions and
> I would like to see more output of the optimization procedure, in particular
> the numerical gradient of the parameter vector during each iteration.
> The documentation of optim() describes that the trace parameter should allow
> one to trace the progress of the optimization.
> I use the following command:
> 
> optim(par = vPar,
>          fn = calcLogLik,
>          method = "BFGS",
>          control = list(trace = TRUE, fnscale = -1, maxit = 2000));
> 
> which gives very little information:
> 
> initial  value 3.056998
> final  value 2.978351
> converged
> 
> Specifying trace >1, for instance trace = 20, does not result in more
> information. Is there a way to view more details of the progress perhaps by
> using another optimizer?
> 
> Thanks,
> 
> Shimrit Abraham
> 
> 	[[alternative HTML version deleted]]
>



More information about the R-SIG-Finance mailing list