def L_MH_sampler(pars, target, initial_state, run_data, likelihood=True):
    ds, N = run_data.DataStore, run_data.N
    sp = target['State Space']
    opt_scale, L = sp['sigma_opt'], sp['Id']

    if likelihood:
        decision_function, comparison_function = likelihood_acceptance_decision, compose2(
            np.log, target['pdf'])
    else:
        decision_function, comparison_function = acceptance_decision, target[
            'pdf']

    target_pdf = target['pdf']
    current = initial_state
    accepted = True

    z_samples = get_samples(sp=sp, name='Z')
    for n in range(1, N):
        save_state(data_store=ds,
                   step=n,
                   state=current,
                   value=target_pdf(current),
                   accepted_p=accepted)
        proposed = L_generate_candidate(center=current,
                                        L=L,
                                        scale=opt_scale,
                                        z_sample=z_samples[n])
        accepted = decision_function(current, proposed, target_pdf)
        if accepted:
            current = proposed
        else:  # The else clause is redundant but added for readability.
            current = current
    return run_data
def init_AM_pars(sp):
    dim, origin, idty, = sp['dim'], sp['Origin'], sp['Id'], 
    sigma_0, sigma_opt = 0.1/np.sqrt(dim), sp['sigma_opt']
    cov_0, cov_opt = sigma_0**2*idty, sigma_opt**2*idty
    return AM_Pars(Origin=origin, Id=idty,
                   sigma_0=sigma_0, sigma_opt=sigma_opt,
                   C_0=cov_0, C_opt=cov_opt, 
                   z_samples=get_samples(sp=sp, name='Z'))
Esempio n. 3
0
def init_CMA_pars(sp):
    dim = sp['dim']
    return CMA_Parameters(z_samples=get_samples(sp=sp, name='Z'),
                          s=1,
                          k=1 + dim / 2,
                          t_succ=2 / 11,
                          c_p=1 / 12,
                          c_c=2 / (dim + 2),
                          c_cov=2 / (dim**2 + 6),
                          p_thres=0.44)
Esempio n. 4
0
def C_GaA_sampler(pars, target, initial_state, run_data):
    target_pdf, sp = target['pdf'], target['State Space']
    Origin, Id = sp['Origin'], sp['Id']

    ds, N = run_data.DataStore, run_data.N
    z_samples = get_samples(sp=sp, name='Z')

    #Set up and save the initial state
    m = x_current = initial_state
    sigma = 1
    C = Id

    save_state(data_store=ds,
               step=0,
               state=x_current,
               value=target_pdf(x_current),
               accepted_p=True,
               mean=m,
               covariance=C,
               scale=sigma,
               threshold=None)

    #Sample and save state
    for n in range(1, N):
        z_sample = z_samples[n]
        x_proposed = C_generate_GaA_candidate(mean=x_current,
                                              C=C,
                                              z_sample=z_sample)
        accepted = acceptance_decision(x_current, x_proposed, target_pdf)
        if accepted:
            x_current = x_proposed
            sigma = expand(sigma, pars=pars)
            m = GaA_mean_update(mean=m, sample=x_proposed, pars=pars)
            C = GaA_C_update(C=C, mean=m, sample=x_proposed, pars=pars)
        else:
            sigma = contract(sigma, pars=pars)
        save_state(data_store=ds,
                   step=n,
                   state=x_current,
                   value=target_pdf(x_current),
                   accepted_p=accepted,
                   mean=m,
                   covariance=C,
                   scale=sigma,
                   threshold=None)
    return run_data