Driver 2: The choice of portfolio weights
Investors can optimize the choice of weight to obtain the highest risk-adjusted return, as measured by the portfolio Sharpe ratio.
In the special case of investing the total portfolio value in only two assets, there is only one weight to determine, because the weight on the second asset equals one minus the weight of the first asset.
Let us do this in the case of a portfolio invested in U.S. equities and U.S. bonds. We will be using the brute force approach of trying a large number of possible weights and keeping the weight that yields the highest value for the portfolio Sharpe ratio (assuming a zero risk-free rate).
This exercise is part of the course
Introduction to Portfolio Analysis in R
Exercise instructions
- Create a vector called
grid
usingseq()
that begins at 0, ends at 1, by an increment of 0.01. - Initialize empty vector
vsharpe
with the same length asgrid
. A popular way of doing this is by creating a vector that contains NA's using function rep(). You will replace these NA's in the loop that you will create next. - In the for loop, you will compute the Sharpe ratio for each of the possible weights in
grid
. The first command in the for-loop selects the i-th element of grid and stores it in objectweight
, which changes in each iteration. - You want to see how the portfolio return changes with a changing weight. Create an object
preturns
that equals the sum ofweight
timesreturns_equities
, and(1-weight)
timesreturns_bonds
. - Next, you will replace the NAs in
vsharpe
with the annualized Sharpe ratio (SharpeRatio.annualized()
) ofpreturns
. - Fill in the plot function where potential weights (
grid
) is plotted on the x-axis and the Sharpe ratios on the y-axis.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a grid
grid <- seq(from = ___, to = ___, by = ___)
# Initialize an empty vector for Sharpe ratios
vsharpe <- rep(NA, times = ___ )
# Create a for loop to calculate Sharpe ratios
for(i in 1:length(grid)) {
weight <- ___[i]
preturns <- ___ * ___ + (1 - ___) * ___
vsharpe[i] <- SharpeRatio.annualized(___)
}
# Plot weights and Sharpe ratio
plot(___, ___, xlab = "Weights", ylab= "Ann. Sharpe ratio")
abline(v = grid[vsharpe == max(vsharpe)], lty = 3)