Calculating portfolio returns
In order to build and backtest a portfolio, you have to be comfortable working with the returns of multiple assets in a single object.
In this exercise, you will be using a pandas
DataFrame
object, already stored as the variable StockReturns
, to hold the returns of multiple assets and to calculate the returns of a model portfolio.
The model portfolio is constructed with pre-defined weights for some of the largest companies in the world just before January 2017:
Company Name | Ticker | Portfolio Weight |
---|---|---|
Apple | AAPL | 12% |
Microsoft | MSFT | 15% |
Exxon Mobil | XOM | 8% |
Johnson & Johnson | JNJ | 5% |
JP Morgan | JPM | 9% |
Amazon | AMZN | 10% |
General Electric | GE | 11% |
FB | 14% | |
AT&T | T | 16% |
Note that the portfolio weights should sum to 100% in most cases
This is a part of the course
“Introduction to Portfolio Risk Management in Python”
Exercise instructions
- Finish defining the numpy array of model
portfolio_weights
with the values according to the table above. - Use the
.mul()
method to multiply theportfolio_weights
across the rows ofStockReturns
to get weighted stock returns. - Then use the
.sum()
method across the rows on theWeightedReturns
object to calculate the portfolio returns. - Finally, review the plot of cumulative returns over time.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Finish defining the portfolio weights as a numpy array
portfolio_weights = np.array([0.12, 0.15, 0.08, 0.05, 0.09, 0.10, 0.11, ____, ____])
# Calculate the weighted stock returns
WeightedReturns = StockReturns.____(portfolio_weights, axis=____)
# Calculate the portfolio returns
StockReturns['Portfolio'] = WeightedReturns.____(axis=____)
# Plot the cumulative portfolio returns over time
CumulativeReturns = ((1+StockReturns["Portfolio"]).cumprod()-1)
CumulativeReturns.plot()
plt.show()