Ejemplo n.º 1
0
def forward_sample_from_prior(
        inf_seed,
        n_samples,
        M_c,
        T,
        probe_columns=(0, ),
        ROW_CRP_ALPHA_GRID=(),
        COLUMN_CRP_ALPHA_GRID=(),
        S_GRID=(),
        MU_GRID=(),
        do_multiprocessing=True,
        N_GRID=default_n_grid,
):
    helper = functools.partial(
        _forward_sample_from_prior,
        M_c=M_c,
        T=T,
        probe_columns=probe_columns,
        ROW_CRP_ALPHA_GRID=ROW_CRP_ALPHA_GRID,
        COLUMN_CRP_ALPHA_GRID=COLUMN_CRP_ALPHA_GRID,
        S_GRID=S_GRID,
        MU_GRID=MU_GRID,
        N_GRID=N_GRID,
    )
    cpu_count = 1 if not do_multiprocessing else multiprocessing.cpu_count()
    with gu.MapperContext(do_multiprocessing) as mapper:
        seeds = numpy.random.randint(32676, size=cpu_count)
        n_samples_list = gu.divide_N_fairly(n_samples, cpu_count)
        forward_sample_data_list = mapper(helper, zip(seeds, n_samples_list))
        forward_sample_data = condense_diagnostics_data_list(
            forward_sample_data_list)
    return forward_sample_data
Ejemplo n.º 2
0
def run_posterior_chains(
    M_c,
    T,
    num_chains,
    num_iters,
    probe_columns,
    row_crp_alpha_grid,
    column_crp_alpha_grid,
    s_grid,
    mu_grid,
    N_GRID=default_n_grid,
    CT_KERNEL=0,
):
    # run geweke: transition-erase loop
    helper = functools.partial(
        run_posterior_chain,
        M_c=M_c,
        T=T,
        num_iters=num_iters,
        probe_columns=probe_columns,
        ROW_CRP_ALPHA_GRID=row_crp_alpha_grid,
        COLUMN_CRP_ALPHA_GRID=column_crp_alpha_grid,
        S_GRID=s_grid,
        MU_GRID=mu_grid,
        N_GRID=N_GRID,
        CT_KERNEL=CT_KERNEL,
        # this breaks with multiprocessing
        plot_rand_idx=(num_chains == 1),
    )
    seeds = list(range(num_chains))
    do_multiprocessing = num_chains != 1
    with gu.MapperContext(do_multiprocessing) as mapper:
        diagnostics_data_list = mapper(helper, seeds)
        pass
    return diagnostics_data_list
Ejemplo n.º 3
0
def get_log_density_series(values, bins):
    bin_widths = numpy.diff(bins)
    #
    with gu.MapperContext() as mapper:
        counts = mapper(get_count, [(el, bins) for el in values])
        pass
    counts = numpy.vstack(counts).cumsum(axis=0)
    #
    ratios = counts / numpy.arange(1., len(counts) + 1.)[:, numpy.newaxis]
    densities = ratios / bin_widths[numpy.newaxis, :]
    log_densities = numpy.log(densities)
    return log_densities
Ejemplo n.º 4
0
def get_fixed_gibbs_kl_series(forward, not_forward):
    forward, not_forward = make_same_length(forward, not_forward)
    forward, not_forward = map(numpy.array, (forward, not_forward))
    grid = numpy.array(sorted(set(forward).union(not_forward)))
    kls = numpy.repeat(numpy.nan, len(forward))
    try:
        bins = numpy.append(grid, grid[-1] + numpy.diff(grid)[-1])
        #
        log_true_series = get_log_density_series(forward, bins)
        log_inferred_series = get_log_density_series(not_forward, bins)
        arg_tuples = [(grid, x, y)
                      for x, y in zip(log_true_series, log_inferred_series)]
        with gu.MapperContext() as mapper:
            kls = mapper(_get_kl_tuple, arg_tuples)
            pass
    except Exception as e:
        # this definitley happens if len(grid) == 1; as in column crp alpha for
        # single column model
        pass
    return kls