Skip to content
All library documents

Choosing and Tuning R Optimizers for Portfolio Return Objectives

Article Quant Q&A · Author: Jcarl

Summary

The post asks how to maximize a portfolio return metric computed by a large R function with one bounded input parameter. The parameter is restricted to a range from 1.0 to 1.5, starts at 1.25, and only needs precision to increments of 0.05. The author reports that optimx with L-BFGS-B runs for about 30 minutes and asks about alternative packages, gradients and Hessians, step-size control, and method choice.

The central methodological issue is that supplying derivatives helps only when they can be derived or estimated reliably; a portfolio-building pipeline with filters may be nonsmooth, so analytic gradients may not be available or useful. Since this is a one-dimensional bounded search with coarse desired precision, a grid search or other derivative-free bounded approach could be suitable and may avoid effort spent defining Hessians. The post includes no benchmark comparing approaches, and it does not provide the full objective function, so runtime causes and the best optimizer cannot be determined from the example alone.

Key ideas

  • The objective is portfolio total return as a function of a bounded parameter.
  • The parameter lies between 1.0 and 1.5, with a desired step no finer than 0.05.
  • The author reports a roughly 30-minute run using optimx and L-BFGS-B.
  • Gradients and Hessians require suitable derivatives and may be a poor fit for a filtered, potentially nonsmooth objective.
  • The abbreviated code and lack of comparative benchmarks limit conclusions about the best package or method.

Tags

Full text
# Optimizing/maximizing portfolio returns & defining gradient function in R


# Optimizing/maximizing portfolio returns & defining gradient function in R












I have been searching on this forum and see some similar questions, such as: R packages for calibration (optimization/minimization) of pricing models , but I believe I have a somewhat different question.

I have some code in R for determining portfolio return metrics (total return, sharpe ratio, etc) over a given time series that is dependent on an input parameter (actually I have multipe input parameters, but I would like to figure this out for one parameter first). I'd like to get some advice on the best optimization package in R to use for this. I'm currently trying 'optimx' and 'nloptr', but like to know there is a better package or approach?

First, my input parameter is a constant where I'd like to set my lower and upper bound at 1.0 and 1.5, and then vary the input to produce the maximum total return. My current input parameter is 1.25, but I'd also like to restrict/define the step size to no more than 0.05.

Second, my objective function is a series of filters and processes to build a portfolio based on the input parameter. Ultimately the objective function calculates and returns the total return for the portfolio.

Below is the code I have:

```
    library(optimx)
    startx <- 1.25

    testfunc <- function(x) { "For simplicity, I'm leaving most of this objective function out as it's hundreds of lines of code.  But it includes a series of filters and calculations that returns a single number representing total return of the portfolio.  The last calculation of the function is below.
    .
    Ra_AnnRtns <- RaRb %>%
    tq_performance(Ra = Ra_using_weights, Rb = NULL, performance_fun = Return.annualized)

    return(Ra_AnnRtns)}

    anstest<-optimx(startx,fn=testfunc,gr=NULL, hess=NULL, lower=1, upper=1.5, 
    method="L-BFGS-B", control=list(save.failures=TRUE, maximize=TRUE, trace=1))
```

As you can see the gradient and hessian functions are NULL.

This code does work and produces a realistic result, but it is slow. It is currently taking about 30 mins to solve. What advice would you have for the following questions?

- Are there packages other than 'optimx' or 'nloptr' that would be better suited for my situation?

- If so, I realize that one thing I should do is provide gradient and hessian functions. Is this correct? If so, then how do you define a gradient function for my objective function? Is it somehow just a function of the total return calculation, or is it more complex that that? Similiar question for the hessian function.

- How can I define the step size in the optimx function as it doesn't need to be more accurante than 0.05?

- As you can see, I'm currently using the "L-BFGS-B" method. Is that a reasonable method to use or should I consider one of the other methods within 'optimx'?

Thanks in advance, and I realize I asked a lot of questions in this post so assistance on any of my questions is appreciated.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.