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
| library(tidyverse) | |
| #### clear workspace #### | |
| rm(list=ls()) | |
| #### read in the html file and clean up the data #### | |
| msgs <- readLines("~/Documents/gosschat.html") | |
| msgs <- gsub(" ", msgs, replacement="__") | |
| msgs <- gsub("\"", msgs, replacement="##") | |
| msgs <- gsub("\'", msgs, replacement="##") |
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
| library(tidyverse) | |
| month_vec <- c(rep(1, 31), | |
| rep(2, 28), | |
| rep(3, 31), | |
| rep(4, 30), | |
| rep(5, 31), | |
| rep(6, 30), | |
| rep(7, 31), | |
| rep(8, 31), |
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
| library(tidyverse) | |
| library(rvest) | |
| library(leaflet) | |
| library(mapsapi) | |
| trivia <- "http://www.chicagotribune.com/redeye/culture/ct-redeye-do-trivia-chicago-bars-20180320-story.html" | |
| bars <- read_html(trivia) %>% | |
| html_nodes("a strong") %>% | |
| html_text() |
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
| # imports | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from sklearn.linear_model import LinearRegression | |
| from sklearn.metrics import mean_squared_error, r2_score | |
| # generate random data-set | |
| np.random.seed(0) | |
| x = np.random.rand(100, 1) | |
| y = 2 + 3 * x + np.random.rand(100, 1) |
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
| # imports | |
| import numpy as np | |
| class LinearRegressionUsingGD: | |
| """Linear Regression Using Gradient Descent. | |
| Parameters | |
| ---------- | |
| eta : float |
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
| np.random.seed(42) | |
| num_ports = 6000 | |
| all_weights = np.zeros((num_ports, len(stocks.columns))) | |
| ret_arr = np.zeros(num_ports) | |
| vol_arr = np.zeros(num_ports) | |
| sharpe_arr = np.zeros(num_ports) | |
| for x in range(num_ports): | |
| # Weights | |
| weights = np.array(np.random.random(4)) |
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
| plt.figure(figsize=(12,8)) | |
| plt.scatter(vol_arr, ret_arr, c=sharpe_arr, cmap='viridis') | |
| plt.colorbar(label='Sharpe Ratio') | |
| plt.xlabel('Volatility') | |
| plt.ylabel('Return') | |
| plt.scatter(max_sr_vol, max_sr_ret,c='red', s=50) # red dot | |
| plt.show() |
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
| def get_ret_vol_sr(weights): | |
| weights = np.array(weights) | |
| ret = np.sum(log_ret.mean() * weights) * 252 | |
| vol = np.sqrt(np.dot(weights.T, np.dot(log_ret.cov()*252, weights))) | |
| sr = ret/vol | |
| return np.array([ret, vol, sr]) | |
| def neg_sharpe(weights): | |
| # the number 2 is the sharpe ratio index from the get_ret_vol_sr | |
| return get_ret_vol_sr(weights)[2] * -1 |
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
| frontier_x = [] | |
| for possible_return in frontier_y: | |
| cons = ({'type':'eq', 'fun':check_sum}, | |
| {'type':'eq', 'fun': lambda w: get_ret_vol_sr(w)[0] - possible_return}) | |
| result = minimize(minimize_volatility,init_guess,method='SLSQP', bounds=bounds, constraints=cons) | |
| frontier_x.append(result['fun']) |
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
| data = [] | |
| for event in event_types: | |
| event_data = dict( | |
| lat = df.loc[df['EVENT_TYPE'] == event,'BEGIN_LAT'], | |
| lon = df.loc[df['EVENT_TYPE'] == event,'BEGIN_LON'], | |
| name = event, | |
| marker = dict(size = 8, opacity = 0.5), | |
| type = 'scattermapbox' | |
| ) | |
| data.append(event_data) |