BUG FIX: Variable overwrite in PTSwap for-loop#58
Merged
Conversation
vhaasteren
approved these changes
Oct 21, 2025
Member
vhaasteren
left a comment
There was a problem hiding this comment.
This looks completely fine. Clean changes
This was referenced Oct 21, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe the bug
Parallel tempering swap overwrites data when completing a swap. This leads to neighbouring swapped chains to have the same likelihood and parameter values.
Issue
The buggy code in the for-loop under Line 682
Imagine the following scenario where
p0s=[0,1]andswap_map = [1, 0](ignorelog_Lsfor this example).swap_mapis a map to assign the new positions of the elements ofp0s. In this example,swap_mapshould swap the positions of the elementsp0ssuch thatp0s==[1,0]. This doesn't happen in this code.In the first iteration of the loop (
j=0) over the elements of both arrays (size=2), we have the following assignment:p0s[0] = p0s[swap_map[0]]. The value ofswap_map[0]=1, thereforep0s[0]is assignedp0s[1]=1.Therefore, after one iteration of the loop, the elements of
p0sis[1,1].Continuing through the second iteration,
swap_mapassigned the first element of the currentp0s(which is1) to its zeroth position.Hence, the loop returns
p0s=[1,1], notp0s=[1,0]as desired.The bug is that the arrays
p0sandlog_Lsare being overwritten with every iteration of the loop, and its elements are being accessed with the expectation that it is not being overwritten.** Test **
Here is a plot of the distribution of log-likelihood (from the T=1.0 "cold" chain) for a single-chain MCMC, and 4-temperature PTMCMC for the buggy and fixed code. The MCMC is fitting a multi-dimensional Gaussian distribution to noisy data generated from a multi-dimensional Gaussian distribution. The distribution of the log-likelihoods should be consistent between the cold-chain of the PTMCMC and the single-chain MCMC. As you can see, the buggy code explores a less-likely region of the parameter space. The corrected code (in this PR) is consistent with expected behaviour.