<jupyter_start><jupyter_text>Evaluating Returns<jupyter_code>def import_files(path_name, name):
    file = path_name+name
    df = pd.read_csv(file, delimiter=',')
    return df
cluster5 = pd.read_csv('1yr_cluster.csv').drop(columns=['Unnamed: 0'])
for i in range(len(cluster5)):
    cluster5.loc[i, 'Date'] = pd.to_datetime(cluster5.loc[i, 'Date'],
                                          format='%Y/%m/%d').date()
print(cluster5.shape)
cluster5
path_name='/Users/samypalaniappan/Desktop/Metis_Working_Directory/Projects/Project-4-Stock Superclusters/notebooks/data/raw_scraped/'
sp500 = import_files(path_name, '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]
for i in range(len(sp500)):
    sp500.loc[i, 'Date'] = pd.to_datetime(sp500.loc[i, 'Date'],
                                          format='%Y/%m/%d').date()
start = pd.to_datetime('2019-02-26', format='%Y/%m/%d').date()
end = pd.to_datetime('2020-02-28', format='%Y/%m/%d').date()
mask = (sp500.Date > start) & (sp500.Date < end)
sp500 = sp500[mask]
print(sp500.shape)
sp500.tail()

cluster6 = pd.concat([cluster5, sp500])
cluster6.shape
cluster_avg = pd.DataFrame(cluster5.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]
for i in range(len(cluster_avg)):
    cluster_avg.loc[i, 'Date'] = pd.to_datetime(cluster_avg.loc[i, 'Date'],
                                          format='%Y/%m/%d').date()
print(cluster_avg.shape)
cluster_avg.tail()
cluster_avg = pd.concat([cluster5, cluster_avg])
print(cluster_mean.shape)

cluster7 = pd.concat([cluster6, cluster_avg])
print(cluster7.shape)
cluster7.Cluster.unique()
data = pd.DataFrame(cluster_avg.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_avg.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), linewidth=2)
plt.title('ClusterPrice Fluctuation (Feb 2019 - 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), linewidth=2)
plt.title('Traded Volume', fontsize=24)
plt.xlabel('Date', fontsize=20)
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.legend(fontsize=12);
maxi = pd.to_datetime('2020-02-20', format='%Y/%m/%d').date()
mini = pd.to_datetime('2019-03-01', format='%Y/%m/%d').date()
mask = cluster7.Date == maxi
cluster_max = cluster7[mask]
cluster_max = cluster_max.rename(columns={'Volume':'Final_Vol','Price':'Final_Price'})
print(cluster_max.shape)
print(cluster_max.Cluster.unique())
mask = cluster7.Date == mini
cluster_min = cluster7[mask]
cluster_min = cluster_min.rename(columns={'Volume':'Init_Vol','Price':'Init_Price'})
print(cluster_min.Cluster.unique())
print(cluster_min.shape)
minmax = pd.merge(left=cluster_min, right=cluster_max, on='Name')
minmax.head()
minmax = pd.merge(left=cluster_min, right=cluster_max, on='Name', how='outer')
minmax = minmax.dropna()
print(minmax.Cluster_x.unique())
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=(-50, 60), 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/3

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 = cluster5.Cluster == 3.0
cluster3 = cluster5[mask]
cluster3 =cluster3.groupby('Name').agg('mean').reset_index()
dct =[]
for i in range(len(cluster3)):
    x = {'Name':cluster3.Name[i], 'Price':int(cluster3.Price[i])}
    dct.append(x)
    
df = pd.DataFrame(dct)
df.to_csv('1yr_result.csv')
for i in range(len(df)):
    print(f'{df.Name[i]}; {df.Price[i]}')<jupyter_output>AAPL; 229
ADBE; 295
AGN; 162
AIZ; 115
AKAM; 84
ALLE; 107
AMAT; 49
AMD; 34
AMT; 212
ANSS; 219
ARNC; 25
ATVI; 51
BLL; 67
CAG; 28
CBOE; 111
CCI; 134
CHTR; 423
CMG; 773
COST; 275
CPB; 42
CTAS; 247
DG; 142
DGX; 100
DHI; 48
DVA; 61
EFX; 135
EQIX; 525
ES; 78
ETR; 108
EW; 209
FBHS; 56
FIS; 128
FISV; 101
GPN; 164
HSY; 139
INFO; 65
KLAC; 142
KMX; 85
KSU; 133
LEN; 54
LRCX; 229
MA; 272
MAA; 122
MCO; 211
MSFT; 139
MU; 45
NEE; 217
NEM; 37
NVDA; 190
PCG; 14
PHM; 35
QCOM; 76
QRVO; 83
RMD; 131
SBAC; 232
SIG; 19
SNPS; 131
SO; 57
SPGI; 245
SRCL; 53
SWKS; 89
TDG; 486
TGT; 97
TIF; 106
WDC; 53
WU; 22
{'fraction_non_alphanumeric': 0.1715437444013138, 'fraction_numerical': 0.060465810689758134, 'mean_word_length': 5.746223564954683, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '190379', 'license': 'FTL', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2602, 'n_words': 473, 'path': 'Code/Kmeans_StocksClusters_1Yr.ipynb', 'repo_name': 'SamyPal/Project4-UnsupervisedLearning-NLP', 'size': '6698'}