Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. # import python's number cruncher
  2. from pandas_datareader import data as web
  3. import pandas as pd
  4. import numpy as np
  5.  
  6. assets = ['AAPL', 'GM', 'GE', 'FB', 'WMT']
  7.  
  8. df = pd.DataFrame()
  9.  
  10. for stock in assets:
  11. df[stock] = web.DataReader(stock, data_source='yahoo',
  12. start='2015-1-1' , end='2017-1-1')['Adj Close']
  13.  
  14. d_returns = df.pct_change()
  15.  
  16. cov_matrix_d = d_returns.cov()
  17. cov_matrix_a = cov_matrix_d * 250
  18.  
  19. weights = np.array([0.2, 0.2, 0.2, 0.2, 0.2]) # assign equal weights
  20.  
  21. # calculate the variance and risk of the portfolo
  22. port_variance = np.dot(weights.T, np.dot(cov_matrix_a, weights))
  23. port_volatility = np.sqrt(np.dot(weights.T, np.dot(cov_matrix_a, weights)))
  24.  
  25. percent_var = str(round(port_variance, 4) * 100) + '%'
  26. percent_vols = str(round(port_volatility, 4) * 100) + '%'
  27.  
  28. print('Variance of Portfolio is {}, Portfolio Risk is {}'
  29. .format(percent_var, percent_vols))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement