Advertisement
Guest User

script

a guest
Mar 3rd, 2020
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. from rf import return_portfolios, optimal_portfolio
  4. import numpy as np
  5.  
  6. # 1. Load the stock data
  7. stock_data = pd.read_csv('stock_data_weak.csv')
  8. print(stock_data)
  9. # 2. Find the quarterly return for each period
  10. selected = list(stock_data.columns[1:])
  11. returns_quarterly = stock_data[selected].pct_change()
  12. # print(returns_quarterly)
  13.  
  14. # 3. Find the expected returns
  15. expected_returns = returns_quarterly.mean()
  16. # print(expected_returns)
  17.  
  18. # 4. Find the covariance
  19. cov_quarterly = returns_quarterly.cov()
  20. # print(cov_quarterly)
  21.  
  22. # 5. Find a set of random portfolios
  23. random_portfolios = return_portfolios(expected_returns, cov_quarterly)
  24.  
  25. # 6. Plot the set of random portfolios
  26. random_portfolios.plot.scatter(x='Volatility', y='Returns', fontsize=12)
  27.  
  28. # 7. Calculate the set of portfolios on the EF
  29. weights, returns, risks = optimal_portfolio(returns_quarterly[1:])
  30.  
  31. # 8. Plot the set of portfolios on the EF
  32. plt.ylabel('Expected Returns',fontsize=14)
  33. plt.xlabel('Volatility (Std. Deviation)',fontsize=14)
  34. plt.title('Efficient Frontier', fontsize=24)
  35.  
  36. # Compare the set of portfolios on the EF to the
  37. try:
  38.   single_asset_std=np.sqrt(np.diagonal(cov_quarterly))
  39.   plt.scatter(single_asset_std,expected_returns,marker='X',color='red',s=200)
  40. except:
  41.   pass
  42. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement