示例#1
0
    def proposal(self, current, current_log_pdf, **kwargs):
        if current_log_pdf is None:
            current_log_pdf = self.target_log_pdf(current)

        forward_drift = self._compute_drift(current)
        forward_mu = current + forward_drift

        forward_drift_norm = np.linalg.norm(forward_drift)
        logger.debug("Norm of forward drift: %.3f" % forward_drift_norm)
        self.forward_drift_norms += [forward_drift_norm]

        proposal = sample_gaussian(N=1,
                                   mu=forward_mu,
                                   Sigma=self.L_C,
                                   is_cholesky=True,
                                   cov_scaling=self.step_size)[0]
        forward_log_prob = self.proposal_log_pdf(current,
                                                 proposal[np.newaxis, :],
                                                 drift=forward_drift)[0]

        backward_drift = self._compute_drift(proposal)
        backward_log_prob = self.proposal_log_pdf(proposal,
                                                  current[np.newaxis, :],
                                                  drift=backward_drift)[0]

        proposal_log_pdf = self.target_log_pdf(proposal)

        result_kwargs = {}

        return proposal, proposal_log_pdf, current_log_pdf, forward_log_prob, backward_log_prob, result_kwargs
示例#2
0
    def proposal(self, current):
        """
        Returns a sample from the proposal centred at current, and its log-probability
        """

        if self.schedule is None and (self.mu is None or self.C is None):
            raise ValueError("Kameleon has not seen data yet." \
                             "Either call set_batch_covariance() or set update schedule")

        L_R = self._construct_proposal_covariance(current)
        proposal = sample_gaussian(N=1,
                                   mu=current,
                                   Sigma=L_R,
                                   is_cholesky=True)[0]
        proposal_log_prob = log_gaussian_pdf(proposal,
                                             current,
                                             L_R,
                                             is_cholesky=True)

        # probability of proposing current when would be sitting at proposal
        L_R_inv = self._construct_proposal_covariance(proposal)
        proopsal_log_prob_inv = log_gaussian_pdf(current,
                                                 proposal,
                                                 L_R_inv,
                                                 is_cholesky=True)

        return proposal, proposal_log_prob, proopsal_log_prob_inv
示例#3
0
    def proposal(self, current, current_log_pdf, **kwargs):
        if self.Z is None and self.schedule is None:
            raise ValueError("%s has not seen data yet. Call set_batch()" %
                             self.__class__.__name__)

        if current_log_pdf is None:
            current_log_pdf = self.target_log_pdf(current)

        L_R = self._construct_proposal_covariance(current)
        proposal = sample_gaussian(N=1,
                                   mu=current,
                                   Sigma=L_R,
                                   is_cholesky=True)[0]
        proposal_log_prob = log_gaussian_pdf(proposal,
                                             current,
                                             L_R,
                                             is_cholesky=True)
        proposal_log_pdf = self.target_log_pdf(proposal)

        # probability of proposing y when would be sitting at proposal
        L_R_inv = self._construct_proposal_covariance(proposal)
        proopsal_log_prob_inv = log_gaussian_pdf(current,
                                                 proposal,
                                                 L_R_inv,
                                                 is_cholesky=True)

        log_acc_prob = proposal_log_pdf - current_log_pdf + proopsal_log_prob_inv - proposal_log_prob
        log_acc_prob = np.min([0, log_acc_prob])

        results_kwargs = {}

        return proposal, proposal_log_pdf, current_log_pdf, proposal_log_prob, proopsal_log_prob_inv, results_kwargs
示例#4
0
    def proposal(self, current, current_log_pdf, **kwargs):
        if current_log_pdf is None:
            current_log_pdf = self.target_log_pdf(current)
        
        forward_drift = self._compute_drift(current)
        forward_mu = current + forward_drift

        forward_drift_norm = np.linalg.norm(forward_drift)
        logger.debug("Norm of forward drift: %.3f" % forward_drift_norm)
        self.forward_drift_norms += [forward_drift_norm]
        
        proposal = sample_gaussian(N=1, mu=forward_mu, Sigma=self.L_C,
                                   is_cholesky=True, cov_scaling=self.step_size)[0]
        forward_log_prob = self.proposal_log_pdf(current, proposal[np.newaxis, :],
                                                 drift=forward_drift)[0]
        
        backward_drift = self._compute_drift(proposal)
        backward_log_prob = self.proposal_log_pdf(proposal, current[np.newaxis, :],
                                         drift=backward_drift)[0]
            
        proposal_log_pdf = self.target_log_pdf(proposal)
        
        result_kwargs = {}
        
        return proposal, proposal_log_pdf, current_log_pdf, forward_log_prob, backward_log_prob, result_kwargs
示例#5
0
    def proposal(self, current, current_log_pdf, **kwargs):
        if current_log_pdf is None:
            current_log_pdf = self.target_log_pdf(current)
        
        proposal = sample_gaussian(N=1, mu=current, Sigma=self.L_C,
                                   is_cholesky=True, cov_scaling=self.step_size)[0]
        forw_backw_log_prob = self.proposal_log_pdf(current, proposal[np.newaxis, :])[0]

        proposal_log_pdf = self.target_log_pdf(proposal)
        
        results_kwargs = {}
        
        # probability of proposing current when would be sitting at proposal is symmetric
        return proposal, proposal_log_pdf, current_log_pdf, forw_backw_log_prob, forw_backw_log_prob, results_kwargs
 def proposal(self, current):
     """
     Returns a sample from the proposal centred at current, and its log-probability
     """
     
     if self.schedule is None and (self.mu is None or self.C is None):
         raise ValueError("Kameleon has not seen data yet." \
                          "Either call set_batch_covariance() or set update schedule")
     
     L_R = self._construct_proposal_covariance(current)
     proposal = sample_gaussian(N=1, mu=current, Sigma=L_R, is_cholesky=True)[0]
     proposal_log_prob = log_gaussian_pdf(proposal, current, L_R, is_cholesky=True)
     
     # probability of proposing current when would be sitting at proposal
     L_R_inv = self._construct_proposal_covariance(proposal)
     proopsal_log_prob_inv = log_gaussian_pdf(current, proposal, L_R_inv, is_cholesky=True)
     
     return proposal, proposal_log_prob, proopsal_log_prob_inv
示例#7
0
    def proposal(self, current, current_log_pdf, **kwargs):
        if current_log_pdf is None:
            current_log_pdf = self.target_log_pdf(current)

        proposal = sample_gaussian(N=1,
                                   mu=current,
                                   Sigma=self.L_C,
                                   is_cholesky=True,
                                   cov_scaling=self.step_size)[0]
        forw_backw_log_prob = self.proposal_log_pdf(current,
                                                    proposal[np.newaxis, :])[0]

        proposal_log_pdf = self.target_log_pdf(proposal)

        results_kwargs = {}

        # probability of proposing current when would be sitting at proposal is symmetric
        return proposal, proposal_log_pdf, current_log_pdf, forw_backw_log_prob, forw_backw_log_prob, results_kwargs
示例#8
0
 def proposal(self, current, current_log_pdf, **kwargs):
     if self.Z is None and self.schedule is None:
         raise ValueError("%s has not seen data yet. Call set_batch()" % self.__class__.__name__)
     
     if current_log_pdf is None:
         current_log_pdf = self.target_log_pdf(current)
     
     L_R = self._construct_proposal_covariance(current)
     proposal = sample_gaussian(N=1, mu=current, Sigma=L_R, is_cholesky=True)[0]
     proposal_log_prob = log_gaussian_pdf(proposal, current, L_R, is_cholesky=True)
     proposal_log_pdf = self.target_log_pdf(proposal)
     
     # probability of proposing y when would be sitting at proposal
     L_R_inv = self._construct_proposal_covariance(proposal)
     proopsal_log_prob_inv = log_gaussian_pdf(current, proposal, L_R_inv, is_cholesky=True)
     
     log_acc_prob = proposal_log_pdf - current_log_pdf + proopsal_log_prob_inv - proposal_log_prob
     log_acc_prob = np.min([0, log_acc_prob])
     
     results_kwargs = {}
     
     return proposal, proposal_log_pdf, current_log_pdf, proposal_log_prob, proopsal_log_prob_inv, results_kwargs
示例#9
0
# proposals centred at those points
Ys = np.array([[-20, 9.7], [-10, 0], [0,-3], [10, 0], [20, 9.7]])

for j in range(len(colors)):

    # pick point at random, embed, gradient
    y = Ys[j]
    phi_y = feature_map_single(y, omega, u)
    grad_phi_y = feature_map_grad_single(y, omega, u)
    
    # draw a number of proposals at the current point
    n_proposals = 100
    X_star = np.zeros((n_proposals, D))
    
    # generate proposal samples in feature space
    for i in range(n_proposals):
        plt.plot(y[0], y[1], '*', markersize=15, color=colors[j])
        
        # construct covariance, adding exploration noise
        R = eta**2 * np.dot(grad_phi_y, np.dot(C, grad_phi_y.T))
        L_R = np.linalg.cholesky(R)
        
        # sample proposal
        x_star = sample_gaussian(N=1, mu=y, Sigma=L_R, is_cholesky=True)
        
        X_star[i] = x_star
    
    plt.plot(X_star[:, 0], X_star[:, 1], 'x', color=colors[j])
plt.show()