Learning R For Finance – Post 24
The portfolio efficient frontier is the set of portfolios that offer the maximum expected return for any given level of risk. In the original formulation by prof. Harry Markowitz more than half a century ago, risk was defined as the variance of the expected returns and the main idea was that risk could be reduced if the assets composing the portfolio were not all perfectly correlated. Fundamentally, if asset prices did not move all in the same direction at the same time, portfolio diversification could yield the same return with less risk or, alternatively, higher returns with the same risk. If we can measure assets returns, variances and covariances (correlations), and we apply the standard mathematical technique used to solve optimization problems, we can determine a set of optimal portfolios for any given level of risk (or for any given level of return).
It is important to note that the Markowitz approach was put forward as a comparative statics problem as it was typical in the economic theory of that time. In other words, in this model time plays no role: returns, variances and assets covariances (correlations) are assumed to be practically the same in the future as they were in the past. No assumption is made about the dynamics of these variables, about the existence of a possible relationship between expected asset returns and their own expected variances or about the possibility that covariances become all highly positive at the same time during a market crisis.
In spite of all this, or perhaps because no satisfactory solution to these issues has yet been found, the efficient frontier remains a useful reference for investors and the listing below shows how it can be computed in R using the PortfolioAnalytics package. In this example, we compute the efficient frontier using two different risk measures: the standard deviation (sigma) and the expected shortfall (ES) and we use a modified version of one of the demo scripts included in the PortfolioAnalytics package.
The data used in this example are the daily prices of nine ETF’s listed in euro in the period January 2 2007 – October 26 2015 and they can be downloaded here. Remember to remove the .doc extension before using this file and change the dash in the name to a dot (sorry about this, but WordPress does not allow uploading files with .dat or .Rdata extensions and moreover, it converts dots in the names to dashes…).
# Load required packages
# the following packages contain the optimzation algorithms used by PortfolioAnalytics:
# this package provides multicore capabilities to PortfolioAnalytics:
# Load the data and take out the ".Close" extension from the asset names for shorter legends in plotting.
names(prices) = gsub(".Close","",names(prices))
# convert to monthly prices
ep = endpoints(prices, "months")
ep = ep[-1]
mon.prices = prices[ep,]
# compute monthly discrete returns
R = na.omit(Return.calculate(mon.prices,method="discrete"))
# create a vector containing the asset names
funds = names(R)
# Set up the initial portfolio object with some basic constraints.
init = portfolio.spec(assets=funds)
# the "type=leverage" argument allows to specify asset weights that are below
# zero and greater than 1: this can be used for long-short portfolio or
# (as in this case) to allow some numerical slack (min -0.005, max = 1.005) in
# the optimization algorithms
init = add.constraint(portfolio=init, type="leverage")
init = add.constraint(portfolio=init, type="box", min=0, max=1)
# the following instruction adds some "group constraints": here we force the
# portfolio to always hold at least 10% of the assets in bond ETFs (EM13IM,
# IBCXIM and/or EMGIM) and 10% in equity ETFs while at the same time, none of
# these two groups can exceed 60% of the portfolio. The numbers in the argument
# "groups" refer to the ETF position in the vector "funds". Note that the
# "add.constaint" instruction actually allows for many other configurations.
init = add.constraint(portfolio=init, type="group", groups=list(c(3, 6, 7), c(1, 2, 4, 5, 8, 9)), group_min=0.1, group_max=0.6)
# We now add the objectives for a portfolio that uses the constraints above and
# we call it "meanES.portf".
# First, we want to minimize the risk (with the argument type= "risk") where
# risk is here defined as the expected shortfall ("ES"):
meanES.portf = add.objective(portfolio=init, type="risk", name="ES")
# Second, we want to achieve the maximum possible return (with the argument
# type= "return") defined as the mean of the monthly returns:
meanES.portf = add.objective(portfolio=meanES.portf, type="return", name="mean")
# Warning messages stating "In is.na(le) : is.na() applied to non-(list or vector)
# of type 'NULL'" that may appear while running the program are due to an issue with
# the current version (1.03636) of the PortfolioAnalysis package that does not affect
# the results and that can be safely ignored.
# We now define a second portfolio called "meanSigma.portf" where we want to minimize
# the risk defined as standard deviation ("StdDev"):
meanSigma.portf = add.objective(portfolio=init, type="risk", name="StdDev")
# even in the second portfolio we want to achieve the maximum possible return:
meanSigma.portf = add.objective(portfolio=meanSigma.portf, type="return", name="mean")
In the next post we will execute these instructions.
(to be continued)
 More precisely the maximization of returns for a given variance is a linear optimization problem with quadratic constraints, while the minimization of risk for a given return is a quadratic optimzation problem with linear constraints. The mathematical solutions to these problems can be found in any good investment textbook.
Learning R For Finance
With these posts I just wish to share my experience in learning how to use R to solve some real life financial problems. This will be mostly done introducing and discussing some programs listings. See here for an introduction. See also here for the folder structure that is used in the program listings.