-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_models.py
executable file
·480 lines (425 loc) · 15 KB
/
test_models.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import unittest
from learners import *
import model
kt_vals = {i: log(i+.5) - log(i+1) for i in range(32)}
def test_partition_bounds():
p = PTW(KT, depth=4)
assert p.get_child_string() == ""
p.update(0)
assert p.get_child_string() == "PTW^0"
assert p.log_prob == log(0.5)
p.update(0)
assert p.get_child_string() == "PTW^1"
assert p.completed_log_probs[0] == 0.0
assert p.completed_log_probs[1] != 0.0
p.update(0)
assert p.get_child_string() == "PTW^1 PTW^0"
p.update(0)
assert p.get_child_string() == "PTW^2"
p.update(0)
assert p.get_child_string() == "PTW^2 PTW^0"
p.update(0)
assert p.get_child_string() == "PTW^2 PTW^1"
p.update(0)
assert p.get_child_string() == "PTW^2 PTW^1 PTW^0"
p.update(0)
assert p.get_child_string() == "PTW^3"
p.update(0)
assert p.get_child_string() == "PTW^3 PTW^0"
p.update(0)
assert p.get_child_string() == "PTW^3 PTW^1"
p.update(0)
assert p.get_child_string() == "PTW^3 PTW^1 PTW^0"
p.update(0)
assert p.get_child_string() == "PTW^3 PTW^2"
p.update(0)
assert p.get_child_string() == "PTW^3 PTW^2 PTW^0"
p.update(0)
assert p.get_child_string() == "PTW^3 PTW^2 PTW^1"
p.update(0)
assert p.get_child_string() == "PTW^3 PTW^2 PTW^1 PTW^0"
p.update(0)
assert p.get_child_string() == "PTW^4"
try:
p.update(0)
assert False
except ValueError:
assert True
def test_depth_differences():
p2 = PTW(KT, depth=2)
p4 = PTW(KT, depth=4)
p8 = PTW(KT, depth=8)
# greater depth means greater resistance to splitting
for _ in range(2):
p2.update(0); p4.update(0); p8.update(0)
assert p2.total_loss > p4.total_loss
assert p4.total_loss > p8.total_loss
assert p8.total_loss > -p8.model_log_probs[0]
for _ in range(2):
p2.update(0); p4.update(0); p8.update(0)
assert p2.total_loss > p4.total_loss
assert p4.total_loss > p8.total_loss
assert p8.total_loss > -p8.model_log_probs[0]
# but our internal nodes are similar
assert p2.log_prob == p4.completed_log_probs[2]
for _ in range(4):
p4.update(0); p8.update(0)
assert p4.total_loss > p8.total_loss
assert p8.total_loss > -p8.model_log_probs[0]
assert p4.completed_log_probs[4] == p8.completed_log_probs[4]
def test_setup():
pt = PTW(KT, depth=2)
assert pt.predict(0) == 0.5
assert pt.predict(1) == 0.5
def test_first_steps_depth():
p = PTW(KT, depth=5)
p.update(0)
assert p.log_prob == log(0.5)
assert p.models[0].predict(0) == 0.75
assert p.predict(0) < 0.75
def test_prob_sum():
# probabilities of a discrete alphabet should sum to one
p = PTW(KT, depth=12)
assert approx(p.predict(0) + p.predict(1), 1)
p.update(0)
assert approx(p.predict(0) + p.predict(1), 1)
p.update(0)
print(p.predict(0), p.predict(1), p.predict(0) + p.predict(1))
assert approx(p.predict(0) + p.predict(1), 1, precision=4)
def test_model_update():
# the total loss having seen a symbol should equal the loss for predicting
# the signal
pt = PTW(KT)
mpt = model.PTW(model.KT)
for i in range(16):
assert approx(mpt.log_predict(0), pt.log_predict(0))
assert mpt.log_predict(0) == mpt.update(0)
assert pt.log_predict(0) == pt.update(0)
def test_improved_model():
# the probability of seeing a symbol should be greater once we've seen
# a symbol
pt = PTW(KT, depth=12)
for _ in range(10):
p0 = pt.predict(0)
pt.update(0)
assert pt.predict(0) > p0
p1 = pt.predict(1),
assert approx(p1 + pt.predict(0), 1, precision=4)
pt.update(1)
assert approx(pt.predict(1) + pt.predict(0), 1, precision=8)
assert pt.predict(1) > p1
def test_ptw_cost():
# there should be a small penalty to PTW if no switches have occurred
pt = PTW(KT, depth=5)
mpt = model.PTW(5, Base=model.KT)
kt = KT()
pt.update(0); kt.update(0); mpt.update(0)
for i in range(11):
assert approx(pt.update(0), mpt.update(0))
kt.update(0)
print(i+1, pt.predict(0), kt.predict(0), mpt.predict(0))
assert approx(pt.log_prob, mpt.log_prob)
assert pt.model_log_probs[0] == kt.log_prob
assert mpt.predict(0) < kt.predict(0)
assert approx(mpt.predict(0), pt.predict(0))
assert pt.predict(0) < kt.predict(0)
assert pt.predict(0) > pt.predict(1)
assert approx(pt.predict(0) + pt.predict(1), 1)
assert approx(kt.predict(0) + kt.predict(1), 1)
def test_all_sequences_sum():
"""
Generate all possible k-length binary sequences. Calculate the log prob of them all.
Make sure they sum to 1
"""
def test_compare_kt():
mKT = model.KT()
m_loss = 0
aKT = KT()
a_loss = 0
for _ in range(16):
m_loss += mKT.update(0)
a_loss += aKT.update(0)
assert approx(a_loss, m_loss, precision=15)
assert approx(mKT.predict(0), aKT.predict(0))
assert approx(mKT.predict(1), aKT.predict(1))
assert approx(aKT.predict(0) + aKT.predict(1), 1)
def test_compare_ptw_updates():
mPTW = model.PTW(13, Base=model.KT)
aPTW = PTW(KT, depth=13)
for i in range(128):
print(i)
assert approx(mPTW.update(0), aPTW.update(0))
def test_compare_ptw():
mPTW = model.PTW(1, Base=model.KT)
aPTW = PTW(KT, depth=1)
for _ in range(2):
assert mPTW.predict(0) == aPTW.predict(0)
assert mPTW.update(0) == aPTW.update(0)
try:
aPTW.update(0)
assert False
except ValueError:
assert True
mPTW = model.PTW(2, Base=model.KT)
aPTW = PTW(KT, depth=2)
for _ in range(4):
assert mPTW.predict(0) == aPTW.predict(0)
assert mPTW.update(0) == aPTW.update(0)
try:
aPTW.update(0)
assert False
except ValueError:
assert True
mPTW = model.PTW(12, Base=model.KT)
aPTW = PTW(KT, depth=12)
for _ in range(4):
assert approx(mPTW.predict(0), aPTW.predict(0))
assert approx(mPTW.update(0), aPTW.update(0))
assert approx(mPTW.log_prob, aPTW.log_prob)
for _ in range(2*4):
assert approx(mPTW.predict(0), aPTW.predict(0))
assert approx(mPTW.update(0), aPTW.update(0))
assert approx(mPTW.log_prob, aPTW.log_prob)
def test_log_store():
mls = model.LogStore()
als = LogStore()
for i in range(64):
mls.add(i)
als.add(i)
assert all([mls[j] == als[j] for j in range(len(mls))])
class DebugModel():
def __init__(self, t=None, tp1=None, left=None, right=None):
if tp1 is None:
tp1 = t
self.bounds = (t, tp1)
self.loss_bound = t
self.left = left
self.right = right
try:
self.num_steps = tp1-t+1
except:
self.num_steps = 0
def update(self, data):
if self.bounds[0] is None:
self.bounds = (data, data)
else:
self.bounds = (self.bounds[0], data)
self.num_steps += 1
@property
def log_prob(self):
if self.num_steps == 0:
return DebugModel()
else:
return DebugModel(*self.bounds)
def __repr__(self):
if self.left is None:
return "{}:{}".format(*self.bounds)
else:
return "{2}:{0}_{1}:{3}".format(self.left,
self.right,
*self.bounds)
def __len__(self):
return self.num_steps
'''
defunct at the moment
class DebugPTL(PTW):
def calculate_partition_loss(self, new_model, left_loss, new_loss):
if new_loss:
return DebugModel(left_loss.bounds[0], new_loss.bounds[1],
left=left_loss.bounds[1], right=new_loss.bounds[0])
else:
return DebugModel(*left_loss.bounds)
def test_debug_model():
t = DebugModel()
assert str(t) == "None:None"
assert len(t) == 0
t.update(0)
assert str(t) == "0:0"
assert len(t) == 1
t.update(1)
assert str(t) == "0:1"
assert len(t) == 2
def test_partition_list():
p = DebugPTL(DebugModel, depth=5)
p.update(0)
assert str(p._models) == "[0:0]"
assert str(p._losses) == "[0:0]"
p.update(1)
assert str(p._models) == "[0:1]"
assert str(p._losses) == "[0:0_1:1]"
p.update(2)
assert str(p._models) == "[0:2, 2:2]"
assert str(p._losses) == "[0:0_1:1, 2:2]"
p.update(3)
assert str(p._models) == "[0:3]"
assert str(p._losses) == "[0:1_2:3]"
p.update(4)
assert str(p._models) == "[0:4, 4:4]"
assert str(p._losses) == "[0:1_2:3, 4:4]"
p.update(5)
assert str(p._models) == "[0:5, 4:5]"
assert str(p._losses) == "[0:1_2:3, 4:4_5:5]"
p.update(6)
assert str(p._models) == "[0:6, 4:6, 6:6]"
assert str(p._losses) == "[0:1_2:3, 4:4_5:5, 6:6]"
for i in range(7, 15):
p.update(i)
assert str(p._models) == "[0:14, 8:14, 12:14, 14:14]"
assert str(p._losses) == "[0:3_4:7, 8:9_10:11, 12:12_13:13, 14:14]"
p.update(15)
assert str(p._models) == "[0:15]"
assert str(p._losses) == "[0:7_8:15]"
'''
'''
# very old tests
class PTWdValues(unittest.TestCase):
global sample_seq, ktp, pr
sample_seq = {'empty': (),
'single': (1,),
'single0': (0,),
'flipped': (1, 0),
'repeated': (1, 1),
'alternating': (1, 0, 1, 0),
'three': (1, 1, 1),
'four': (1, 1, 1, 1),
'five': (1, 1, 1, 1, 1),
'six': (1, 1, 1, 1, 1, 1),
'eight': (1, 1, 1, 1, 1, 1, 1, 1)}
ktp = {k: kt.KTModel(v).get_prob()
for k, v in sample_seq.items()}
pr = {'empty': [1.0 for _ in range(5)],
'single': [ktp['single'] for _ in range(5)],
'single0': [ktp['single'] for _ in range(5)],
'flipped': [PTWd.quick_calc(i,
ktp['flipped'],
ktp['single'],
ktp['single'])
for i in range(5)],
'repeated': [PTWd.quick_calc(i,
ktp['repeated'],
ktp['single'],
ktp['single'])
for i in range(5)]}
pr['alternating'] = [ktp['alternating'],
.5 * ktp['alternating'] + .5 * ktp['flipped'] ** 2,
.5 * ktp['alternating'] + .5 * pr['flipped'][1] ** 2
]
pr['four'] = [ktp['four'],
.5 * ktp['four'] + .5 * ktp['repeated'] ** 2,
.5 * ktp['four'] + .5 * pr['repeated'][1] ** 2]
pr['three'] = [ktp['three'],
.5 * ktp['three'] + .5 * ktp['repeated'] * ktp['single'],
.5 * ktp['three'] + .5 * pr['repeated'][1] * pr['single'][1]]
pr['five'] = [ktp['five'],
.5 * ktp['five'] + .5 * pr['repeated'][0] * pr['three'][0],
.5 * ktp['five'] + .5 * pr['four'][1] * pr['single'][1]]
pr['six'] = [ktp['six'],
.5 * ktp['six'] + .5 * pr['repeated'][0] * pr['four'][0],
.5 * ktp['six'] + .5 * pr['four'][1] * pr['repeated'][0]]
pr['eight'] = [ptw.ptw_recursive(i, kt.KTModel,
sample_seq['eight'],
(1, 0), False) for i in range(4)]
def test_constructor(self):
"""Constructor can take a sequence argument"""
for desc, probs in pr.items():
seq = sample_seq[desc]
for depth, prob in enumerate(probs):
lprob = log2(prob)
s = "of {0} at depth {1} should be {2}".format(desc,
depth,
prob)
if depth is not 0 and len(seq) > exp2(depth):
with self.assertRaises(ptw.SequenceLengthError) as cm:
m = PTWd(depth, kt.KTModel, symbols=(1, 0), sequence=seq)
the_exception = cm.exception
self.assertIsInstance(the_exception,
ptw.SequenceLengthError,
"Depth {0} and seq {1}".format(depth,
seq))
else:
m = PTWd(depth, kt.KTModel, symbols=(1, 0), sequence=seq)
self.assertEqual(list(m.sequence), list(seq),
"Should create " + desc + " sequence")
self.assertAlmostEqual(m.get_prob(),
prob,
msg = "Probability " + s,
places = PRECISION)
self.assertAlmostEqual(m.prob,
lprob,
msg = "Log probability of " + s,
places = PRECISION)
def test_extend_sequence(self):
"""Extending the general model should work as expected"""
for desc, probs in pr.items():
seq = sample_seq[desc]
for depth, prob in enumerate(probs):
if depth is not 0 and len(seq) > exp2(depth):
with self.assertRaises(ptw.SequenceLengthError) as cm:
m = PTWd(depth, kt.KTModel, symbols=(1, 0), sequence=seq)
the_exception = cm.exception
self.assertIsInstance(the_exception,
ptw.SequenceLengthError,
"Depth {0} and seq {1}".format(depth,
seq))
elif depth is not 0:
m = PTWd(depth, kt.KTModel)
m.extend_sequence(seq)
self.assertEqual(m.sequence, list(seq),
"Empty model should allow extension")
def test_conditional_prob_sum(self):
"""The conditional probability of all symbols should sum to one"""
for desc, probs in pr.items():
seq = sample_seq[desc]
for depth, prob in enumerate(probs):
if len(seq) + 1 < exp2(depth):
m = PTWd(depth, kt.KTModel, symbols=(1, 0), sequence=list(seq))
p1 = m.conditional_prob(1)
p2 = m.conditional_prob(0)
self.assertAlmostEqual(p1+p2, 1.0,
msg = "{0}: {1}".format(desc, seq),
places=PRECISION)
def test_sum_conditionals(self):
"""The conditional probability of all symbols should sum to one"""
for desc, probs in pr.items():
seq = sample_seq[desc]
for depth, prob in enumerate(probs):
if len(seq) + 1 < exp2(depth):
m = PTWd(depth, kt.KTModel, symbols=(1, 0), sequence=list(seq))
p1 = m.conditional_prob(1, log_form = True)
p2 = m.conditional_prob(0, log_form = True)
self.assertAlmostEqual(log_sum_exp([p1, p2]), 0.0,
msg = "{0}: {1}".format(desc, seq),
places=PRECISION)
p1 = exp2(p1)
p2 = exp2(p2)
self.assertAlmostEqual(p1+p2, 1.0,
msg = "{0}: {1}".format(desc, seq),
places=PRECISION)
def test_empty_conditional(self):
"""The conditional probability over empty should be the same as the direct prob"""
symbols = [0, 1]
for s in symbols:
for d in range(1, 5):
m = PTWd(d, kt.KTModel)
p1 = m.conditional_prob(s, log_form=False)
p2 = PTWd.calculate_prob(kt.KTModel, [s], symbols, log_form=False)
self.assertEqual(p1, p2,
msg = "Depth {0}: sym {1}".format(d, s))
def test_prod_conditional(self):
"""The current prob * cond prob should be the same as extending seq"""
for desc, probs in pr.items():
seq = sample_seq[desc]
symbols = (1, 0)
for depth, prob in enumerate(probs):
if len(seq) + 1 < exp2(depth):
for s in symbols:
m = PTWd(depth, kt.KTModel, symbols=(1, 0), sequence=list(seq))
cp = m.conditional_prob(s, log_form = True)
fp = cp + m.prob
m.update(s)
ep = m.prob
msg = "{0}: {1}".format(desc, seq),
self.assertAlmostEqual(fp, ep,
msg=msg,
places=PRECISION)
'''