Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce number of logp evaluations #3600

Merged
merged 2 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions pymc3/smc/smc.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,12 @@ def sample_smc(

if parallel and cores > 1:
pool = mp.Pool(processes=cores)
results = pool.starmap(likelihood_logp, [(sample,) for sample in posterior])
else:
results = [likelihood_logp(sample) for sample in posterior]
likelihoods = np.array(results).squeeze()

while beta < 1:
if parallel and cores > 1:
results = pool.starmap(likelihood_logp, [(sample,) for sample in posterior])
else:
results = [likelihood_logp(sample) for sample in posterior]
likelihoods = np.array(results).squeeze()
beta, old_beta, weights, sj = calc_beta(beta, likelihoods, threshold)

model.marginal_likelihood *= sj
Expand Down Expand Up @@ -238,16 +237,20 @@ def sample_smc(
if parallel and cores > 1:
results = pool.starmap(
metrop_kernel,
[(posterior[draw], tempered_logp[draw], *parameters) for draw in range(draws)],
[
(posterior[draw], tempered_logp[draw], likelihoods[draw], *parameters)
for draw in range(draws)
],
)
else:
results = [
metrop_kernel(posterior[draw], tempered_logp[draw], *parameters)
metrop_kernel(posterior[draw], tempered_logp[draw], likelihoods[draw], *parameters)
for draw in tqdm(range(draws), disable=not progressbar)
]

posterior, acc_list = zip(*results)
posterior, acc_list, likelihoods = zip(*results)
posterior = np.array(posterior)
likelihoods = np.array(likelihoods)
acc_rate = sum(acc_list) / proposed
stage += 1

Expand Down
4 changes: 3 additions & 1 deletion pymc3/smc/smc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def _posterior_to_trace(posterior, variables, model, var_info):
def metrop_kernel(
q_old,
old_tempered_logp,
old_likelihood,
proposal,
scaling,
accepted,
Expand Down Expand Up @@ -147,9 +148,10 @@ def metrop_kernel(
q_old, accept = metrop_select(new_tempered_logp - old_tempered_logp, q_new, q_old)
if accept:
accepted += 1
old_likelihood = ll
old_tempered_logp = new_tempered_logp

return q_old, accepted
return q_old, accepted, old_likelihood


def calc_beta(beta, likelihoods, threshold=0.5, psis=True):
Expand Down