/
outer.py
262 lines (235 loc) · 9.37 KB
/
outer.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import myownq
import random
import util
import math
import geneticAlgorithm
#list of all fundamentals used including price
allKeys = ['"ACCOCI"', '"ASSETS"', '"ASSETSC"', '"ASSETSNC"', '"BVPS"', '"CAPEX"', '"CASHNEQ"', '"COR"', '"CURRENTRATIO"', '"DE"', '"DEBT"', '"DEPAMOR"', '"DILUTIONRATIO"', '"DPS"', '"EBIT"', '"EBITDA"', '"EBT"', '"EPS"', '"EPSDIL"', '"EQUITY"', '"FCF"', '"FCFPS"', '"GP"', '"INTANGIBLES"', '"INTEXP"', '"INVENTORY"', '"LIABILITIES"', '"LIABILITIESC"', '"LIABILITIESNC"', '"NCF"', '"NCFCOMMON"', '"NCFDEBT"', '"NCFDIV"', '"NCFF"', '"NCFI"', '"NCFO"', '"NCFX"', '"NETINC"', '"NETINCCMN"', '"NETINCDIS"', '"PAYABLES"', '"PB"', '"PREFDIVIS"', '"RECEIVABLES"', '"RETEARN"', '"REVENUE"', '"RND"', '"SGNA"', '"SHARESWA"', '"SHARESWADIL"', '"TANGIBLES"', '"TAXEXP"', '"TBVPS"', '"WORKINGCAPITAL"']
#list with price removed
keys = ['"ACCOCI"', '"ASSETS"', '"ASSETSC"', '"ASSETSNC"', '"BVPS"', '"CAPEX"', '"CASHNEQ"', '"COR"', '"CURRENTRATIO"', '"DE"', '"DEBT"', '"DEPAMOR"', '"DILUTIONRATIO"', '"DPS"', '"EBIT"', '"EBITDA"', '"EBT"', '"EPS"', '"EPSDIL"', '"EQUITY"', '"FCF"', '"FCFPS"', '"GP"', '"INTANGIBLES"', '"INTEXP"', '"INVENTORY"', '"LIABILITIES"', '"LIABILITIESC"', '"LIABILITIESNC"', '"NCF"', '"NCFCOMMON"', '"NCFDEBT"', '"NCFDIV"', '"NCFF"', '"NCFI"', '"NCFO"', '"NCFX"', '"NETINC"', '"NETINCCMN"', '"NETINCDIS"', '"PAYABLES"', '"PB"', '"PREFDIVIS"', '"RECEIVABLES"', '"RETEARN"', '"REVENUE"', '"RND"', '"SGNA"', '"SHARESWA"', '"SHARESWADIL"', '"TANGIBLES"', '"TAXEXP"', '"TBVPS"', '"WORKINGCAPITAL"']
gradientEpsilon = 0.01
popN = 25 # n number of chromos per population
genesPerCh = 54
max_iterations = 5000
chromos = geneticAlgorithm.generatePop(popN) #generate new population of random chromosomes
iterations = 0
#stochastic descent with random restarts
def runStochasticDescent():
initialized = False
#randomly select 10 fundamentals to use
selectedKeys = random.sample(keys, 10)
#add in price
selectedKeys += ['"PRICE"']
remainingKeys = [key for key in keys if key not in selectedKeys]
hashVal = hash(tuple(selectedKeys))
#store the scores for this set of fundamentals
fScoreCorrect = dict()
fScoreRewards = dict()
previousState = dict()
visited = []
maxState = dict()
#loop through 100,000 iterations
for x in range(1,100000):
#random restarts
if (x%10000 == 0):
selectedKeys = random.sample(keys, 10)
selectedKeys += ['"PRICE"']
remainingKeys = [key for key in keys if key not in selectedKeys]
hashVal = hash(tuple(selectedKeys))
previousState = dict()
#print ("RESTART")
#get the score for this set of fundamentals by running the inner loop
result = myownq.runInnerLoop(selectedKeys)
fScoreRewards[hashVal] = result[0]
fScoreCorrect[hashVal] = result[1]
#save the inner loop agent that ran the inner q-learning for this set
agent = result[2]
visited.append(hashVal)
move = False
#move if its the first step
if (len(previousState.keys()) == 0):
move = True
#move if the new state has a better score
elif (fScoreRewards[previousState["hash"]] < fScoreRewards[hashVal]):
move = True
#move randomly with a chance equal to gradientEpsilon
else:
move = util.flipCoin(gradientEpsilon)
if (move):
#update values for previous state
previousState["hash"] = hashVal
previousState["selected"] = selectedKeys
previousState["remaining"] = remainingKeys
#print (fScoreRewards[hashVal])
#print (fScoreCorrect[hashVal])
#update max state if necessary
if (len(maxState.keys()) == 0 or fScoreRewards[hashVal] > maxState["score"]):
maxState["hash"] = hashVal
maxState["selected"] = selectedKeys
maxState["remaining"] = remainingKeys
maxState["score"] = fScoreRewards[hashVal]
maxState["correct"] = fScoreCorrect[hashVal]
maxState["agent"] = agent
#avoid repeated states
repeatedState = True
numRepeatedStates = 0
while (repeatedState):
numRepeatedStates += 1
hashVal = previousState["hash"]
selectedKeys = previousState["selected"]
remainingKeys = previousState["remaining"]
#either randomly remove a fundamental, or add one
#don't want too few fundamentals, so only remove if there are more than 4
#also don't want too many, so limit it at around 45
if ((len(selectedKeys) > 4 and util.flipCoin(0.5)) or len(selectedKeys) > 45):
randomKey = random.sample(selectedKeys, 1)[0]
while (randomKey == '"PRICE"'):
randomKey = random.sample(selectedKeys, 1)[0]
selectedKeys.remove(randomKey)
remainingKeys.append(randomKey)
else:
randomKey = random.sample(remainingKeys, 1)[0]
selectedKeys.append(randomKey)
remainingKeys.remove(randomKey)
hashVal = hash(tuple(selectedKeys))
if (hashVal not in visited):
repeatedState = False
#we are probably stuck in an infinite loop if this occurs, so we need to randomly sample
if (numRepeatedStates >=1000):
selectedKeys = random.sample(keys, 10)
selectedKeys += ['"PRICE"']
remainingKeys = [key for key in keys if key not in selectedKeys]
hashVal = hash(tuple(selectedKeys))
previousState = dict()
#print ("FINAL SOLUTION")
print (maxState)
#random anneal probablity, dependent on time
def annealSchedule(delta, time):
return math.exp(float(delta) / float(100001 - time)**0.4)
#save the agent returned from the max state
simulatedAgent = myownq.qAgent()
#Simulated Annealing
def runSimulatedAnnealing():
initialized = False
#no price in these keys
selectedKeys = random.sample(keys, 10)
selectedKeys += ['"PRICE"']
remainingKeys = [key for key in keys if key not in selectedKeys]
hashVal = hash(tuple(selectedKeys))
fScoreCorrect = dict()
fScoreRewards = dict()
previousState = dict()
visited = []
maxState = dict()
maxAgent = None
testing = False
#run the iterations
for x in range(1,100000):
# if (x % 100 == 0):
# print x
# if (x < 10):
result = myownq.runInnerLoop(selectedKeys, myownq.qAgent())
# else:
# if (not testing):
# #print maxState
# testing = True
# maxAgent = maxState["agent"]
# selectedKeys = maxState["selected"]
# result = myownq.runTestLoop(selectedKeys, maxAgent)
# #print "RESULT"
# #print result
# #print maxState
fScoreRewards[hashVal] = result[0]
fScoreCorrect[hashVal] = result[1]
agent = result[2]
visited.append(hashVal)
move = False
#move if we are on the first step
if (len(previousState.keys()) == 0):
move = True
else:
delta = fScoreRewards[hashVal] - fScoreRewards[previousState["hash"]]
#move if we move to a better state
if (delta >= 0):
move = True
#move with a probablity defined by the anneal schedule function
else:
move = random.random() <= annealSchedule(delta, x)
if (len(previousState.keys()) == 0 or move):
previousState["hash"] = hashVal
previousState["selected"] = selectedKeys
previousState["remaining"] = remainingKeys
# print (fScoreRewards[hashVal])
# print (fScoreCorrect[hashVal])
# print x
if (len(maxState.keys()) == 0 or fScoreRewards[hashVal] > maxState["score"]):
maxState["hash"] = hashVal
maxState["selected"] = selectedKeys
maxState["remaining"] = remainingKeys
maxState["score"] = fScoreRewards[hashVal]
maxState["correct"] = fScoreCorrect[hashVal]
maxState["agent"] = agent
repeatedState= True
numRepeatedStates = 0
#avoid repeating states
while (repeatedState):
numRepeatedStates += 1
hashVal = previousState["hash"]
selectedKeys = previousState["selected"]
remainingKeys = previousState["remaining"]
#either randomly remove a fundamental, or add one
#don't want too few fundamentals, so only remove if there are more than 4
#also don't want too many, so limit it at like 45
if ((len(selectedKeys) > 4 and util.flipCoin(0.5)) or len(selectedKeys) > 45):
randomKey = random.sample(selectedKeys, 1)[0]
while (randomKey == '"PRICE"'):
randomKey = random.sample(selectedKeys, 1)[0]
selectedKeys.remove(randomKey)
remainingKeys.append(randomKey)
else:
randomKey = random.sample(remainingKeys, 1)[0]
selectedKeys.append(randomKey)
remainingKeys.remove(randomKey)
hashVal = hash(tuple(selectedKeys))
if (hashVal not in visited):
repeatedState = False
if (numRepeatedStates >=1000):
selectedKeys = random.sample(keys, 10)
selectedKeys += ['"PRICE"']
remainingKeys = [key for key in keys if key not in selectedKeys]
# print ("FINAL SOLUTION")
print (maxState)
# agent = maxState["agent"]
# agent.setTestingOn()
#print maxAgent.getTotalRewards()
#print maxAgent.getPercentCorrect()
#run the genetic algorithm
def runGeneticAlgorithm():
while True:
if (iterations == max_iterations):
#get the new generation, ranked by fitness score
rankedPop = geneticAlgorithm.rankPop(chromos)
#print(len(rankedPop))
#print rankedPop
chromos = []
#get the best agent from the population
agent = geneticAlgorithm.iteratePop(rankedPop, popN, True)
listKeys = agent[0]
keyNames = []
#find the best keys for this agent
for i in range(len(listKeys)):
if (listKeys[i] == 1):
keyNames.append(allKeys[i])
print agent
print keyNames
break
# take the population of random chromos and rank them based on their fitness score/proximity to target output
rankedPop = geneticAlgorithm.rankPop(chromos)
#print rankedPop
chromos = []
#get the new chromosomes
chromos = geneticAlgorithm.iteratePop(rankedPop, popN, False)
iterations += 1
#runStochasticDescent()
runSimulatedAnnealing()
#runGeneticAlgorithm()