Example #1
0
def check_generate(name):
    check_cm(name)
    cm = ComponentModel(name)
    cm.realize_hp()
    params = cm.generate_post()
    b = BasicDistribution(name, pm=params)
    b.sample_data()
Example #2
0
def check_ss_io(name):
    check_cm(name)
    cm = ComponentModel(name)
    cm.realize_hp()
    assert_equal(ComponentModel(name, ss=cm.dump_ss()).dump_ss(), cm.dump_ss())
    cm.add_data(cm.sample_data())
    assert_equal(ComponentModel(name, ss=cm.dump_ss()).dump_ss(), cm.dump_ss())
Example #3
0
def check_ss_io(name):
    check_cm(name)
    cm = ComponentModel(name)
    cm.realize_hp()
    assert_equal(ComponentModel(name, ss=cm.dump_ss()).dump_ss(), cm.dump_ss())
    cm.add_data(cm.sample_data())
    assert_equal(ComponentModel(name, ss=cm.dump_ss()).dump_ss(), cm.dump_ss())
Example #4
0
def check_generate(name):
    check_cm(name)
    cm = ComponentModel(name)
    cm.realize_hp()
    params = cm.generate_post()
    b = BasicDistribution(name, pm=params)
    b.sample_data()
Example #5
0
def check_dd(impl, data_count, D):
    check_cm(impl)
    data = histogram(np.random.randint(D, size=data_count), bin_count=D)
    cm = ComponentModel(
            impl,
            ss={'counts': data},
            p={'D': D})
    cm.realize_hp()
    _check_discrete(cm)
Example #6
0
def check_sums(name):
    check_cm(name)
    cm = ComponentModel(name)
    cm.realize_hp()
    values = [cm.sample_data() for _ in range(COUNT)]
    score = 0.
    for value in values:
        score += cm.pred_prob(value)
        cm.add_data(value)
    assert_almost_equal(score, cm.data_prob())
Example #7
0
def check_sums(name):
    check_cm(name)
    cm = ComponentModel(name)
    cm.realize_hp()
    values = [cm.sample_data() for _ in range(COUNT)]
    score = 0.
    for value in values:
        score += cm.pred_prob(value)
        cm.add_data(value)
    assert_almost_equal(score, cm.data_prob())
Example #8
0
def check_probs(a, b):
    check_cm(a)
    check_cm(b)
    a = ComponentModel(a)
    a.realize_hp()
    b = ComponentModel(b, hp=a.dump_hp())
    dps = [a.sample_data() for _ in range(DPS)]
    for y in dps:
        assert_almost_equal(a.data_prob(), b.data_prob())
        assert_almost_equal(a.pred_prob(y), b.pred_prob(y))
        a.add_data(y)
        b.add_data(y)
Example #9
0
def check_sample_data_seed(name):
    check_cm(name)
    n = 10
    seed(0)
    cm1 = ComponentModel(name)
    cm1.realize_hp()
    data_values1 = [cm1.sample_data() for _ in range(n)]
    seed(0)
    cm2 = ComponentModel(name)
    cm2.realize_hp()
    data_values2 = [cm2.sample_data() for _ in range(n)]
    for i in range(n):
        assert_almost_equal(data_values1[i], data_values2[i])
Example #10
0
def check_sample_data_seed(name):
    check_cm(name)
    n = 10
    seed(0)
    cm1 = ComponentModel(name)
    cm1.realize_hp()
    data_values1 = [cm1.sample_data() for _ in range(n)]
    seed(0)
    cm2 = ComponentModel(name)
    cm2.realize_hp()
    data_values2 = [cm2.sample_data() for _ in range(n)]
    for i in range(n):
        assert_almost_equal(data_values1[i], data_values2[i])
Example #11
0
def check_ss(a, b):
    check_cm(a)
    check_cm(b)
    a = ComponentModel(a)
    a.realize_hp()
    b = ComponentModel(b, hp=a.dump_hp())
    dps = [a.sample_data() for _ in range(DPS)]
    assert_equal(a.dump_ss(), b.dump_ss())
    for y in dps:
        a.add_data(y)
        b.add_data(y)
        assert_close(a.dump_ss(), b.dump_ss())
    for y in dps:
        a.remove_data(y)
        b.remove_data(y)
        assert_close(a.dump_ss(), b.dump_ss())
Example #12
0
def check_exchangeable(name):
    check_cm(name)
    cm = ComponentModel(name)
    cm.realize_hp()
    values = [cm.sample_data() for _ in range(COUNT)]
    p1 = permutation(COUNT)
    p2 = permutation(COUNT)
    for i in range(COUNT):
        cm.add_data(values[p1[i]])
    prob1 = cm.data_prob()
    for i in range(COUNT):
        cm.remove_data(values[p1[i]])
    assert_almost_equal(cm.data_prob(), 0.)
    for i in range(COUNT):
        cm.add_data(values[p2[i]])
    prob2 = cm.data_prob()
    assert_almost_equal(prob1, prob2)
Example #13
0
def check_exchangeable(name):
    check_cm(name)
    cm = ComponentModel(name)
    cm.realize_hp()
    values = [cm.sample_data() for _ in range(COUNT)]
    p1 = permutation(COUNT)
    p2 = permutation(COUNT)
    for i in range(COUNT):
        cm.add_data(values[p1[i]])
    prob1 = cm.data_prob()
    for i in range(COUNT):
        cm.remove_data(values[p1[i]])
    assert_almost_equal(cm.data_prob(), 0.)
    for i in range(COUNT):
        cm.add_data(values[p2[i]])
    prob2 = cm.data_prob()
    assert_almost_equal(prob1, prob2)
Example #14
0
def test_vectorize():
    for name in MODELS:
        check_cm(name)
        cm0 = ComponentModel(name)
        cm0.realize_hp()
        hp0 = cm0.dump_hp()
        cms = [ComponentModel(name, hp=hp0) for _ in range(COMPS)]
        for cm in cms:
            dps = [cm.sample_data() for _ in range(DPS)]
            for dp in dps:
                cm.add_data(dp)

        mod = cms[0].mod
        hp = cms[0].hp
        ss = [cm.ss for cm in cms]
        for cm in cms:
            y = cm.sample_data()
            scores = numpy.zeros(COMPS)
            mod.add_pred_probs(hp, ss, y, scores)
            for cm, score in zip(cms, scores):
                assert_almost_equal(score, cm.pred_prob(y))
Example #15
0
def test_vectorize():
    for name in MODELS:
        check_cm(name)
        cm0 = ComponentModel(name)
        cm0.realize_hp()
        hp0 = cm0.dump_hp()
        cms = [ComponentModel(name, hp=hp0) for _ in range(COMPS)]
        for cm in cms:
            dps = [cm.sample_data() for _ in range(DPS)]
            for dp in dps:
                cm.add_data(dp)

        mod = cms[0].mod
        hp = cms[0].hp
        ss = [cm.ss for cm in cms]
        for cm in cms:
            y = cm.sample_data()
            scores = numpy.zeros(COMPS)
            mod.add_pred_probs(hp, ss, y, scores)
            for cm, score in zip(cms, scores):
                assert_almost_equal(score, cm.pred_prob(y))
Example #16
0
def add_remove_add(name, raw_hps, raw_ss0=None):
    '''
    This tests add_data, remove_data, pred_prob, data_prob
    '''

    DATA_COUNT = 20

    for raw_hp in raw_hps:

        cm = ComponentModel(name, hp=raw_hp, ss=raw_ss0)
        cm.realize_hp()
        data = []
        score = 0

        for _ in range(DATA_COUNT):
            dp = cm.sample_data()
            data.append(dp)
            score += cm.pred_prob(dp)
            cm.add_data(dp)

        cm_all = ComponentModel(name, ss=cm.dump_ss())
        assert_close(
                score,
                cm.data_prob(),
                err_msg='p(x1,...,xn) != p(x1) p(x2|x1) p(xn|...)')

        random.shuffle(data)

        for dp in data:
            cm.remove_data(dp)

        cm0 = ComponentModel(name, ss=raw_ss0)
        assert_close(cm.ss, cm0.ss, err_msg='ss + data - data != ss')

        random.shuffle(data)

        for dp in data:
            cm.add_data(dp)

        assert_close(cm.ss, cm_all.ss, err_msg='ss - data + data != ss')
Example #17
0
def add_remove_add(name, raw_hps, raw_ss0=None):
    '''
    This tests add_data, remove_data, pred_prob, data_prob
    '''

    DATA_COUNT = 20

    for raw_hp in raw_hps:

        cm = ComponentModel(name, hp=raw_hp, ss=raw_ss0)
        cm.realize_hp()
        data = []
        score = 0

        for _ in range(DATA_COUNT):
            dp = cm.sample_data()
            data.append(dp)
            score += cm.pred_prob(dp)
            cm.add_data(dp)

        cm_all = ComponentModel(name, ss=cm.dump_ss())
        assert_close(score,
                     cm.data_prob(),
                     err_msg='p(x1,...,xn) != p(x1) p(x2|x1) p(xn|...)')

        random.shuffle(data)

        for dp in data:
            cm.remove_data(dp)

        cm0 = ComponentModel(name, ss=raw_ss0)
        assert_close(cm.ss, cm0.ss, err_msg='ss + data - data != ss')

        random.shuffle(data)

        for dp in data:
            cm.add_data(dp)

        assert_close(cm.ss, cm_all.ss, err_msg='ss - data + data != ss')
Example #18
0
def check_hp_io(name):
    check_cm(name)
    cm = ComponentModel(name)
    cm.realize_hp()
    assert_equal(ComponentModel(name, hp=cm.dump_hp()).dump_hp(), cm.dump_hp())
Example #19
0
def check_dd(impl, data_count, D):
    check_cm(impl)
    data = histogram(np.random.randint(D, size=data_count), bin_count=D)
    cm = ComponentModel(impl, ss={'counts': data}, p={'D': D})
    cm.realize_hp()
    _check_discrete(cm)
Example #20
0
def check_hp_io(name):
    check_cm(name)
    cm = ComponentModel(name)
    cm.realize_hp()
    assert_equal(ComponentModel(name, hp=cm.dump_hp()).dump_hp(), cm.dump_hp())