# possible to change
    D = 2
    N = 1000
    
    # target is banana density, fallback to Gaussian if theano is not present
    if banana_available:
        target = Banana(D=D)
    else:
        target = IsotropicZeroMeanGaussian(D=D)

    samplers = [
                StandardMetropolis(target, D),
                AdaptiveMetropolis(target, D),
                KernelAdaptiveMetropolis(target, D, N=200)
                
                ]

    for sampler in samplers:
        # MCMC parameters, feel free to increase number of iterations
        start = np.zeros(D)
        num_iter = 1000
        
        # run MCMC
        samples, proposals, accepted, acc_prob, log_pdf, times, step_sizes = mini_mcmc(sampler, start, num_iter, D)
        
        visualise_trace(samples, log_pdf, accepted, step_sizes)
        plt.suptitle("%s, acceptance rate: %.2f" % \
                     (sampler.__class__.__name__, np.mean(accepted)))
        
    plt.show()
        # kmc sampler instance
        # note that this version still adapts the step size towards certain acceptance
        # set schedule=None to avoid this; other schedules are possible
        # the sqrt schedule is very conservative
        schedule = standard_sqrt_schedule
        acc_star = 0.7
        kmc = KMCStatic(
            surrogate,
            target,
            momentum,
            num_steps_min,
            num_steps_max,
            step_size_min,
            step_size_max,
            adaptation_schedule=schedule,
            acc_star=acc_star,
        )

        # MCMC parameters, feel free to increase number of iterations
        start = X[0]
        num_iter = 500

        # run MCMC
        samples, proposals, accepted, acc_prob, log_pdf, times, step_sizes = mini_mcmc(kmc, start, num_iter, D)

        visualise_trace(samples, log_pdf, accepted, log_pdf_density=surrogate, step_sizes=step_sizes)
        plt.suptitle("%s, acceptance rate: %.2f" % (surrogate.__class__.__name__, np.mean(accepted)))

    plt.show()
Esempio n. 3
0
        # the sqrt schedule is very conservative
        schedule = standard_sqrt_schedule
        acc_star = 0.7
        kmc = KMCStatic(surrogate,
                        target,
                        momentum,
                        num_steps_min,
                        num_steps_max,
                        step_size_min,
                        step_size_max,
                        adaptation_schedule=schedule,
                        acc_star=acc_star)

        # MCMC parameters, feel free to increase number of iterations
        start = X[0]
        num_iter = 500

        # run MCMC
        samples, proposals, accepted, acc_prob, log_pdf, times, step_sizes = mini_mcmc(
            kmc, start, num_iter, D)

        visualise_trace(samples,
                        log_pdf,
                        accepted,
                        log_pdf_density=surrogate,
                        step_sizes=step_sizes)
        plt.suptitle("%s, acceptance rate: %.2f" % \
                     (surrogate.__class__.__name__, np.mean(accepted)))

    plt.show()
Esempio n. 4
0
    # possible to change
    D = 2
    N = 1000

    # target is banana density, fallback to Gaussian if theano is not present
    if banana_available:
        target = Banana(D=D)
    else:
        target = IsotropicZeroMeanGaussian(D=D)

    samplers = [
        StandardMetropolis(target, D),
        AdaptiveMetropolis(target, D),
        KernelAdaptiveMetropolis(target, D, N=200)
    ]

    for sampler in samplers:
        # MCMC parameters, feel free to increase number of iterations
        start = np.zeros(D)
        num_iter = 1000

        # run MCMC
        samples, proposals, accepted, acc_prob, log_pdf, times, step_sizes = mini_mcmc(
            sampler, start, num_iter, D)

        visualise_trace(samples, log_pdf, accepted, step_sizes)
        plt.suptitle("%s, acceptance rate: %.2f" % \
                     (sampler.__class__.__name__, np.mean(accepted)))

    plt.show()