示例#1
0
def leapfrog_friction_habc_no_storing(c,
                                      V,
                                      q,
                                      dlogq,
                                      p,
                                      dlogp,
                                      step_size=0.3,
                                      num_steps=1):
    """
    MATLAB code by Chen et al
    
    function [ newx ] = sghmc( U, gradU, m, dt, nstep, x, C, V )
    %% SGHMC using gradU, for nstep, starting at position x
    
    p = randn( size(x) ) * sqrt( m );
    B = 0.5 * V * dt; 
    D = sqrt( 2 * (C-B) * dt );
    
    for i = 1 : nstep
        p = p - gradU( x ) * dt  - p * C * dt  + randn(1)*D;
        x = x + p./m * dt;
    end
    newx = x;
    end
    """

    # friction term (as in HABC)
    D = len(q)
    B = 0.5 * V * step_size
    C = np.eye(D) * c + V
    L_friction = np.linalg.cholesky(2 * step_size * (C - B))
    zeros_D = np.zeros(D)

    # create copy of state
    p = np.array(p.copy())
    q = np.array(q.copy())

    # alternate full momentum and variable updates
    for _ in range(num_steps):
        friction = sample_gaussian(N=1,
                                   mu=zeros_D,
                                   Sigma=L_friction,
                                   is_cholesky=True)[0]

        # just like normal momentum update but with friction
        p = p - step_size * -dlogq(q) - step_size * C.dot(-dlogp(p)) + friction

        # normal position update
        q = q + step_size * -dlogp(p)

    return q, p
示例#2
0
    def proposal(self, current, current_log_pdf):
        """
        Returns a sample from the proposal centred at current, acceptance probability,
        and its log-pdf under the target.
        """
        if current_log_pdf is None:
            current_log_pdf = self.target.log_pdf(current)

        # generate proposal
        proposal = sample_gaussian(N=1, mu=current, Sigma=np.eye(self.D) * np.sqrt(self.step_size), is_cholesky=True)[0]
        proposal_log_pdf = self.target.log_pdf(proposal)
        
        # compute acceptance prob, proposals probability cancels due to symmetry
        acc_log_prob = np.min([0, proposal_log_pdf - current_log_pdf])
        
        # probability of proposing current when would be sitting at proposal is symmetric
        return proposal, np.exp(acc_log_prob), proposal_log_pdf
示例#3
0
def leapfrog_friction_habc_no_storing(c, V, q, dlogq, p, dlogp, step_size=0.3, num_steps=1):
    """
    MATLAB code by Chen et al
    
    function [ newx ] = sghmc( U, gradU, m, dt, nstep, x, C, V )
    %% SGHMC using gradU, for nstep, starting at position x
    
    p = randn( size(x) ) * sqrt( m );
    B = 0.5 * V * dt; 
    D = sqrt( 2 * (C-B) * dt );
    
    for i = 1 : nstep
        p = p - gradU( x ) * dt  - p * C * dt  + randn(1)*D;
        x = x + p./m * dt;
    end
    newx = x;
    end
    """
    
    # friction term (as in HABC)
    D = len(q)
    B = 0.5 * V * step_size
    C = np.eye(D) * c + V
    L_friction = np.linalg.cholesky(2 * step_size * (C - B))
    zeros_D = np.zeros(D)
    
    # create copy of state
    p = np.array(p.copy())
    q = np.array(q.copy())
    
    # alternate full momentum and variable updates
    for _ in range(num_steps):
        friction = sample_gaussian(N=1, mu=zeros_D, Sigma=L_friction, is_cholesky=True)[0]

        # just like normal momentum update but with friction
        p = p - step_size * -dlogq(q) - step_size * C.dot(-dlogp(p)) + friction

        # normal position update
        q = q + step_size * -dlogp(p)
        

    return q, p
示例#4
0
 def proposal(self, current, current_log_pdf):
     """
     Returns a sample from the proposal centred at current, acceptance probability,
     and its log-pdf under the target.
     """
     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
     
     return proposal, np.exp(log_acc_prob), proposal_log_pdf
 """
 Example that visualises trajectories of KMC lite and finite on a simple target.
 C.f. Figures 1 and 2 in the paper.
 """
 
 # for D=2, the fitted log-density is plotted, otherwise trajectory only
 D = 2
 N = 1000
 
 # target is banana density, fallback to Gaussian if theano is not present
 if banana_available:
     target = Banana()
     X = sample_banana(N, D)
 else:
     target = IsotropicZeroMeanGaussian(D=D)
     X = sample_gaussian(N=N)
 
 # plot trajectories for both KMC lite and finite, parameters are chosen for D=2
 for surrogate in [
                     KernelExpFiniteGaussian(sigma=10, lmbda=0.001, m=N, D=D),
                     KernelExpLiteGaussian(sigma=20, lmbda=0.001, D=D, N=N),
                     KernelExpLiteGaussianLowRank(sigma=20, lmbda=0.1, D=D, N=N, cg_tol=0.01),
                     
                   ]:
     surrogate.fit(X)
     
     
     # HMC parameters
     momentum = IsotropicZeroMeanGaussian(D=D, sigma=.1)
     num_steps = 1000
     step_size = .01
示例#6
0
    D = 2
    N = 200
    seed = int(sys.argv[1])
    np.random.seed(seed)
    # target is banana density, fallback to Gaussian if theano is not present
    if banana_available:
        b = 0.03
        V = 100
        target = Banana(bananicity=b, V=V)
        X = sample_banana(5000, D, bananicity=b, V=V)
        ind = np.random.permutation(range(X.shape[0]))[:N]
        X = X[ind]
        print 'sampling from banana distribution...', X.shape
    else:
        target = IsotropicZeroMeanGaussian(D=D)
        X = sample_gaussian(N=N)
        print 'sampling from gaussian distribution'

    # compute sigma
    sigma = np.median(squareform(pdist(X))**2) / np.log(N + 1.0) * 2
    M = 200
    if N < M:
        start_samples = np.tile(
            X, [int(M / N) + 1, 1])[:M] + np.random.randn(M, 2) * 2
    else:
        start_samples = X[:M] + np.random.randn(M, 2) * 2
    #start_samples[:, 0] *= 4; start_samples[:, 1] *= 2

    # plot trajectories for both KMC lite and finite, parameters are chosen for D=2
    results = []
    num_steps = 2000