forked from Rarlegend/cs182
/
gradientDescent.py
219 lines (192 loc) · 8.26 KB
/
gradientDescent.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
import myownq
import random
import util
import math
import geneticAlgorithm
allKeys = 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"']
#stochastic descent with random restarts
def runStochasticDescent():
gradientEpsilon = 0.01
initialized = False
#no price in these keys
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"']
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()
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")
result = myownq.runInnerLoop(selectedKeys)
fScoreRewards[hashVal] = result[0]
fScoreCorrect[hashVal] = result[1]
agent = result[2]
visited.append(hashVal)
move = False
if (len(previousState.keys()) == 0):
move = True
elif (fScoreRewards[previousState["hash"]] < fScoreRewards[hashVal]):
move = True
else:
move = util.flipCoin(gradientEpsilon)
if (move):
previousState["hash"] = hashVal
previousState["selected"] = selectedKeys
previousState["remaining"] = remainingKeys
print (fScoreRewards[hashVal])
print (fScoreCorrect[hashVal])
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
STOPIT = False
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):
STOPIT = True
break
if (STOPIT):
break
print ("FINAL SOLUTION")
print (maxState)
def annealSchedule(delta, time):
return math.exp(float(delta) / float(100001 - time)**0.4)
simulatedAgent = myownq.qAgent()
#The function that runs the inner Q-learning
def runSimulatedAnnealing():
initialized = False
#no price in these keys
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"']
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()
for x in range(1,10000):
result = myownq.runInnerLoop(selectedKeys)
fScoreRewards[hashVal] = result[0]
fScoreCorrect[hashVal] = result[1]
agent = result[2]
visited.append(hashVal)
move = False
if (len(previousState.keys()) == 0):
move = True
else:
delta = fScoreRewards[hashVal] - fScoreRewards[previousState["hash"]]
if (delta >= 0):
move = True
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])
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
STOPIT = False
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):
STOPIT = True
break
if (STOPIT):
break
print ("FINAL SOLUTION")
print (maxState)
def runGeneticAlgorithm():
popN = 100 # n number of chromos per population
genesPerCh = 54
max_iterations = 1000
chromos = geneticAlgorithm.generatePop(popN) #generate new population of random chromosomes
iterations = 0
while True:
if (iterations == max_iterations):
rankedPop = geneticAlgorithm.rankPop(chromos)
#print(len(rankedPop))
#print rankedPop
chromos = []
agent = geneticAlgorithm.iteratePop(rankedPop, popN, True)
listKeys = agent[0]
keyNames = []
for i in range(len(listKeys)):
if (listKeys[i] == 1):
keyNames.append(allKeys[i])
print agent
print keyNames
break
# take the pop of random chromos and rank them based on their fitness score/proximity to target output
rankedPop = geneticAlgorithm.rankPop(chromos)
#print rankedPop
chromos = []
chromos = geneticAlgorithm.iteratePop(rankedPop, popN, False)
iterations += 1
#runStochasticDescent()
#runSimulatedAnnealing()
runGeneticAlgorithm()