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
Cet exercice fait partie du cours
Introduction to Portfolio Risk Management in Python
Instructions
- Finish defining the numpy array of model
portfolio_weightswith the values according to the table above. - Use the
.mul()method to multiply theportfolio_weightsacross the rows ofStockReturnsto get weighted stock returns. - Then use the
.sum()method across the rows on theWeightedReturnsobject to calculate the portfolio returns. - Finally, review the plot of cumulative returns over time.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de 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()