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
Exemple #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
def test_log_gaussian_pdf_multiple_equals_log_gaussian_pdf_looped_isotropic_cov(
):
    N = 100
    D = 3
    X = np.random.randn(N, D)

    mu = np.random.randn(D)
    cov_scaling = 2.

    log_pdfs = log_gaussian_pdf_multiple(X, mu, cov_scaling=cov_scaling)
    grads = log_gaussian_pdf_multiple(X,
                                      mu,
                                      compute_grad=True,
                                      cov_scaling=cov_scaling)

    print log_pdfs

    for i, x in enumerate(X):
        log_pdf = log_gaussian_pdf(x, mu, cov_scaling=cov_scaling)
        grad = log_gaussian_pdf(x,
                                mu,
                                compute_grad=True,
                                cov_scaling=cov_scaling)

        assert_allclose(log_pdf, log_pdfs[i])
        assert_allclose(grad, grads[i])
 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
def test_log_gaussian_pdf_multiple_equals_log_gaussian_pdf_looped_full_cov():
    N = 100
    D = 3
    X = np.random.randn(N, D)
    
    mu = np.random.randn(D)
    L_C = np.linalg.cholesky(np.dot(X.T, X) + np.eye(D))
    cov_scaling = 2.
    
    log_pdfs = log_gaussian_pdf_multiple(X, mu, L_C, is_cholesky=True, cov_scaling=cov_scaling)
    grads = log_gaussian_pdf_multiple(X, mu, L_C, is_cholesky=True, compute_grad=True, cov_scaling=cov_scaling)

    print log_pdfs

    for i, x in enumerate(X):
        log_pdf = log_gaussian_pdf(x, mu, L_C, is_cholesky=True, cov_scaling=cov_scaling)
        grad = log_gaussian_pdf(x, mu, L_C, is_cholesky=True, compute_grad=True, cov_scaling=cov_scaling)
        
        assert_allclose(log_pdf, log_pdfs[i])
        assert_allclose(grad, grads[i])
def test_log_gaussian_pdf_multiple_equals_log_gaussian_pdf_looped_isotropic_cov():
    N = 100
    D = 3
    X = np.random.randn(N, D)
    
    mu = np.random.randn(D)
    cov_scaling = 2.
    
    log_pdfs = log_gaussian_pdf_multiple(X, mu, cov_scaling=cov_scaling)
    grads = log_gaussian_pdf_multiple(X, mu, compute_grad=True, cov_scaling=cov_scaling)

    print log_pdfs

    for i, x in enumerate(X):
        log_pdf = log_gaussian_pdf(x, mu, cov_scaling=cov_scaling)
        grad = log_gaussian_pdf(x, mu, compute_grad=True, cov_scaling=cov_scaling)
        
        assert_allclose(log_pdf, log_pdfs[i])
        assert_allclose(grad, grads[i])
        
Exemple #7
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
def test_log_gaussian_pdf_multiple_equals_log_gaussian_pdf_looped_full_cov():
    N = 100
    D = 3
    X = np.random.randn(N, D)

    mu = np.random.randn(D)
    L_C = np.linalg.cholesky(np.dot(X.T, X) + np.eye(D))
    cov_scaling = 2.

    log_pdfs = log_gaussian_pdf_multiple(X,
                                         mu,
                                         L_C,
                                         is_cholesky=True,
                                         cov_scaling=cov_scaling)
    grads = log_gaussian_pdf_multiple(X,
                                      mu,
                                      L_C,
                                      is_cholesky=True,
                                      compute_grad=True,
                                      cov_scaling=cov_scaling)

    print log_pdfs

    for i, x in enumerate(X):
        log_pdf = log_gaussian_pdf(x,
                                   mu,
                                   L_C,
                                   is_cholesky=True,
                                   cov_scaling=cov_scaling)
        grad = log_gaussian_pdf(x,
                                mu,
                                L_C,
                                is_cholesky=True,
                                compute_grad=True,
                                cov_scaling=cov_scaling)

        assert_allclose(log_pdf, log_pdfs[i])
        assert_allclose(grad, grads[i])