-
Notifications
You must be signed in to change notification settings - Fork 0
/
loanStats.py
414 lines (310 loc) · 12.6 KB
/
loanStats.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import sqlite3
import re, pickle
from collections import defaultdict
from itertools import groupby
from statistics import mean, median, variance
from math import log
#from scipy.stats import ttest_ind, ks_2samp, levene, ansari
import matplotlib.pyplot as plt
import numpy
from sklearn.cluster import MeanShift, DBSCAN
from sklearn.ensemble import AdaBoostRegressor, RandomForestRegressor
from sklearn.linear_model import LogisticRegression
from sklearn.decomposition import PCA
import apiTools
con = sqlite3.connect('current.db')
cur = con.cursor()
hcon = sqlite3.connect('history.db')
hcur = hcon.cursor()
import warnings
warnings.filterwarnings("ignore")
def loadActive(filename):
try:
cur.execute("delete from loans")
except:
pass
cur.execute("vacuum")
sql = """create table if not exists loans (LoanId INT, NoteId INT, OrderId INT,
OutstandingPrincipal REAL, AccruedInterest REAL, Status TEXT, AskPrice REAL,
Markup REAL, YTM REAL, DaysSinceLastPayment INT, CreditScoreTrend TEXT,
FICO TEXT, Listed TEXT, NeverLate INT, LoanClass TEXT, LoanMaturity TEXT,
OriginalNoteAmount TEXT, InterestRate REAL, RemainingPayments INTEGER,
PrincipalInterest REAL, ApplicationType TEXT)"""
cur.execute(sql)
sql = """insert into loans (LoanId, NoteId, OrderId,
OutstandingPrincipal, AccruedInterest, Status, AskPrice,
Markup, YTM, DaysSinceLastPayment, CreditScoreTrend,
FICO, Listed, NeverLate, LoanClass, LoanMaturity,
OriginalNoteAmount, InterestRate, RemainingPayments,
PrincipalInterest, ApplicationType) values
(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"""
with open(filename, "r") as f:
header = True
rowData = []
for idx, line in enumerate(f):
if header:
header = False
continue
rowData.append(line.strip().replace('"','').split(','))
if idx > 0 and idx % 1000 == 0:
cur.executemany(sql, rowData)
con.commit()
rowData = []
print(idx)
def processScores(inlist):
cleanScores = [x[0].replace('"','') for x in inlist]
splitScores = [int(x.split("-")[0]) for x in cleanScores]
return splitScores
def fico_default():
#goodStatus = ['Fully Paid','Current','In Grace Period','Does not meet the credit policy. Status:Fully Paid']
#badStatus = ['Charged Off','Default','Late (31-120 days)','Late (16-30 days)','Does not meet the credit policy. Status:Charged Off']
#neutralStatus = [None, 'Issued']
cur.execute('select FICO, status from loans where not status like "%Issued%"')
uKeys = []
groups = []
data = sorted(cur.fetchall(), key=lambda x: x[1])
for k, g in groupby(data, lambda x: x[1]):
scores = list(g)
groups.append(processScores(scores)) # Store group iterator as a list
uKeys.append(k)
'''
for i in range(len(groups)):
transformData = list(map(log, groups[i]))
print(uKeys[i], mean(transformData), variance(transformData))
'''
for i in range(1, len(groups)):
print(uKeys[0], " vs ", uKeys[i])
#transformData1 = list(map(log, groups[i-1]))
#transformData2 = list(map(log, groups[i]))
print(ttest_ind(groups[0], groups[i], False))
'''
plt.hist(list(map(log, groups[0])), bins=30, histtype='stepfilled', normed=True, color='b', label=uKeys[0])
plt.hist(list(map(log, groups[3])), bins=30, histtype='stepfilled', normed=True, color='r', alpha=0.5, label=uKeys[3])
#plt.title("Gaussian/Uniform Histogram")
plt.xlabel("Value")
plt.ylabel("Count")
plt.legend()
plt.show()
'''
def priceRegression():
fields = [ 'LoanId', 'NoteId', 'OrderId','OutstandingPrincipal', 'CreditScoreTrend','FICO',
'LoanClass', 'LoanMaturity','OriginalNoteAmount', 'InterestRate', 'RemainingPayments', 'Status', 'AskPrice']
print("Loading Data...")
cur.execute('select {} from loans'.format(",".join(fields)))
ids = []
xVals = []
yVals = []
for row in cur.fetchall():
ids.append(row[:3])
xVals.append(list([x for x in row[3:-1]]))
yVals.append(float(row[-1]))
print("Cleaning Variables...")
# Clean up variables
delIdx = []
cstIdx = 1
loanClassIdx = 3
ficoIdx = 2
statusIdx = 8
for idx in range(len(xVals)):
if "DOWN" in xVals[idx][cstIdx]:
xVals[idx][cstIdx] = 0
elif "FLAT" in xVals[idx][cstIdx]:
xVals[idx][cstIdx] = 1
elif "UP" in xVals[idx][cstIdx]:
xVals[idx][cstIdx] = 2
xVals[idx][ficoIdx] = int(xVals[idx][ficoIdx].split("-")[0])
'''
if "true" in xVals[idx][5]:
xVals[idx][5] = 0
elif "false" in xVals[idx][5]:
xVals[idx][5] = 1
'''
ch1 = xVals[idx][loanClassIdx][0]
xVals[idx][loanClassIdx] = (ord(ch1)-64) + (.2 * int(xVals[idx][loanClassIdx][1]))-.1
if xVals[idx][statusIdx] == "Issued":
xVals[idx][statusIdx] = 0
elif xVals[idx][statusIdx] == "Current":
xVals[idx][statusIdx] = 1
elif xVals[idx][statusIdx] == "In Grace Period":
xVals[idx][statusIdx] = 2
elif xVals[idx][statusIdx] == "Late (16-30 days)":
xVals[idx][statusIdx] = 3
elif xVals[idx][statusIdx] == "Late (31-120 days)":
xVals[idx][statusIdx] = 4
try:
xVals[idx] = list(map(float, xVals[idx]))
except:
print(xVals[idx])
delIdx.append(idx)
for idx in reversed(delIdx):
del(xVals[idx])
del(yVals[idx])
print("Performing Regression...")
cData = numpy.array([numpy.array(xi) for xi in xVals])
reg = RandomForestRegressor(n_jobs=-1)
reg.fit(cData, yVals)
'''
targets = []
cur.execute("select LoanId, NoteId, OrderId from loans where Markup < 0")
for row in cur.fetchall():
targets.append((row[0],row[1],row[2]))
targets = set(targets)
print("Preparing Output...")
transformedData = reg.predict(cData)
results = {}
for x,y,z in zip(ids, transformedData, yVals):
if x in targets:
results[x] = (y/z, y)
for row in sorted(results.items(), key = lambda x: x[1][0])[:10]:
print(row)
'''
return reg
def regression():
fields = [ 'LoanId', 'NoteId', 'OrderId','OutstandingPrincipal', 'CreditScoreTrend','FICO',
'LoanClass', 'LoanMaturity','OriginalNoteAmount', 'InterestRate', 'RemainingPayments', 'pmtHistoryScore', 'Status']
print("Loading Data...")
hcur.execute('select {} from loans where pmtHistoryScore > 0'.format(",".join(fields)))
ids = []
xVals = []
yVals = []
for row in hcur.fetchall():
ids.append(row[:3])
xVals.append(list([x for x in row[3:-1]]))
if row[-1] == "Current":
censored = 0
else:
censored = 1
yVals.append(censored)
print("Cleaning Variables...")
# Clean up variables
delIdx = []
cstIdx = 1
loanClassIdx = 3
ficoIdx = 2
for idx in range(len(xVals)):
if "DOWN" in xVals[idx][cstIdx]:
xVals[idx][cstIdx] = 0
elif "FLAT" in xVals[idx][cstIdx]:
xVals[idx][cstIdx] = 1
elif "UP" in xVals[idx][cstIdx]:
xVals[idx][cstIdx] = 2
xVals[idx][ficoIdx] = int(xVals[idx][ficoIdx].split("-")[0])
'''
if "true" in xVals[idx][5]:
xVals[idx][5] = 0
elif "false" in xVals[idx][5]:
xVals[idx][5] = 1
'''
ch1 = xVals[idx][loanClassIdx][0]
xVals[idx][loanClassIdx] = (ord(ch1)-64) + (.2 * int(xVals[idx][loanClassIdx][1]))-.1
try:
xVals[idx] = list(map(float, xVals[idx]))
except:
print(xVals[idx])
delIdx.append(idx)
for idx in reversed(delIdx):
del(xVals[idx])
del(yVals[idx])
print("Performing Regression...")
cData = numpy.array([numpy.array(xi) for xi in xVals])
reg = LogisticRegression(solver='sag', n_jobs=-1, max_iter=200)
reg.fit(cData, yVals)
print("Score: ", reg.score(cData, yVals))
print("Coeffs: ", reg.coef_)
return reg
def calcPayment(pv, interest, periods):
rate = float(interest) / 1200
payment = (rate * pv) / (1-(1+rate) ** (-1.0 * float(periods)))
return payment
def amortTable(pv, payment, interest, periods):
rate = float(interest) / 1200
tableVals = []
for i in range(int(periods)):
#print(pv)
owedInterest = pv * rate
if pv + owedInterest < payment:
payment = pv + owedInterest
appliedToPrincipal = payment - owedInterest
pv -= appliedToPrincipal
tableVals.append((owedInterest, pv))
return tableVals
def calcValue(loanVals, reg):
amount, csTrend, fico, loanClass, periods, interest, remaining, score = loanVals
monthlyPayment = calcPayment(amount, interest, periods)
aTable = amortTable(amount, monthlyPayment, interest, periods)
value = 1.0
sumPayments = 0
for idx, row in enumerate(aTable[-int(remaining):]):
currentValues = [row[1], csTrend, fico, loanClass, periods, amount, interest, periods-1-idx, score]
prediction = reg.predict_proba(currentValues)[0][0]
value *= prediction
sumPayments += row[0]
#print(idx, currentValues, prediction)
#print("Value: ", value)
return value * sumPayments
def findLoans(reg, price):
fields = [ 'LoanId', 'NoteId', 'OrderId','OutstandingPrincipal', 'CreditScoreTrend','FICO',
'LoanClass', 'LoanMaturity','OriginalNoteAmount', 'InterestRate', 'RemainingPayments', 'AskPrice']
print("Loading Data...")
cur.execute('select {} from loans where status = "Current" and AskPrice < {} and Markup < 0'.format(",".join(fields), price))
hcur.execute('select loanClass, avg(pmtHistoryScore) from loans group by loanClass')
avgScores = {x[0]:x[1] for x in hcur.fetchall()}
ids = []
xVals = []
yVals = []
for row in cur.fetchall():
ids.append(list(row[:3]))
xVals.append(list([x for x in row[3:-1]]))
yVals.append(float(row[-1]))
print("Cleaning Variables...")
# Clean up variables
delIdx = []
cstIdx = 1
loanClassIdx = 3
ficoIdx = 2
for idx in range(len(xVals)):
if "DOWN" in xVals[idx][cstIdx]:
xVals[idx][cstIdx] = 0
elif "FLAT" in xVals[idx][cstIdx]:
xVals[idx][cstIdx] = 1
elif "UP" in xVals[idx][cstIdx]:
xVals[idx][cstIdx] = 2
xVals[idx][ficoIdx] = int(xVals[idx][ficoIdx].split("-")[0])
'''
if "true" in xVals[idx][5]:
xVals[idx][5] = 0
elif "false" in xVals[idx][5]:
xVals[idx][5] = 1
'''
xVals[idx].append(avgScores[xVals[idx][loanClassIdx]])
ch1 = xVals[idx][loanClassIdx][0]
xVals[idx][loanClassIdx] = (ord(ch1)-64) + (.2 * int(xVals[idx][loanClassIdx][1]))-.1
try:
xVals[idx] = list(map(float, xVals[idx]))
except:
print(xVals[idx])
delIdx.append(idx)
for idx in reversed(delIdx):
del(xVals[idx])
del(ids[idx])
del(yVals[idx])
print("Calculating Values...")
for idx in range(len(xVals)):
regVars = [xVals[idx][5], xVals[idx][1], xVals[idx][2], xVals[idx][3], xVals[idx][4], xVals[idx][6], xVals[idx][7], xVals[idx][8]]
value = calcValue(regVars, reg)
rate = (numpy.power((value / yVals[idx]), (1.0 / xVals[idx][7])) -1)
#print(rate, value, yVals[idx], xVals[idx][7])
xVals[idx].append(rate)
sortedList = sorted(((e,i) for i,e in enumerate(xVals)), key = lambda x: x[0][-1], reverse = True)
for row in sortedList[:10]:
#print("\t",row[0])
print(ids[row[1]], "Ask : {}".format(yVals[row[1]]), "Rate: {:.3g}".format(row[0][-1]), "Months Remaining: {}".format(int(row[0][-3])))
if __name__ == "__main__":
#apiTest.getCurrentNotes()
#loadActive("currentNotes.csv")
with open('accountNum.pkl', 'rb') as f:
accountNumber = pickle.load(f)
#available = apiTools.availableCash(accountNumber)
available = 30
reg = regression()
findLoans(reg, available)