Пример #1
0
def construct_sample_ky_matrix(p_target):
    Z = get_common_denominator(p_target)
    k, l = get_binary_expansion_length(Z)
    Zkl = get_Zkl(k, l)
    Ms = get_common_numerators(Zkl, p_target)
    P, kp, lp = make_ddg_matrix(Ms, k, l)
    return P, kp, lp
def test_make_matrix():
    Ms, k, l = [6, 6, 6, 6], 5, 3
    P = make_matrix(Ms, k, l)
    assert P[0] == P[1] == P[2] == P[3] == frac_to_bits(Ms[0], k, l)

    Ms, k, l = [6, 6, 6, 6], 5, 3
    P, kp, lp = make_ddg_matrix(Ms, k, l)
    assert kp == 2
    assert lp == 2
    assert P[0] == P[1] == P[2] == P[3] == [0, 1]
def test_reduction_to_single_node():
    # An end-to-end test with automatic
    # reduction and so forth.
    P_desired = [
        [0, 0, 0, 0], # 0
        [1, 1, 1, 1], # 15
    ]
    Ms, k, l = [0, 15], 4, 0
    P, kp, lp = make_ddg_matrix(Ms, k, l)
    root = make_ddg_tree(P, kp, lp)
    assert kp == 1
    assert lp == 0
    assert root.label == 1
    assert root.right is None
    assert root.left is None
def test_deterministic(seed):
    rng = numpy.random.RandomState(seed)

    Ms, k, l = [0, 31], 5, 0
    P, kp, lp = make_ddg_matrix(Ms, k, l)
    root = make_ddg_tree(P, kp, lp)
    encoding = {}
    pack_tree(encoding, root, 0)

    bits = BitStream(kp, rng)
    N_sample = 10000
    samples_mat = [sample_ky_matrix(P, kp, lp, bits) for _i in range(N_sample)]
    samples_enc = [sample_ky_encoding(encoding, bits) for _i in range(N_sample)]
    assert Counter(samples_mat)[1] == N_sample
    assert Counter(samples_enc)[1] == N_sample
def test_nondetermistic(seed):
    rng = numpy.random.RandomState(seed)

    Ms, k, l = [3, 12], 4, 0
    P, kp, lp = make_ddg_matrix(Ms, k, l)
    root = make_ddg_tree(P, kp, lp)
    encoding = {}
    pack_tree(encoding, root, 0)

    bits = BitStream(kp, rng)
    N_sample = 10000
    samples_mat = [sample_ky_matrix(P, kp, lp, bits) for _i in range(N_sample)]
    samples_enc = [sample_ky_encoding(encoding, bits) for _i in range(N_sample)]

    pval_mat = get_chisquare_pval([3/15, 12/15], samples_mat)
    assert 0.05 < pval_mat

    pval_enc = get_chisquare_pval([3/15, 12/15], samples_enc)
    assert 0.05 < pval_enc
def test_sample_ky_matrix_cached():
    Ms, k, l = [3, 2, 1, 7, 2, 1], 4, 4
    P, kp, lp = make_ddg_matrix(Ms, k, l)
    h = make_hamming_vector(P)
    T = make_hamming_matrix(P)

    samples = []
    bitstrings = get_bitstrings(4)
    for bits in bitstrings:
        result0 = sample_ky_matrix(P, kp, lp, (int(b) for b in bits))
        result1 = sample_ky_matrix_cached(kp, lp, h, T, (int(b) for b in bits))
        assert result0 == result1
        samples.append(result0)

    counter = Counter(samples)
    assert counter[1] == 3
    assert counter[2] == 2
    assert counter[3] == 1
    assert counter[4] == 7
    assert counter[5] == 2
    assert counter[6] == 1
Пример #7
0
def construct_sample_rejection_matrix(p_target):
    Ms, k = get_rejection_Ms_k(p_target)
    P, kp, lp = make_ddg_matrix(Ms, k, k)
    return P, kp, lp