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
# variance and standard deviation of each investment
var1 = cov_matrix[0,0] # variance of any asset is the covariance of its returns WITH its returns
var2 = cov_matrix[1,1] # variance of any asset is the covariance of its returns WITH its returns
std1 = np.sqrt(var1) # std deviation is simply the square root of the variance
std2 = np.sqrt(var2) # std deviation is simply the square root of the variance
# correlation between Asset 1 & 2's returns
cov = cov_matrix[0,1]
corr = cov / (std1 * std2) # correlation of returns between 2 assets = covariance of their returns / (their std multiplied)
# just a function that returns a percentile for a given float
def percentage (number):
return str(round(number, 4) * 100) + '%'
# print the various variables for intepretation
print('Expected Return of Investment 1 = {}'.format(percentage(expected_return1)))
print('Expected Return of Investment 2 = {}'.format(percentage(expected_return2)))
print('Expected Return of Portfolio = {}'.format(percentage(portfolio_returns)))
print('Standard Deviation of Investment 1 = {}'.format(percentage(std1)))
print('Standard Deviation of Investment 1 = {}'.format(percentage(std2)))
# Import numpy
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)
import quandl
quandl.ApiConfig.api_key = 'INSERT YOU API KEY HERE'
# get the table for daily stock prices and,
# filter the table for selected tickers, columns within a time range
# set paginate to True because Quandl limits tables API to 10,000 rows per call
data = quandl.get_table('WIKI/PRICES', ticker = ['AAPL', 'MSFT', 'WMT'],
qopts = { 'columns': ['ticker', 'date', 'adj_close'] },
date = { 'gte': '2015-12-31', 'lte': '2016-12-31' },
ticker date adj_close
None
0 AAPL 2015-12-31 101.696810
1 AAPL 2016-01-04 101.783763
2 AAPL 2016-01-05 99.233131
3 AAPL 2016-01-06 97.291172
4 AAPL 2016-01-07 93.185040
# create a new dataframe with 'date' column as index
new = data.set_index('date')
# use pandas pivot function to sort adj_close by tickers
clean_data = new.pivot(columns='ticker')
# check the head of the output
clean_data.head()
adj_close
ticker AAPL MSFT WMT
date
2015-12-31 101.696810 53.096499 58.379766
2016-01-04 101.783763 52.445713 58.532144
2016-01-05 99.233131 52.684973 59.922592
2016-01-06 97.291172 51.727934 60.522580
2016-01-07 93.185040 49.928701 61.932075
# import needed libraries
import quandl
import pandas as pd
# add quandl API key for unrestricted
quandl.ApiConfig.api_key = 'INSERT YOU API KEY HERE'
# get the table for daily stock prices and,
# filter the table for selected tickers, columns within a time range
# set paginate to True because Quandl limits tables API to 10,000 rows per call
# import needed modules
import quandl
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# get adjusted closing prices of 5 selected companies with Quandl
quandl.ApiConfig.api_key = 'PASTE YOUR API KEY HERE'
selected = ['CNP', 'F', 'WMT', 'GE', 'TSLA']
data = quandl.get_table('WIKI/PRICES', ticker = selected,
# reorganise data pulled by setting date as index with
# columns of tickers and their corresponding adjusted prices
clean = data.set_index('date')
table = clean.pivot(columns='ticker')
table.head()