Beispiel #1
0
pdf = MultivariateNormal(torch.zeros(2, dtype=pdf_dtype), covariance_matrix=torch.eye(2, dtype=pdf_dtype))

def log_pdf(theta, x, y):
    return pdf.log_prob(theta)

model = DistributionModel(log_pdf, 2, dtype=pdf.loc.dtype)

# %% Setup PowerPosteriorSampler

num_chains = 5

per_chain_samplers = [['MALA', {'step': 0.25}] for _ in range(num_chains)]

sampler = PowerPosteriorSampler(
    model,
    DataLoader(EmptyXYDataset()),
    per_chain_samplers,
    theta0=torch.tensor([-1, 1], dtype=model.dtype),
    between_step=1,
    check_input=True
)

# %% Run PowerPosteriorSampler

start_time = timer()

sampler.run(num_epochs=11000, num_burnin_epochs=1000, verbose=True, verbose_step=1000)

end_time = timer()
print("Time taken: {}".format(timedelta(seconds=end_time-start_time)))
pdf = MultivariateNormal(torch.zeros(2, dtype=pdf_dtype),
                         covariance_matrix=torch.eye(2, dtype=pdf_dtype))


def log_pdf(theta, x, y):
    return pdf.log_prob(theta)


model = DistributionModel(log_pdf, 2, dtype=pdf.loc.dtype)

# %% Setup HMC sampler

sampler = HMC(model,
              theta0=torch.tensor([-1, 1], dtype=model.dtype),
              dataloader=DataLoader(EmptyXYDataset()),
              tuner=HMCDATuner(1., e0=2.))

# %% Run HMC sampler

sampler.run(num_epochs=11000,
            num_burnin_epochs=1000,
            verbose=True,
            verbose_step=1000)

# %% Compute acceptance rate

print('Acceptance rate: {}'.format(sampler.get_chain().acceptance_rate()))

# %% Compute Monte Carlo mean
Beispiel #3
0
#         [
#             MultivariateNormal(means[0], covariance_matrix=covs[0]),
#             MultivariateNormal(means[1], covariance_matrix=covs[1])
#         ]))

def log_pdf(theta, x, y):
    return torch.log(
        torch.exp(-0.5 * (torch.dot(theta-means[0], theta-means[0])))
        + torch.exp(-0.5 * torch.dot(theta-means[1], theta-means[1]))
    )

model = DistributionModel(log_pdf, 2, dtype=pdf_dtype)

# %% Setup RAM sampler

sampler = RAM(model, theta0=torch.tensor([0., 0.], dtype=model.dtype), dataloader=DataLoader(EmptyXYDataset()))

# %% Run RAM sampler

sampler.run(num_epochs=11000, num_burnin_epochs=1000)

# %% Compute acceptance rate

print('Acceptance rate: {}'.format(sampler.get_chain().acceptance_rate()))

# %% Compute Monte Carlo mean

print('Monte Carlo mean: {}'.format(sampler.get_chain().mean()))

# %% Compute posterior covariance matrix