コード例 #1
0
ファイル: banana.py プロジェクト: wangyongguang/SteinGrad
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)
コード例 #2
0
    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
        accor = autocorr(Qs[:, 0])
        accor_total.append(accor)
        Qs_total.append(Qs)
        # compute min ESS
        ess = RCodaTools.ess_coda_vec(Qs[T_ess + 1:])
        ess = np.minimum(ess, Qs[T_ess + 1:].shape[0])
        min_ess = np.min(ess)
        ess_total.append(min_ess)
        # compute acceptance prob
        acc_probs_total.append(acc_probs)
        # compute E[x1] estimates for different time t
        e_x1 = expectation(Qs[1:, 0])
        mean_x1_total.append(e_x1)