Get startedGet started for free

Objective functions

1. Objective functions

Objective functions compute the objective value for the optimizer to either maximize or minimize.

2. Objective functions

In portfolio optimization problems, common objective functions are risk, return, utility, and benchmark relative performance metrics. In the PortfolioAnalytics package, objective functions can be any valid R function. PortfolioAnalytics was designed to integrate with the PerformanceAnalytics package to use its wide array of risk, return and performance functions. For example, when you add an objective with name="StdDev", you are specifying that function from PerformanceAnalytics to be used as the objective function. The most common risk objective you will use in the course is StdDev, which calculates portfolio standard deviation. Another common risk measure in portfolio optimization is expected shortfall which is calculated with the ES function. While this course will focus on risk measures for portfolios, if you are concerned with performance relative to a benchmark, you can also include benchmark relative metrics as objective functions.

3. Custom objective functions

Recall that an objective function is just a valid R function. This means that custom, user defined functions can be used as objective functions. While the PerformanceAnalytics package has support for a large number of performance measures, a portfolio manager or analyst may wish to use his or her own proprietary functions as objective functions. There are a few guidelines for defining a custom objective function. The function arguments should be named R for the asset returns, weights for the portfolio weights, and mu, sigma, m3, m4 for the first, second, third, and fourth moment of asset returns, respectively. These specific argument names are matched automatically and handled in an efficient manner. As is standard for optimizers, the objective function should return a single value.

4. Example: custom objective function

Here you start by defining a function to calculate annualized Sharpe Ratio. The Sharpe Ratio is a risk adjusted return measure and is calculated by dividing the excess portfolio annualized return by the annualized portfolio standard deviation. You follow the guidelines by using R for the returns, weights for the weights, and sigma for the second moment. You need two additional arguments, scale for annualizing and rfr for the risk free rate. Because the portfolio return is annualized, the risk free rate should also be in annualized terms. First, you calculate the annualized return of the portfolio, then subtract the risk free rate to get the excess annualized return. Next, you calculate portfolio variance which is the transpose of the weights times the variance-covariance matrix, sigma, times the weights. The square root of the variance is the standard deviation so you take the square root of the variance and multiply by the square root of the scale argument, for example 12 for monthly returns, to calculate the portfolio annualized standard deviation. Finally, you calculate the annualized Sharpe Ratio by dividing the portfolio annual excess return by the portfolio annual standard deviation.

5. Example: custom objective function

Next, we load the edhec dataset where we will only use the first four columns for this example problem. We create the portfolio specification with full investment and long only constraints. We add the objective function and specify the name of our custom objective function as the name argument. We specify type="return" because we want to maximize the objective measure. We need to specify scale=12 and rfr=0-point-02 as a named list for the additional arguments to be passed to the objective function.

6. Let's practice!

Now, let's practice what you just learned.