-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathensemble.py
94 lines (63 loc) · 2.95 KB
/
ensemble.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def majority_voting(df):
local_df = df.copy()
x=local_df.loc[:,'iteration0':'iteration24']
local_df['ensemble']=x.mode(axis=1).iloc[:, 0]
local_df = local_df.drop(local_df.columns.difference(['ensemble']), axis=1)
return local_df
def ensemble(type, ensembleFolderName):
dollSum=0
rewSum=0
posSum=0
negSum=0
covSum=0
numSum=0
values=[]
columns = ["Experiment","#Wins","#Losses","Dollars","Coverage","Accuracy"]
sp500=pd.read_csv("./dataset/jpm/test_data.csv",index_col='date_time')
df=pd.read_csv("./Output/ensemble/"+ensembleFolderName+"/ensemble_"+type+".csv",index_col='date_time')
df=majority_voting(df)
num=0
rew=0
pos=0
neg=0
doll=0
cov=0
#Lets iterate through each date and decision
for date, i in df.iterrows():
#If the date in the predictions is in the index of sp500 (which is also a date)
if date in sp500.index:
num+=1
#If the output is 1 (long)
if (i['ensemble']==1):
#If the close - open is positive at that day, we have earning money. Positives are equal to 1. Otherwise, no incrementation
pos+= 1 if (float(sp500.at[date,'delta_next_day'])) > 0 else 0
#If close - open is negative at that day, we are losing money. Negatives are equal to 1. Otherwise, no incrementation
neg+= 1 if (float(sp500.at[date,'delta_next_day'])) < 0 else 0
#Lets calculate the reward (positive or negative)
rew+=float(sp500.at[date,'delta_next_day'])
#In dollars, we just multiply by the sp500 points by the differences
doll+=float(sp500.at[date,'delta_next_day'])
#There is coverage (of course)
cov+=1
#The same stuff happens for short.
elif (i['ensemble']==2):
pos+= 1 if float(sp500.at[date,'delta_next_day']) < 0 else 0
neg+= 1 if float(sp500.at[date,'delta_next_day']) > 0 else 0
rew+=-float(sp500.at[date,'delta_next_day'])
cov+=1
doll+=-float(sp500.at[date,'delta_next_day'])
values.append([str(1),str(round(pos,2)),str(round(neg,2)),str(round(doll,2)),str(round(cov/num,2)),(str(round(pos/cov,2)) if (cov>0) else "")])
#Now lets sum walk by walk
dollSum+=doll
rewSum+=rew
posSum+=pos
negSum+=neg
covSum+=cov
numSum+=num
#Now lets summarize everything showing the sum of values
values.append(["sum",str(round(posSum,2)),str(round(negSum,2)),str(round(dollSum,2)),str(round(covSum/numSum,2)),(str(round(posSum/covSum,2)) if (covSum>0) else "")])
return values,columns
################