def test_manual_example(): model = ctw.create_model() model.see_generated([1]) eq_(_get_history_p(model), 0.5) model.see_generated([1]) # pw = 0.5 * (3/8 + 1 * 0.5 * 0.5) = 5/16.0 eq_float_(_get_history_p(model), 5/16.0)
def test_manual_example(): model = ctw.create_model() model.see_generated([1]) eq_(_get_history_p(model), 0.5) model.see_generated([1]) # pw = 0.5 * (3/8 + 1 * 0.5 * 0.5) = 5/16.0 eq_float_(_get_history_p(model), 5 / 16.0)
def test_impossible_history(): model = ctw.create_model(deterministic=True, max_depth=2) model.see_generated([0, 0, 0]) eq_(0.0, model.predict_one()) model.revert_generated(3) eq_(0.5, model.predict_one()) model.see_generated([1, 1, 1]) eq_(1.0, model.predict_one())
def test_continue_example(): # A test of the documentation example: # ./continue.py -n 10 01101 model = ctw.create_model() model.see_generated(to_bits("01101")) p_given = _get_history_p(model) model.see_generated(to_bits("1011011011")) p_seq = _get_history_p(model) eq_float_(p_seq/float(p_given), 0.052825, precision=6)
def test_max_depth_sum(): for seq_len in xrange(10): total = 0.0 for seq in iter_all_seqs(seq_len): model = ctw.create_model(max_depth=8) model.see_generated(to_bits(seq)) total += _get_history_p(model) eq_float_(total, 1.0, precision=15)
def test_max_depth(): model = ctw.create_model(max_depth=0) eq_(_get_history_p(model), 1.0) model.see_generated([1]) eq_(model.root.log_p_estim, math.log(0.5)) eq_(model.root.log_pw, model.root.log_p_estim) model.see_generated([1]) eq_(_get_history_p(model), naive_ctw._estim_kt_p(0, 2))
def test_continue_example(): # A test of the documentation example: # ./continue.py -n 10 01101 model = ctw.create_model() model.see_generated(to_bits("01101")) p_given = _get_history_p(model) model.see_generated(to_bits("1011011011")) p_seq = _get_history_p(model) eq_float_(p_seq / float(p_given), 0.052825, precision=6)
def test_predict_one(): seq_len = 8 for determ in [False, True]: for seq in iter_all_seqs(seq_len): model = ctw.create_model(determ) verifier = naive_ctw.create_model(determ) for c in seq: model.see_generated(to_bits(c)) verifier.see_generated(to_bits(c)) eq_float_(model.predict_one(), verifier.predict_one(), precision=10)
def test_children_respecting(): model = ctw.create_model(deterministic=True) model.see_generated([0, 1]) model.switch_history() model.see_generated([0]) p_estim = 0 p_uncovered = 0.5 * 0.5 p_child = 0.5 eq_(model.get_history_log_p(), math.log(0.5 * (p_estim + p_child * 1.0 * p_uncovered)))
def test_invalid_history(): model = ctw.create_model(deterministic=True) model.see_generated([0, 1, 1]) model.switch_history() model.see_generated([0, 1]) try: model.see_generated([0]) assert False, "ImpossibleHistoryError is expected" except ctw.ImpossibleHistoryError, e: assert str(e).startswith( "Impossible history. Try non-deterministic prior.")
def test_max_depth_example(): # The calculated probablities are from the # 'Reflections on "The Context-Tree Weighting Method: Basic Properties"' # paper (figure 6 and 7). model = ctw.create_model(max_depth=3) model.see_added([1,1,0]) model.see_generated(to_bits("0100110")) p_seq = _get_history_p(model) eq_float_(p_seq, 7/2048.0) model.see_generated([0]) p_seq2 = _get_history_p(model) eq_float_(p_seq2, 153/65536.0)
def test_max_depth_example(): # The calculated probablities are from the # 'Reflections on "The Context-Tree Weighting Method: Basic Properties"' # paper (figure 6 and 7). model = ctw.create_model(max_depth=3) model.see_added([1, 1, 0]) model.see_generated(to_bits("0100110")) p_seq = _get_history_p(model) eq_float_(p_seq, 7 / 2048.0) model.see_generated([0]) p_seq2 = _get_history_p(model) eq_float_(p_seq2, 153 / 65536.0)
def test_predict_from_zero_history(): model = ctw.create_model(deterministic=True) model.see_generated([1, 1]) model.switch_history() p_estim = 0.5 p_uncovered = 0.5 p_child = 0.5 pw = 0.5 * (p_estim + p_uncovered * p_child * 1.0) eq_float_(model.get_history_log_p(), math.log(pw)) p_estim = 0.5 p_uncovered = 0.5 * 0.5 p_child = 0.5 pw = 0.5 * (p_estim + p_uncovered * p_child * 1.0) eq_float_(model.predict_one(), pw / math.exp(model.get_history_log_p()))
def create_model(deterministic=False, max_depth=None, num_factors=8): cts = [] for i in xrange(num_factors): cts.append(ctw.create_model(deterministic, max_depth)) return _Factored(cts)
def test_sure(): model = ctw.create_model(deterministic=True, max_depth=2) model.see_generated([1, 1, 1]) eq_(1.0, model.predict_one())
def create_model(deterministic=False, max_depth=None, num_factors=8): cts = [] for i in range(num_factors): cts.append(ctw.create_model(deterministic, max_depth)) return _Factored(cts)
def test_see(): contexted = naive_ctw._Contexted(naive_ctw._estim_kt_p) for seq in iter_all_seqs(seq_len=10): model = ctw.create_model() model.see_generated(to_bits(seq)) eq_float_(_get_history_p(model), contexted.calc_p("", seq))
def test_empty_context(): model = ctw.create_model() eq_(model.predict_one(), 0.5)
def test_predict_first(): for determ in [False, True]: model = ctw.create_model(determ) verifier = naive_ctw.create_model(determ) eq_float_(model.predict_one(), verifier.predict_one())
def test_see(): contexted =naive_ctw._Contexted(naive_ctw._estim_kt_p) for seq in iter_all_seqs(seq_len=10): model = ctw.create_model() model.see_generated(to_bits(seq)) eq_float_(_get_history_p(model), contexted.calc_p("", seq))