forked from pickledgator/LCBT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcbt.php
335 lines (279 loc) · 9.7 KB
/
lcbt.php
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
<?php
error_reporting(0);
ini_set('memory_limit', '4G');
ini_set('max_execution_time', 86400*7);
require 'Filters.php';
// Print detailed information about a filter list
$filters = array(
new AmountRequested(),
new CreditGrade(),
new DebtToIncomeRatio(),
new Delinquencies(),
new EarliestCreditLine(),
new HomeOwnership(),
new Inquiries(),
new LoanPurpose(),
new MonthsSinceLastDelinquency(),
new PublicRecordsOnFile(),
new RevolvingLineUtilization(),
new State(),
new TotalCreditLines(),
new WordsInDescription()
);
if ($argv[1]) {
$f = explode(",", $argv[1]);
for ($i = 0; $i < count($f); $i++) {
$filters[$i]->current = $f[$i] != '' ? $f[$i] : null;
echo $filters[$i]->details();
}
LCBT::initialize();
$result = LCBT::test($filters);
extract($result);
echo "{$num_loans} loans ({$loans_per_month}/mo.) tested at {$expected_apy}% APY\n";
echo "{$num_defaulted} loans defaulted ({$pct_defaulted}%, \$" .
"{$avg_default_loss} avg loss)\n";
echo "{$net_apy}% net APY\n\n";
exit;
}
LCBT::initialize();
$ga_test = new GA_Test();
$ga_test->run();
class GA_Test {
public $population = array();
public static $population_size = 512;
public static $iterations = 4096;
public static $elite_rate = 0.05;
public static $mutation_rate = 0.05;
public function __construct() {
for ($i=0; $i<self::$population_size; $i++) {
$filters = array(
new AmountRequested(),
new CreditGrade(),
new DebtToIncomeRatio(),
new Delinquencies(),
new EarliestCreditLine(),
new HomeOwnership(),
new Inquiries(),
new LoanPurpose(),
new MonthsSinceLastDelinquency(),
new PublicRecordsOnFile(),
new RevolvingLineUtilization(),
new State(),
new TotalCreditLines(),
new WordsInDescription()
);
foreach ($filters as $filter) {
if (($filter->current = rand(0, $filter->getCount())) == $filter->getCount())
$filter->current = null;
}
$this->population[] = array(
'filters' => $filters
);
}
}
public function run() {
for ($i=0; $i<self::$iterations; $i++) {
echo "Calculating fitness";
$this->calculate_fitness();
$this->sort_by_fitness();
$this->print_best();
$this->mate();
}
}
public function calculate_fitness() {
foreach ($this->population as &$citizen) {
$citizen['results'] = LCBT::test($citizen['filters']);
echo ".";
}
echo "\n";
}
public function sort_by_fitness() {
usort($this->population, function($a, $b) {
$a_fit = $a['results']['net_apy'];
$b_fit = $b['results']['net_apy'];
if ($a['results']['num_loans'] < 600)
$a_fit = 0;
if ($b['results']['num_loans'] < 600)
$b_fit = 0;
return $b_fit - $a_fit;
});
}
public function print_best() {
extract($this->population[0]['results']);
$filters = '';
foreach ($this->population[0]['filters'] as $filter)
$filters .= $filter->current . ',';
$filters = substr($filters, 0, -1);
echo "{$filters}\n";
echo "{$num_loans} loans ({$loans_per_month}/mo.) tested at {$expected_apy}% APY\n";
echo "{$num_defaulted} loans defaulted ({$pct_defaulted}%, \$" .
"{$avg_default_loss} avg loss)\n";
echo "{$net_apy}% net APY\n\n";
}
public function mate() {
// Save the elite!
$num_elite = (int)(self::$elite_rate * self::$population_size);
$final_population = array_slice($this->population, 0, $num_elite);
for ($i=$num_elite; $i<self::$population_size; $i++) {
$final_population[$i] = $this->population[$i];
for ($j=0; $j<count($final_population[$i]['filters']); $j++) {
// Mate with best 20% of population.
$partner = rand(0, self::$population_size / 5);
$final_population[$i]['filters'][$j]->current = $this->population[$partner]['filters'][$j]->current;
// Mutate!
if (!rand(0, 1/self::$mutation_rate)) {
$filter = $final_population[$i]['filters'][$j];
if (($filter->current = rand(0, $filter->getCount())) == $filter->getCount())
$filter->current = null;
}
}
}
$this->population = $final_population;
}
}
class LCBT {
private static $row = 0;
private static $labels = false;
private static $results = array();
private static $filters = array();
private static $loans = array();
public static function initialize() {
// Check in-memory cache.
if (!count(self::$loans)) {
// Check serialized file cache.
$csv_mod = @filemtime("LoanStats.csv");
$serialized_mod = @filemtime("loans.serialized");
if ($serialized_mod && $serialized_mod > $csv_mod) {
echo "Initializing from cache...";
self::$loans = unserialize(file_get_contents("loans.serialized"));
echo " done\n";
}
// double cache miss
else if (($handle = fopen("LoanStats.csv", "r")) !== FALSE) {
echo "Initializing from csv...";
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if (!self::getLabels($data))
continue;
if (!($loan = self::getLoan($data)))
continue;
self::$loans[] = $loan;
}
fclose($handle);
file_put_contents("loans.serialized", serialize(self::$loans));
echo " done\n";
}
else {
die("LoanStats.csv not found.\n");
}
}
}
public static function test($filters) {
self::initialize();
self::$filters = $filters;
$invested = array();
$num = 0;
foreach (self::$loans as $loan) {
$invest = self::consider($loan);
if ($invest) {
$invested[] = $loan;
$statuses[$loan["Status"]]++;
}
}
return self::getNAR($invested);
}
public static function getNAR($invested) {
$time = time();
$profit = $principal = $defaulted = $lost = 0;
foreach ($invested as $loan) {
$elapsed = floor(($time - strtotime($loan["Issued Date"])) / 86400 / 30);
$balance = $loan["Amount Borrowed"];
$ratio = 25 / $balance;
$payments = 0;
while ($elapsed-- > 0) {
// Interest and service charge for the whole loan (not just me)
$interest = (substr($loan["Interest Rate"], 0, -1) / 1200) * $balance;
$service = 0.01 * $loan["Monthly PAYMENT"];
$payments += $loan["Monthly PAYMENT"];
$default = self::defaultedAmount($loan);
if ($default && $payments > $loan["Payments To Date"]) {
$profit -= $default * $ratio;
$lost += $default * $ratio;
$defaulted++;
break;
}
// Compute my ratio of the profit
$profit += (($interest - $service) * $ratio);
$principal += ($balance * $ratio);
$balance -= $loan["Monthly PAYMENT"];
}
}
$filters = '';
foreach (self::$filters as $filter)
$filters .= $filter->current . ',';
$filters = substr($filters, 0, -1);
$nar = @number_format(pow(1 + $profit / $principal, 12) - 1, 4) * 100;
foreach ($invested as $loan)
$rate += substr($loan["Interest Rate"], 0, -1);
$expect = @number_format($rate / count($invested), 2);
$default_percent =
@number_format(($defaulted / count($invested)) * 100, 1);
$num_loans = count($invested);
$avg_loss = @number_format($lost / $defaulted, 2);
// Count loan volume.
$per_month = 0;
foreach ($invested as $loan) {
if (substr($loan['Issued Date'], 0, 7) == '2010-09')
$per_month++;
}
return array(
'filters' => $filters,
'num_loans' => $num_loans,
'loans_per_month' => $per_month,
'expected_apy' => $expect,
'num_defaulted' => $defaulted,
'pct_defaulted' => $default_percent,
'avg_default_loss' => $avg_loss,
'net_apy' => $nar
);
}
public static function defaultedAmount($loan) {
if (in_array($loan["Status"], array("Charged Off", "Default")))
return ($loan["Amount Borrowed"] - $loan["Payments To Date"]);
return 0;
}
public static function consider($loan) {
foreach (self::$filters as $filter)
if (!is_null($filter->current) && !$filter->apply($loan))
return false;
return true;
}
public static function getLoan($data) {
if (self::$row <= 2)
return false;
$loan = array();
$cols = count($data);
for ($c=0; $c<$cols; $c++)
$loan[self::$labels[$c]] = $data[$c];
if (!$loan["Status"] || !$loan["Amount Borrowed"])
return false;
// Only look at loans >3 months old
if (!preg_match("/^2\d{3}-\d{2}-\d{2}$/", $loan["Issued Date"]))
return false;
if (strtotime($loan["Issued Date"]) > time() - 86400*30*3)
return false;
// Ignore loans that didn't even start
if (in_array($loan["Status"], array("Removed", "Expired")))
return false;
return $loan;
}
public static function getLabels($data) {
if (++self::$row == 2) {
self::$labels = array();
$cols = count($data);
for ($c=0; $c<$cols; $c++)
self::$labels[$c] = $data[$c];
}
return self::$labels;
}
}
?>