Execute the following steps to create and evaluate the 1/n portfolio.
- Import the libraries:
import yfinance as yf
import numpy as np
import pandas as pd
import pyfolio as pf
- Set up the parameters:
RISKY_ASSETS = ['AAPL', 'IBM', 'MSFT', 'TWTR']
START_DATE = '2017-01-01'
END_DATE = '2018-12-31'
n_assets = len(RISKY_ASSETS)
- Download the stock prices from Yahoo Finance:
prices_df = yf.download(RISKY_ASSETS, start=START_DATE,
end=END_DATE, adjusted=True)
- Calculate individual asset returns:
returns = prices_df['Adj Close'].pct_change().dropna()
- Define the weights:
portfolio_weights = n_assets * [1 / n_assets]
- Calculate the portfolio returns:
portfolio_returns = pd.Series(np.dot(portfolio_weights, returns.T),
index=returns.index)
- Create the tear sheet (simple variant):
pf.create_simple_tear_sheet(portfolio_returns)
The preceding line of code generates the simple tear sheet, and the first table looks like this:
We describe the metrics presented in the first table of the tear sheet in the following section.