Example #1
0
 def __init__(self, surrogate, target, momentum, num_steps_min=10, num_steps_max=100, step_size_min=0.05,
              step_size_max=0.3, adaptation_schedule=None, acc_star=0.7):
     """
     """
     HMCBase.__init__(self, target, momentum, num_steps_min, num_steps_max, step_size_min, step_size_max,
                      adaptation_schedule, acc_star)
     
     assert_implements_log_pdf_and_grad(surrogate)
     
     self.surrogate = surrogate
     self.target = surrogate
     self.orig_target = target
Example #2
0
def get_hmc_kernel():
    num_steps_min, num_steps_max, step_size_min, step_size_max = get_hmc_parameters(
    )
    target, momentum = get_target_momentum()
    hmc = HMCBase(target, momentum, num_steps_min, num_steps_max,
                  step_size_min, step_size_max)
    return hmc
Example #3
0
def sample_banana2(N, D, target):
    momentum = IsotropicZeroMeanGaussian(D=D, sigma=1.0)
    num_steps = 1000
    step_size = 0.2
    hmc = HMCBase(target, momentum, num_steps, num_steps, step_size, \
                  step_size, adaptation_schedule=None)
    start_samples = sample_banana(N, D)
    # simulate trajectory from starting point, note _proposal_trajectory is a "hidden" method
    Qs_total = []
    for i in xrange(N):
        current = start_samples[i]
        current_log_pdf = target.log_pdf(current)
        Qs, acc_probs, log_pdf_q = hmc._proposal_trajectory(
            current, current_log_pdf)
        Qs_total.append(Qs[-1])
    return np.asarray(Qs_total)
Example #4
0
 def accept_prob_log_pdf(self, current, q, p0_log_pdf, p_log_pdf, current_log_pdf):
     # same as super-class, but with original target
     kernel_target = self.target
     self.target = self.orig_target
     
     acc_prob, log_pdf_q = HMCBase.accept_prob_log_pdf(self, current, q, p0_log_pdf, p_log_pdf, current_log_pdf)
     
     # restore target
     self.target = kernel_target
     
     return acc_prob, log_pdf_q
Example #5
0
    def __init__(self,
                 surrogate,
                 target,
                 momentum,
                 num_steps_min=10,
                 num_steps_max=100,
                 step_size_min=0.05,
                 step_size_max=0.3,
                 adaptation_schedule=None,
                 acc_star=0.7):
        """
        """
        HMCBase.__init__(self, target, momentum, num_steps_min, num_steps_max,
                         step_size_min, step_size_max, adaptation_schedule,
                         acc_star)

        assert_implements_log_pdf_and_grad(surrogate)

        self.surrogate = surrogate
        self.target = surrogate
        self.orig_target = target
Example #6
0
    def accept_prob_log_pdf(self, current, q, p0_log_pdf, p_log_pdf,
                            current_log_pdf):
        # same as super-class, but with original target
        kernel_target = self.target
        self.target = self.orig_target

        acc_prob, log_pdf_q = HMCBase.accept_prob_log_pdf(
            self, current, q, p0_log_pdf, p_log_pdf, current_log_pdf)

        # restore target
        self.target = kernel_target

        return acc_prob, log_pdf_q
Example #7
0
def test_hmc_base_init_execute():
    num_steps_min, num_steps_max, step_size_min, step_size_max = get_hmc_parameters(
    )
    target, momentum = get_target_momentum()
    HMCBase(target, momentum, num_steps_min, num_steps_max, step_size_min,
            step_size_max)
Example #8
0
        T = 100
        k = 0
        while k < num_steps + 1:
            # KSD
            Qs_batch = Qs_total[:, k, :]
            ksd_total.append(compute_ksd(Qs_batch, target))
            k += T
        ksd_total = np.asarray(ksd_total)
        results.append([
            Qs_total, acc_probs_total, accor_total, ksd_total, ess_total,
            mean_x1_total
        ])

    # now run HMC
    momentum = IsotropicZeroMeanGaussian(D=D, sigma=1.0)
    hmc = HMCBase(target, momentum, num_steps, num_steps, step_size, \
                  step_size, adaptation_schedule=None)

    # simulate trajectory from starting point, note _proposal_trajectory is a "hidden" method
    Qs_total = []
    acc_probs_total = []
    accor_total = []
    ksd_total = []
    ess_total = []
    mean_x1_total = []
    np.random.seed(seed + 1)
    for i in xrange(M):
        current = start_samples[i]
        current_log_pdf = target.log_pdf(current)
        Qs, acc_probs, log_pdf_q = hmc._proposal_trajectory(
            current, current_log_pdf)
        # compute auto correlation on first dim