Guest User

Untitled

a guest
Jul 16th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. # Chapter 2 : Portfolio Investing
  2.  
  3. portfolio_weights = np.array([0.12, 0.15, 0.08, 0.05, 0.09, 0.10, 0.11, 0.14, 0.16])
  4.  
  5. # Calculate the weighted stock returns
  6. WeightedReturns = StockReturns.mul(portfolio_weights, axis=1)
  7.  
  8. # Calculate the portfolio returns
  9. StockReturns['Portfolio'] = WeightedReturns.sum(axis=1)
  10.  
  11. # Plot the cumulative portfolio returns over time
  12. CumulativeReturns = ((1+StockReturns["Portfolio"]).cumprod()-1)
  13. CumulativeReturns.plot()
  14. plt.show()
  15.  
  16. numstocks = 9
  17.  
  18. # Create an array of equal weights across all assets
  19. portfolio_weights_ew = np.repeat(1/numstocks, numstocks)
  20.  
  21. # Calculate the equally-weighted portfolio returns
  22. StockReturns['Portfolio_EW'] = StockReturns.iloc[:, 0:numstocks].mul(portfolio_weights_ew, axis=1).sum(axis=1)
  23. cumulative_returns_plot(['Portfolio', 'Portfolio_EW'])
  24.  
  25. # Create an array of market capitalizations (in billions)
  26. market_capitalizations = np.array([601.51, 469.25, 349.5, 310.48, 299.77, 356.94, 268.88, 331.57, 246.09])
  27.  
  28. # Calculate the market cap weights
  29. mcap_weights = market_capitalizations/sum(market_capitalizations)
  30.  
  31. # Calculate the market cap weighted portfolio returns
  32. StockReturns['Portfolio_MCap'] = StockReturns.iloc[:, 0:9].mul(mcap_weights, axis=1).sum(axis=1)
  33. cumulative_returns_plot(['Portfolio', 'Portfolio_EW', 'Portfolio_MCap'])
  34.  
  35. correlation_matrix = StockReturns.corr()
  36.  
  37. # Import seaborn as sns
  38. import seaborn as sns
  39.  
  40. # Create a heatmap
  41. sns.heatmap(correlation_matrix,
  42. annot=True,
  43. cmap="YlGnBu",
  44. linewidths=0.3,
  45. annot_kws={"size": 8})
  46.  
  47. # Plot aesthetics
  48. plt.xticks(rotation=90)
  49. plt.yticks(rotation=0)
  50. plt.show()
  51.  
  52. # Calculate the covariance matrix
  53. cov_mat = StockReturns.cov()
  54.  
  55. # Annualize the co-variance matrix
  56. cov_mat_annual = cov_mat*252
  57.  
  58. # Print the annualized co-variance matrix
  59. print(cov_mat_annual)
  60.  
  61. # Calculate the portfolio standard deviation
  62. portfolio_volatility = np.sqrt(np.dot(portfolio_weights.T, np.dot(cov_mat_annual, portfolio_weights)))
  63. print(portfolio_volatility)
  64.  
  65. # Risk free rate
  66. risk_free = 0
  67.  
  68. # Calculate the Sharpe Ratio for each asset
  69. RandomPortfolios['Sharpe'] = (RandomPortfolios["Returns"]-risk_free)/RandomPortfolios["Volatility"]
  70.  
  71. # Print the range of Sharpe ratios
  72. print(RandomPortfolios['Sharpe'].describe()[['min', 'max']])
  73.  
  74. # Sort the portfolios by Sharpe ratio
  75. sorted_portfolios = RandomPortfolios.sort_values(by=['Sharpe'], ascending=True)
  76.  
  77. # Extract the corresponding weights
  78. MSR_weights = sorted_portfolios.iloc[0, 0:numstocks]
  79.  
  80. # Cast the MSR weights as a numpy array
  81. MSR_weights_array = np.array(MSR_weights)
  82.  
  83. # Calculate the MSR portfolio returns
  84. StockReturns['Portfolio_MSR'] = StockReturns.iloc[:, 0:numstocks].mul(MSR_weights_array, axis=1).sum(axis=1)
  85.  
  86. # Plot the cumulative returns
  87. cumulative_returns_plot(['Portfolio_EW', 'Portfolio_MCap', 'Portfolio_MSR'])
Add Comment
Please, Sign In to add comment