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

Fix PTSwap Output #48

Merged
merged 8 commits into from
Dec 7, 2023
Merged
Changes from all commits
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
18 changes: 13 additions & 5 deletions PTMCMCSampler/PTMCMCSampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,15 @@ def PTswap(self, p0, lnlike0, lnprob0, iter):
"""
Do parallel tempering swap.

(Repurposed from Neil Cornish/Bence Becsy's code)

Swap acceptance rates are computed per chain by storing
the number of swaps proposed and accepted. Since swaps
are proposed for every chain, swapProposed is always
incremented and nswap_accepted will be incremented only
for chains that have the swap accepted. The swap acceptance
is calculated elsewhere.

@param p0: current parameter vector
@param lnlike0: current log-likelihood
@param lnprob0: current log posterior value
Expand All @@ -647,12 +656,12 @@ def PTswap(self, p0, lnlike0, lnprob0, iter):
@return lnlike0: new log-likelihood
@return lnprob0: new log posterior value

Repurposed from Neil Cornish/Bence Becsy's code:
"""
Ts = self.ladder

log_Ls = self.comm.gather(lnlike0, root=0) # list of likelihoods from each chain
p0s = self.comm.gather(p0, root=0) # list of parameter arrays from each chain
swap_accepted = np.zeros(self.nchain)

if self.MPIrank == 0:
# set up map to help keep track of swaps
Expand All @@ -669,10 +678,7 @@ def PTswap(self, p0, lnlike0, lnprob0, iter):
acc_ratio = np.exp(log_acc_ratio)
if self.stream.uniform() <= acc_ratio:
swap_map[swap_chain], swap_map[swap_chain + 1] = swap_map[swap_chain + 1], swap_map[swap_chain]
self.nswap_accepted += 1
self.swapProposed += 1
else:
self.swapProposed += 1
swap_accepted[swap_chain] += 1

# loop through the chains and record the new samples and log_Ls
for j in range(self.nchain):
Expand All @@ -682,6 +688,8 @@ def PTswap(self, p0, lnlike0, lnprob0, iter):
# broadcast the new samples and log_Ls to all chains
p0 = self.comm.scatter(p0s, root=0)
lnlike0 = self.comm.scatter(log_Ls, root=0)
self.nswap_accepted += self.comm.scatter(swap_accepted, root=0)
self.swapProposed += 1

# calculate new posterior values
lnprob0 = 1 / self.temp * lnlike0 + self.logp(p0)
Expand Down
Loading