Skip to content

Instantly share code, notes, and snippets.

View PyDataBlog's full-sized avatar
🥷
Code with purpose

Bernard Brenyah PyDataBlog

🥷
Code with purpose
View GitHub Profile
@PyDataBlog
PyDataBlog / S&P 500.ipynb
Created September 13, 2017 08:14
A tutorial for scraping information of all the S&P 500 listed companies
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@PyDataBlog
PyDataBlog / Returns on Single Assets.ipynb
Created September 17, 2017 17:46
A tutorial for calculating the rates of returns on single assets
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@PyDataBlog
PyDataBlog / Expected Returns of a Portfolio.ipynb
Created September 17, 2017 19:40
Tutorial for calculating the expected returns of a portfolio
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@PyDataBlog
PyDataBlog / Riskiness of Single Assets.ipynb
Created September 24, 2017 14:53
A tutorial for assessing the volatility of returns of single assets with Python
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@PyDataBlog
PyDataBlog / Visualising Volatility of Single Assets.py
Last active September 24, 2017 15:20
A script to help visualize the volatility of assets with a histogram
import pandas as pd
from pandas_datareader import data as web
import matplotlib.pyplot as plt
from datetime import datetime
start = datetime(2016,1,1)
end = datetime(2017,1,1)
assets = ['AAPL', 'FB', 'TSLA']
@PyDataBlog
PyDataBlog / Riskiness of a Portfolio.py
Last active September 28, 2017 12:01
A script for assessing the riskiness of a portfolio with Python
# 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:
@PyDataBlog
PyDataBlog / Assessing Riskiness of a Portfolio.ipynb
Created September 26, 2017 18:44
A tutorial for assessing the risk of a portfolio made up of five stocks
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import numpy as np
#store the variables in arrays
prob = np.array([0.25, 0.5, 0.25])
rate_1 = np.array([0.05, 0.075, 0.10])
rate_2 = np.array([0.2, 0.15, 0.1])
# expected return of each investment
expected_return1 = np.sum(prob * rate_1)
expected_return2 = np.sum(prob * rate_2)
# expected return of the equally weighted portfolio
weights = np.array([0.5, 0.5])
individual_returns = np.array([rate_1, rate_2])
portfolio_returns = np.dot(weights, individual_returns)
# covariance matrix given probabilities
cov_matrix = np.cov(rate_1, rate_2, ddof=0, aweights=prob)