Esempio n. 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 get_rejection_table(p_target):
    Z = get_common_denominator(p_target)
    numerators = get_common_numerators(Z, p_target)
    T = [0] * Z
    j = 0
    for i, n in enumerate(numerators):
        T[j:j + n] = [i + 1] * n
        j += n
    assert sum(1 for x in T if x > 0) == Z
    return T
def write_samplers(args):
    (samplers, dirname, idx, p_target, entropy) = args
    structures = [
        ('ky.enc', construct_sample_ky_encoding, write_sample_ky_encoding),
        ('ky.mat', construct_sample_ky_matrix, write_sample_ky_matrix),
        ('ky.matc', construct_sample_ky_matrix_cached,
         write_sample_ky_matrix_cached),
        ('ky.approx.enc', construct_sample_ky_approx_encoding,
         write_sample_ky_encoding),
        ('ky.approx.mat', construct_sample_ky_approx_matrix,
         write_sample_ky_matrix),
        ('ky.approx.matc', construct_sample_ky_approx_matrix_cached,
         write_sample_ky_matrix_cached),
        ('rej.uniform', construct_sample_rejection_uniform,
         write_sample_rejection_uniform),
        ('rej.table', construct_sample_rejection_hash_table,
         write_sample_rejection_hash_table),
        ('rej.binary', construct_sample_rejection_binary_search,
         write_sample_rejection_binary_search),
        ('rej.enc', construct_sample_rejection_encoding,
         write_sample_ky_encoding),
        ('rej.mat', construct_sample_rejection_matrix, write_sample_ky_matrix),
        ('rej.matc', construct_sample_rejection_matrix_cached,
         write_sample_ky_matrix_cached),
        ('interval', construct_sample_interval, write_sample_interval),
        ('alias.exact', construct_sample_alias, write_sample_alias),
    ]

    for suffix, f_construct, f_write in structures:
        if samplers and suffix not in samplers:
            continue
        fpath = os.path.join(dirname, 'd.%05d.%s' % (idx, suffix))
        struc = f_construct(p_target)
        f_write(*struc, fpath)
        print(fpath)

    fname_dist = 'd.%05d.dist' % (idx, )
    fpath_dist = os.path.join(dirname, fname_dist)
    with open(fpath_dist, 'w') as f:
        n = len(p_target)
        Z = get_common_denominator(p_target)
        Ms = get_common_numerators(Z, p_target)
        f.write('%d\n' % (Z, ))
        f.write('%d %s\n' % (n, ' '.join(map(str, Ms))))
        f.write('%1.5f\n' % (entropy, ))
        print(fpath_dist)

    # Make soft links to dist file for non ANCI C baselines.
    for suffix in ['alias.boost', 'inversion.std', 'alias.gsl']:
        if samplers and suffix not in samplers:
            continue
        fname_suffix = fname_dist.replace('.dist', '.%s' % (suffix, ))
        fpath = os.path.join(dirname, fname_suffix)
        subprocess.check_output(['ln', fpath_dist, fpath])
        print(fpath)
def make_rejection_ddg_matrix(p_target):
    n = len(p_target)
    Z = get_common_denominator(p_target)
    k = get_rejection_precision(p_target)
    Ms = get_common_numerators(Z, p_target)
    M_reject = (1 << k) - Z

    h = [0] * k
    H = [-1] * ((n + 1) * k)

    for j in range(k):
        d = 0
        for i in range(n):
            w = (Ms[i] >> ((k - 1) - j)) & 1
            h[j] += (w > 0)
            if w > 0:
                H[d * k + j] = i
                d += 1
        w = (M_reject >> ((k - 1) - j)) & 1
        h[j] += (w > 0)
        if w > 0:
            H[d * k + j] = n

    return h, H
Esempio n. 5
0
def construct_sample_rejection_binary_search(p_target):
    cdf = get_rejection_cdf(p_target)
    Z = get_common_denominator(p_target)
    k = get_rejection_precision(p_target)
    return cdf, Z, k
Esempio n. 6
0
def construct_sample_rejection_hash_table(p_target):
    Z = get_common_denominator(p_target)
    k = get_rejection_precision(p_target)
    T = get_rejection_table(p_target)
    return T, Z, k
Esempio n. 7
0
def construct_sample_rejection_uniform(p_target):
    Z = get_common_denominator(p_target)
    Ms = get_common_numerators(Z, p_target)
    M = max(Ms)
    n = len(p_target)
    return Ms, M, n
Esempio n. 8
0
def construct_sample_interval(p_target):
    n = len(p_target)
    cdf = get_rejection_cdf(p_target)
    Z = get_common_denominator(p_target)
    k = ceil(log2(n))
    return cdf, Z, k
def get_rejection_cdf(p_target):
    Z = get_common_denominator(p_target)
    numerators = get_common_numerators(Z, p_target)
    cdf = [0] + list(cumsum(numerators))
    return cdf
def get_rejection_probabilities(p_target):
    Z = get_common_denominator(p_target)
    k = get_rejection_precision(p_target)
    numerators = get_common_numerators(Z, p_target)
    p_reject = 1 - get_rejection_p_success(p_target)
    return [Fraction(n, 2**k) for n in numerators] + [p_reject]
def get_rejection_p_success(p_target):
    Z = get_common_denominator(p_target)
    k = get_rejection_precision(p_target)
    return Fraction(Z, 2**k)
def get_rejection_precision(p_target):
    Z = get_common_denominator(p_target)
    return ceil(log2(Z))