Last active
September 28, 2017 12:01
-
-
Save PyDataBlog/3dfbaf20f81cf6b420facb3dbe1487e4 to your computer and use it in GitHub Desktop.
A script for assessing the riskiness of a portfolio with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # import python's number crunchers | |
| from pandas_datareader import data as web | |
| import pandas as pd | |
| import numpy as np | |
| assets = ['AAPL', 'GM', 'GE', 'FB', 'WMT'] | |
| df = pd.DataFrame() | |
| for stock in assets: | |
| df[stock] = web.DataReader(stock, data_source='yahoo', | |
| start='2015-1-1' , end='2017-1-1')['Adj Close'] | |
| d_returns = df.pct_change() | |
| cov_matrix_d = d_returns.cov() | |
| cov_matrix_a = cov_matrix_d * 250 | |
| weights = np.array([0.2, 0.2, 0.2, 0.2, 0.2]) # assign equal weights | |
| # calculate the variance and risk of the portfolo | |
| port_variance = np.dot(weights.T, np.dot(cov_matrix_a, weights)) | |
| port_volatility = np.sqrt(np.dot(weights.T, np.dot(cov_matrix_a, weights))) | |
| percent_var = str(round(port_variance, 4) * 100) + '%' | |
| percent_vols = str(round(port_volatility, 4) * 100) + '%' | |
| print('Variance of Portfolio is {}, Portfolio Risk is {}' | |
| .format(percent_var, percent_vols)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment