Exemple #1
0
    def set_up(self):
        # remember orginal abc target for later
        self.abc_target = self.target

        # replace with dummy target responsible for gradient computation
        self.target = DummyHABCTarget(self.abc_target)

        HMCJob.set_up(self)
Exemple #2
0
 def set_up(self):
     if self.learn_parameters or self.force_relearn_parameters:
         self.sigma, self.lmbda = self.determine_sigma_lmbda()
     
     logger.info("Using sigma=%.2f, lmbda=%.6f" % (self.sigma, self.lmbda))
     
     
     logger.info("Estimating density in RKHS, N=%d,D=%d" % (len(self.Z), self.D))
     alpha = score_matching_sym(self.Z, self.sigma, self.lmbda)
     
     # replace target by kernel estimator to simulate trajectories on
     # but keep original target for computing acceptance probability
     self.orig_target = self.target
     self.target = LiteEstimatorGaussian(alpha, self.Z, self.sigma)
     
     HMCJob.set_up(self)
Exemple #3
0
    def __init__(self,
                 abc_target,
                 momentum,
                 num_iterations,
                 start,
                 num_steps_min=10,
                 num_steps_max=100,
                 step_size_min=0.05,
                 step_size_max=0.3,
                 momentum_seed=0,
                 statistics={},
                 num_warmup=500,
                 thin_step=1):

        HMCJob.__init__(self, abc_target, momentum, num_iterations, start,
                        num_steps_min, num_steps_max, step_size_min,
                        step_size_max, momentum_seed, statistics, num_warmup,
                        thin_step)

        self.aggregator = HABCJobResultAggregator()
Exemple #4
0
    def set_up(self):
        if self.learn_parameters or self.force_relearn_parameters:
            self.sigma, self.lmbda = self.determine_sigma_lmbda()

        logger.info("Using sigma=%.2f, lmbda=%.6f" % (self.sigma, self.lmbda))

        gamma = 0.5 * (self.sigma**2)
        logger.info("Sampling random basis")
        omega, u = sample_basis(self.D, self.m, gamma)

        logger.info("Estimating density in RKHS, N=%d, m=%d, D=%d" %
                    (len(self.Z), self.m, self.D))
        theta = score_matching_sym(self.Z, self.lmbda, omega, u)

        # replace target by kernel estimator to simulate trajectories on
        # but keep original target for computing acceptance probability
        self.orig_target = self.target
        self.target = RandomFeatsEstimator(theta, omega, u)

        HMCJob.set_up(self)

        # plot density estimate
        if self.plot:
            import matplotlib.pyplot as plt
            from scripts.tools.plotting import evaluate_density_grid, evaluate_gradient_grid, plot_array

            Xs = np.linspace(-15, 15)
            Ys = np.linspace(-7, 3)
            Xs_grad = np.linspace(-40, 40, 40)
            Ys_grad = np.linspace(-15, 25, 40)
            G = evaluate_density_grid(Xs, Ys, self.target.log_pdf)
            G_norm, quiver_U, quiver_V, _, _ = evaluate_gradient_grid(
                Xs_grad, Ys_grad, self.target.grad)
            plt.subplot(211)
            plt.plot(self.Z[:, 0], self.Z[:, 1], 'bx')
            plot_array(Xs, Ys, np.exp(G), plot_contour=True)
            plt.subplot(212)
            plot_array(Xs_grad, Ys_grad, G_norm, plot_contour=True)
            plt.quiver(Xs_grad, Ys_grad, quiver_U, quiver_V, color='m')
            plt.ioff()
            plt.show()
Exemple #5
0
 def __init__(self, Z, sigma, lmbda,
              target, momentum,
              num_iterations, start,
              num_steps_min=10, num_steps_max=100, step_size_min=0.05,
              step_size_max=0.3, momentum_seed=0,
              learn_parameters=False, force_relearn_parameters=False,
              statistics={}, num_warmup=500, thin_step=1):
     
     HMCJob.__init__(self, target, momentum,
                     num_iterations, start,
                     num_steps_min, num_steps_max, step_size_min,
                     step_size_max, momentum_seed, statistics, num_warmup, thin_step)
     
     self.aggregator = KMCJobResultAggregator(len(Z))
     
     self.Z = Z
     self.sigma = sigma
     self.lmbda = lmbda
     self.learn_parameters = learn_parameters
     self.force_relearn_parameters = force_relearn_parameters
     
     self.upper_bound_N = 2000
     self.plot = False
Exemple #6
0
    def propose(self, current, current_log_pdf, samples, accepted):
        # fixed seed for this trajectory
        self.target.update_fixed_random_state()

        # friction leapfrog integrator, broadcasted with current covariance of noise
        c = 1e-5
        V = self.target.grad_cov_est
        self.integrator = lambda q, dlogq, p, dlogp, step_size, num_steps: \
                        leapfrog_friction_habc_no_storing(c, V,
                                                     q, dlogq, p, dlogp, step_size, num_steps)

        # use normal HMC mechanics from here
        return HMCJob.propose(self, current, current_log_pdf, samples,
                              accepted)
Exemple #7
0
 def accept_prob_log_pdf(self, current, q, p0_log_pdf, p_log_pdf, current_log_pdf, samples):
     # potentially re-use log_pdf of last accepted state
     if current_log_pdf is None:
         current_log_pdf = -np.inf
     
     # same as super-class, but with original target
     kernel_target = self.target
     self.target = self.orig_target
     
     acc_prob, log_pdf_q = HMCJob.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
Exemple #8
0
    def accept_prob_log_pdf(self,
                            current,
                            q,
                            p0_log_pdf,
                            p_log_pdf,
                            current_log_pdf=None):
        # potentially re-use log_pdf of last accepted state
        if current_log_pdf is None:
            current_log_pdf = -np.inf

        # same as super-class, but with original target
        habc_target = self.target
        self.target = self.abc_target

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

        # restore target
        self.target = habc_target

        #         return acc_prob, log_pdf_q
        return 1.0, log_pdf_q
def hmc_generator(D, target, num_warmup, thin_step, momentum_seed):
    # determined by pilot runs
    if D == 2:
        step_size_min = 0.8
        step_size_max = 1.5
    elif D == 8:
        step_size_min = 0.6
        step_size_max = 1.3

    start = get_start(D)
    momentum = IsotropicZeroMeanGaussian(sigma=sigma_p, D=D)
    return HMCJob(target,
                  momentum,
                  num_iterations,
                  start,
                  num_steps_min,
                  num_steps_max,
                  step_size_min,
                  step_size_max,
                  momentum_seed,
                  statistics=statistics,
                  num_warmup=num_warmup,
                  thin_step=thin_step)
Exemple #10
0
 def get_parameter_fname_suffix(self):
     return ("KMC_N=%d_" %
             len(self.Z)) + HMCJob.get_parameter_fname_suffix(self)[4:]
Exemple #11
0
 def get_parameter_fname_suffix(self):
     return "HABC_" + HMCJob.get_parameter_fname_suffix(self)[4:]