Example #1
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)
Example #2
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)
Example #3
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())
Example #4
0
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)
Example #5
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())
Example #6
0
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)
Example #7
0
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))
Example #8
0
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)
Example #9
0
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))
Example #10
0
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)
Example #11
0
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_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.")
Example #15
0
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_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.")
Example #17
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)
Example #18
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 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()))
Example #21
0
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)
Example #22
0
def test_sure():
    model = ctw.create_model(deterministic=True, max_depth=2)
    model.see_generated([1, 1, 1])
    eq_(1.0, model.predict_one())
Example #23
0
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)
Example #24
0
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))
Example #25
0
def test_empty_context():
    model = ctw.create_model()
    eq_(model.predict_one(), 0.5)
Example #26
0
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())
Example #27
0
def test_sure():
    model = ctw.create_model(deterministic=True, max_depth=2)
    model.see_generated([1, 1, 1])
    eq_(1.0, model.predict_one())
Example #28
0
def test_empty_context():
    model = ctw.create_model()
    eq_(model.predict_one(), 0.5)
Example #29
0
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())
Example #30
0
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))