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 AM_sampler(pars, target, initial_state, run_data):
    ds, N = run_data.DataStore, run_data.N
    target_pdf = target['pdf']
    
    current = initial_state
    mean, M2 = pars.Origin, np.zeros_like(pars.Id)
    accepted = True
    
    for n in range(0, N):
        save_state(data_store=ds, step=n,
                   state=current, value=target_pdf(current),
                   mean=mean, covariance=M2, accepted_p=accepted)
        
        # generate new candidate
        candidate = generate_AM_candidate(current=current, M2=M2, n=n, pars=pars)
        
        # run Metropolis Hastings for acceptance criteria
        accepted = acceptance_decision(current=current, proposed=candidate, pdf=target_pdf)
        
        # accepted candidate becomes new state
        if accepted: 
            current = candidate
        # We always update M2, where S^2 = M2/n-1 
        # whether the proposed samples are accepted or not
        mean, M2, n = update_moments(mean, M2, current, n)
    return run_data
Exemple #3
0
def CMA_sampler(pars, target, initial_state, run_data):
    target_pdf, sp = target['pdf'], target['State Space']
    Origin, Id = sp['Origin'], sp['Id']
    s, p_succ, p_c = pars.s, pars.t_succ, Origin
    ds, N = run_data.DataStore, run_data.N
    z_samples = pars.z_samples

    x_current = initial_state
    C = Id

    save_state(data_store=ds,
               step=0,
               state=x_current,
               value=target_pdf(x_current),
               accepted_p=True,
               mean=p_c,
               covariance=C,
               scale=s,
               threshold=p_succ)

    for n in range(1, N):
        # generate new candidate sample
        x_new, delta = generate_CMA_candidate(current=x_current,
                                              scale=s,
                                              cov=C,
                                              z_sample=z_samples[n])

        # run Metropolis Hastings acceptance criteria
        accepted_p = acceptance_decision(current=x_current,
                                         proposed=x_new,
                                         pdf=target_pdf)
        p_succ, s = update_scale(p_succ=p_succ,
                                 sigma=s,
                                 accepted_p=accepted_p,
                                 pars=pars)

        if accepted_p:
            # accepted candidate becomes new state
            x_current = x_new
            p_c, C = update_cov(evol_point=p_c,
                                cov=C,
                                y=delta,
                                avg_success_rate=p_succ,
                                pars=pars)

        # save accepted and non-accepted sates in namedtuple
        save_state(data_store=ds,
                   step=n,
                   state=x_current,
                   value=target_pdf(x_current),
                   accepted_p=accepted_p,
                   mean=p_c,
                   covariance=C,
                   scale=s,
                   threshold=p_succ)
    return run_data
Exemple #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
def MH_sampler(pars,
               target,
               initial_state,
               run_data,
               C_generation=False,
               likelihood=True):
    ds, N = run_data.DataStore, run_data.N

    target_pdf = target['pdf']
    proposal_samples = pars.Proposal['Samples']

    current = initial_state
    accepted = True

    #The integration of the C- and L-variant still has to be done.
    #if C_generation:
    #    generation_function = generate_candidate
    #else:
    #    generation_fuction = L_generate_candidate

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

    for n in range(1, N):
        save_state(data_store=ds,
                   step=n,
                   state=current,
                   value=target_pdf(current),
                   accepted_p=accepted)
        proposed = generate_candidate(center=current,
                                      delta=proposal_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
Exemple #6
0
def AM_sampler(pars, target, initial_state, run_data):
    ds, N = run_data.DataStore, run_data.N

    target_pdf = target['pdf']
    z_samples = pars.z_samples

    current = initial_state
    mean, L, sigma_0 = pars.Origin, pars.L_0, pars.sigma_0
    accepted = True
    d = len(initial_state)
    init_period = 2 * d
    samples = []
    for n in range(init_period):
        save_state(data_store=ds,
                   step=n,
                   state=current,
                   value=target_pdf(current),
                   mean=mean,
                   covariance=L,
                   accepted_p=accepted)
        candidate = L_generate_candidate(m=current,
                                         L=L,
                                         s=sigma_0,
                                         z=z_samples[n])
        accepted = MH_decision(current=current,
                               proposed=candidate,
                               pdf=target_pdf)
        if accepted:
            current = candidate
        else:
            current = current
        samples.append(current)
    # Calculate the first two moments at the end of initial period.
    initial_cov = np.cov(samples, rowvar=False)
    initial_mean = np.mean(samples, axis=0)
    C = initial_cov
    L = la.cholesky(initial_cov)
    mean = initial_mean

    # Once the initial period is finished we start to adapt.
    for n in range(init_period, N):
        #if n%1000 == 0:
        #    print('n:', n)
        save_state(data_store=ds,
                   step=n,
                   state=current,
                   value=target_pdf(current),
                   mean=mean,
                   covariance=L,
                   accepted_p=accepted)

        p_L, p_sigma = get_prop_data(L=L, n=n, pars=pars)
        candidate = L_generate_candidate(m=current,
                                         L=p_L,
                                         s=p_sigma,
                                         z=z_samples[n])
        accepted = MH_decision(current=current,
                               proposed=candidate,
                               pdf=target_pdf)
        if accepted:
            current = candidate
        mean, L, n = update_moments(mean, L, current, n)
    return run_data