<jupyter_start><jupyter_text>Return Calculations<jupyter_code>def import_files(path_name, name):
    file = path_name+name
    df = pd.read_csv(file, delimiter=',')
    return df
cluster_5 = pd.read_csv('5yr_cluster.csv', index_col=None).drop(columns=['Unnamed: 0', 'index'])
print(cluster_5.shape)
cluster_5.head()
sp500 = import_files(base_path, '/raw_scraped/sp500.csv')
sp500 = sp500.rename(columns={'Adj Close':'Price'})
sp500['Name']='SP500'
sp500['Cluster']=6.0
sp500 = sp500.drop(columns=['Open', 'Close', 'High', 'Low'])
cols = ['Cluster', 'Name', 'Date', 'Volume', 'Price']
sp500 = sp500[cols]
sp500.tail()
cluster_6 = pd.concat([cluster_5, sp500])
cluster_6.shape
cluster_avg = pd.DataFrame(cluster.groupby(['Date']).agg('mean')).reset_index()
cluster_avg['Cluster']=7.0
cluster_avg['Name']='AVG'
cols = ['Cluster', 'Name', 'Date', 'Volume', 'Price']
cluster_avg = cluster_avg[cols]
print(cluster_avg.shape)
cluster_avg.head()
cluster_mean = pd.concat([cluster_5, cluster_avg])
print(cluster_mean.shape)

cluster_7 = pd.concat([cluster_6, cluster_avg])
print(cluster_7.shape)
cluster_7.head()
data = pd.DataFrame(cluster_mean.groupby(['Cluster', 'Date']).agg('mean'))
unstacked_price = data.unstack(level=0).drop(columns=['Volume'])
unstacked_volume = data.unstack(level=0).drop(columns=['Price'])
data = pd.DataFrame(cluster_mean.groupby(['Cluster', 'Date']).agg('mean'))
unstacked_price = data.unstack(level=0).drop(columns=['Volume'])
unstacked_volume = data.unstack(level=0).drop(columns=['Price'])
unstacked_price 
unstacked_price.plot(legend=True, figsize=(15, 7))
plt.title('ClusterPrice Fluctuation (Feb 2015 - Feb 2020)', fontsize=24)
plt.xlabel('Date', fontsize=20)
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.legend(fontsize=12)

unstacked_volume.plot(legend=True, figsize=(15, 7))
plt.title('Traded Volume', fontsize=24)
plt.xlabel('Date', fontsize=20)
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.legend(fontsize=12)
mask = (cluster_7.Name=='SP500') & (cluster_7.Date=='2015-02-18')
cluster_7[mask]
mask = cluster_7.Date == '2020-02-18'
cluster_max = cluster_7[mask]
cluster_max = cluster_max.rename(columns={'Volume':'Final_Vol','Price':'Final_Price'})
print(cluster_max.shape)
mask = cluster_7.Date == '2015-02-18'
cluster_min = cluster_7[mask]
cluster_min = cluster_min.rename(columns={'Volume':'Init_Vol','Price':'Init_Price'})
print(cluster_min.shape)
minmax = pd.merge(left=cluster_min, right=cluster_max, on='Name')
minmax.head()
gb = minmax.groupby('Cluster_x')
cluster_earnings = gb.agg({'Name':'count', 'Init_Price':'sum', 'Final_Price':'sum'})
cluster_earnings['Return'] = (cluster_earnings.Final_Price - cluster_earnings.Init_Price)\
/cluster_earnings.Init_Price*100
cluster_earnings = cluster_earnings.reset_index().rename(columns={'Cluster_x':'Cluster'})
# cluster_earnings['Cluster'] = [str(x) for x in cluster_earnings.index]
cluster_earnings['Cluster']=['1', '2', '3','4','5','S&P500','AVG']
# cluster_earnings.loc[6, 'Cluster']='AVG'
cluster_earnings.loc[6, 'Name']=456
cluster_earnings.head(7)
# from bokeh.charts import Bar, output_notebook, show, vplot
from bokeh.palettes import Blues5
from bokeh.models import Legend
from bokeh.plotting import figure

x = cluster_earnings.Cluster
y = cluster_earnings.Return
z = cluster_earnings.Name

plot = figure(x_range=x, y_range=(-100, 250), plot_width=400, plot_height=400, 
           x_axis_label='Cluster', y_axis_label='Average Returns (%)')


plot.xaxis.major_label_orientation = np.pi/4  
plot.xaxis.axis_label_text_font_size = "16pt"
plot.xaxis.major_label_text_font_size = "16pt"
plot.xaxis.axis_label_text_font = "times"
plot.xaxis.axis_label_text_color = "black"


plot.yaxis.axis_label_text_font_size = "16pt"
plot.yaxis.major_label_text_font_size = "16pt"
plot.yaxis.axis_label_text_font = "times"
plot.yaxis.axis_label_text_color = "black"
plot.vbar(x=x, width=0.5, bottom=0,
       top=y, color="blue")
show(plot)
x = cluster_earnings.Cluster
y = cluster_earnings.Return
z = cluster_earnings.Name

plot = figure(x_range=x, y_range=(0, 500), plot_width=400, plot_height=400, 
           x_axis_label='Cluster', y_axis_label='No. of stocks in the cluster')

plot.xaxis.major_label_orientation = np.pi/4  
plot.xaxis.axis_label_text_font_size = "16pt"
plot.xaxis.major_label_text_font_size = "16pt"
plot.xaxis.axis_label_text_font = "times"
plot.xaxis.axis_label_text_color = "black"


plot.yaxis.axis_label_text_font_size = "16pt"
plot.yaxis.major_label_text_font_size = "16pt"
plot.yaxis.axis_label_text_font = "times"
plot.yaxis.axis_label_text_color = "black"
plot.vbar(x=x, width=0.5, bottom=0,
       top=z, color="orange")

show(plot)
mask = cluster_5.Cluster == 4.0
cluster4 = cluster_5[mask]
cluster4 =cluster4.groupby('Name').agg('mean').reset_index()
dct =[]
for i in range(len(cluster4)):
    x = {'Name':cluster4.Name[i], 'Price':int(cluster4.Price[i])}
    dct.append(x)
    
df = pd.DataFrame(dct)
df.to_csv('5yr_result.csv')
for i in range(len(df)):
    print(f'{df.Name[i]}; {df.Price[i]}')<jupyter_output>AAPL; 154
ADBE; 174
ADSK; 105
ALGN; 174
AMAT; 36
AMD; 14
AMZN; 1155
ANSS; 138
ATVI; 48
AVGO; 201
BA; 233
BBY; 51
CDNS; 38
CHTR; 300
CRM; 107
CSX; 48
CTAS; 148
DHI; 36
DHR; 87
EQIX; 380
EW; 128
FB; 146
GOOG; 937
GOOGL; 950
GPN; 99
HUM; 236
IDXX; 160
INTU; 160
ISRG; 350
KLAC; 91
LRCX; 141
MA; 156
MCHP; 70
MCO; 138
MSFT; 81
NFLX; 208
NVDA; 131
PGR; 46
PYPL; 67
RMD; 84
SNPS; 80
SPGI; 156
TDG; 275
TXN; 81
UNH; 185
V; 110
VRSN; 118
XLNX; 66
{'fraction_non_alphanumeric': 0.16657534246575342, 'fraction_numerical': 0.06027397260273973, 'mean_word_length': 6.913294797687861, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '78077', 'license': 'FTL', 'n_tokens_mistral': 2415, 'n_tokens_neox': 2160, 'n_words': 380, 'path': 'Code/Kmeans_StockClusters_5yr.ipynb', 'repo_name': 'SamyPal/Project4-UnsupervisedLearning-NLP', 'size': '5475'}