示例#1
0
start = np.full(ndim, 1/3)

lim_lower = np.full(ndim, 0)
lim_upper = np.full(ndim, 1)
#sampler = StaticSphericalHMC(ndim, target_log_pdf, target.log_pdf_gradient, .005, .005, 8, 8, lim_lower, lim_upper)
sampler = DualAveragingSphericalHMC(ndim, target_log_pdf, target_log_pdf_gradient, .04, start, adapt_schedule, lim_lower, lim_upper, t0=10., gamma=.05, kappa=.75)

target_log_pdf.called = 0
target_log_pdf_gradient.called = 0
t_start = timer()
samples, mean, variance, n_accepted = sampler.sample(nsamples, start)
t_end = timer()

# discard burnin samples
samples = samples[1000:]
mean = samples.mean()
variance = samples.var()

n_target_calls = target_log_pdf.called
n_gradient_calls = target_log_pdf_gradient.called

print_statistics(samples, mean, variance, exp_mean=0.5, exp_var=0.0331, exp_var_var=0.000609, runtime=t_end-t_start, n_target_calls=n_target_calls, n_gradient_calls=n_gradient_calls, n_accepted=n_accepted)

if ndim == 1:
    plot_1d(samples, target.pdf)
elif ndim == 2:
    plot_2d(samples, target.pdf)
else:
    plot_1d(samples[:, 0], target.pdf)
示例#2
0
    ndim,
    target_pdf,
    metropolis_proposal,
    t_initial=100,
    adapt_schedule=metropolis_adapt_schedule)

# burn in
metropolis_sampler.sample(nburnin, start)
metropolis_sampler.is_adaptive = False

target_pdf.called = 0
t_start = timer()
samples, mean, variance = metropolis_sampler.sample(nsamples, start)
t_end = timer()

n_target_calls = target_pdf.called

print_statistics(samples,
                 mean,
                 variance,
                 exp_mean=0.5,
                 exp_var=0.0331,
                 exp_var_var=0.000609,
                 runtime=t_end - t_start,
                 n_target_calls=n_target_calls)

if ndim == 1:
    plot_1d(samples, target.pdf)
elif ndim == 2:
    plot_2d(samples, target.pdf)
                                           sigma=0.5,
                                           lmbda=0.0001,
                                           N=ntrain)

train_samples = train_sampler.sample(ntrain, start)
print(np.array(train_samples))
print(np.array(train_samples).shape)
#plot_1d([sample[0] for sample in train_samples], target.pdf)
surrogate.train(np.array(train_samples))
print(surrogate.log_pdf_gradient(np.array([1 / 3])))

sampler = StaticHMC(ndim,
                    target.pdf,
                    surrogate.log_pdf_gradient,
                    0.05,
                    0.1,
                    10,
                    30,
                    momentum=IsotropicZeroMeanGaussian(ndim, 1))
#sampler = DualAveragingHMC(ndim, target.pdf, surrogate.log_pdf_gradient, 1., start, hmc_adapt_schedule, momentum=IsotropicZeroMeanGaussian(ndim, 1))
#
samples = sampler.sample(nsamples, start)
samples = [sample[0] for sample in samples]
print(samples)
##
plot_1d(samples,
        target.pdf,
        target_log_pdf_gradient=target.log_pdf_gradient,
        surrogate_log_pdf_gradient=surrogate.log_pdf_gradient)
#plot_2d(samples, target.pdf)
    Gaussian(mu=ndim * [4 / 5], cov=0.005)
]
is_proposal_weights = [0.5, 0.5]
importance_sampler = StaticMultiChannelImportanceSampler(
    ndim, target_pdf, is_proposal_dists, is_proposal_weights)

start = 1 / 3
#start = np.full(ndim, 0.5)
target_pdf.called = 0
t_start = timer()
samples, mean, variance = importance_sampler.sample(nsamples, start)
t_end = timer()

n_target_calls = target_pdf.called

print_statistics(samples,
                 mean,
                 variance,
                 exp_mean=0.5,
                 exp_var=0.0331,
                 exp_var_var=0.000609,
                 runtime=t_end - t_start,
                 n_target_calls=n_target_calls)

if ndim == 1:
    plot_1d(samples,
            target.pdf,
            mapping_pdf=importance_sampler.proposal_dist.pdf)
elif ndim == 2:
    plot_2d(samples, target.pdf)
                        momentum=IsotropicZeroMeanGaussian(ndim, 1))

# construct mixed sampler
sampler_weights = [0.5, 0.5]
sampler = MixedSampler([hmc_sampler, importance_sampler], sampler_weights)

target_pdf.called = 0
t_start = timer()
samples, mean, variance = sampler.sample(nsamples, start)
t_end = timer()

n_target_calls = target_pdf.called

print_statistics(samples,
                 mean,
                 variance,
                 exp_mean=0.5,
                 exp_var=0.0331,
                 exp_var_var=0.000609,
                 runtime=t_end - t_start,
                 n_target_calls=n_target_calls)

if ndim == 1:
    plot_1d(samples,
            target.pdf,
            mapping_pdf=importance_sampler.proposal_dist.pdf,
            target_log_pdf_gradient=target.log_pdf_gradient,
            surrogate_log_pdf_gradient=surrogate.log_pdf_gradient)
elif ndim == 2:
    plot_2d(samples, target.pdf)