Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Chapter 2 : Portfolio Investing
- portfolio_weights = np.array([0.12, 0.15, 0.08, 0.05, 0.09, 0.10, 0.11, 0.14, 0.16])
- # Calculate the weighted stock returns
- WeightedReturns = StockReturns.mul(portfolio_weights, axis=1)
- # Calculate the portfolio returns
- StockReturns['Portfolio'] = WeightedReturns.sum(axis=1)
- # Plot the cumulative portfolio returns over time
- CumulativeReturns = ((1+StockReturns["Portfolio"]).cumprod()-1)
- CumulativeReturns.plot()
- plt.show()
- numstocks = 9
- # Create an array of equal weights across all assets
- portfolio_weights_ew = np.repeat(1/numstocks, numstocks)
- # Calculate the equally-weighted portfolio returns
- StockReturns['Portfolio_EW'] = StockReturns.iloc[:, 0:numstocks].mul(portfolio_weights_ew, axis=1).sum(axis=1)
- cumulative_returns_plot(['Portfolio', 'Portfolio_EW'])
- # Create an array of market capitalizations (in billions)
- market_capitalizations = np.array([601.51, 469.25, 349.5, 310.48, 299.77, 356.94, 268.88, 331.57, 246.09])
- # Calculate the market cap weights
- mcap_weights = market_capitalizations/sum(market_capitalizations)
- # Calculate the market cap weighted portfolio returns
- StockReturns['Portfolio_MCap'] = StockReturns.iloc[:, 0:9].mul(mcap_weights, axis=1).sum(axis=1)
- cumulative_returns_plot(['Portfolio', 'Portfolio_EW', 'Portfolio_MCap'])
- correlation_matrix = StockReturns.corr()
- # Import seaborn as sns
- import seaborn as sns
- # Create a heatmap
- sns.heatmap(correlation_matrix,
- annot=True,
- cmap="YlGnBu",
- linewidths=0.3,
- annot_kws={"size": 8})
- # Plot aesthetics
- plt.xticks(rotation=90)
- plt.yticks(rotation=0)
- plt.show()
- # Calculate the covariance matrix
- cov_mat = StockReturns.cov()
- # Annualize the co-variance matrix
- cov_mat_annual = cov_mat*252
- # Print the annualized co-variance matrix
- print(cov_mat_annual)
- # Calculate the portfolio standard deviation
- portfolio_volatility = np.sqrt(np.dot(portfolio_weights.T, np.dot(cov_mat_annual, portfolio_weights)))
- print(portfolio_volatility)
- # Risk free rate
- risk_free = 0
- # Calculate the Sharpe Ratio for each asset
- RandomPortfolios['Sharpe'] = (RandomPortfolios["Returns"]-risk_free)/RandomPortfolios["Volatility"]
- # Print the range of Sharpe ratios
- print(RandomPortfolios['Sharpe'].describe()[['min', 'max']])
- # Sort the portfolios by Sharpe ratio
- sorted_portfolios = RandomPortfolios.sort_values(by=['Sharpe'], ascending=True)
- # Extract the corresponding weights
- MSR_weights = sorted_portfolios.iloc[0, 0:numstocks]
- # Cast the MSR weights as a numpy array
- MSR_weights_array = np.array(MSR_weights)
- # Calculate the MSR portfolio returns
- StockReturns['Portfolio_MSR'] = StockReturns.iloc[:, 0:numstocks].mul(MSR_weights_array, axis=1).sum(axis=1)
- # Plot the cumulative returns
- cumulative_returns_plot(['Portfolio_EW', 'Portfolio_MCap', 'Portfolio_MSR'])
Add Comment
Please, Sign In to add comment