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

post: bugfix: chains should be detempered/reweighted at once (or bad weights) #322

Merged
merged 6 commits into from
Oct 11, 2023

Conversation

JesusTorrado
Copy link
Contributor

Should work.

@cmbant any feedback on the general approach before I go on with the TODO's?

TODO: do the same with Collection methods reweght, mean and cov

@codecov-commenter
Copy link

codecov-commenter commented Oct 9, 2023

Codecov Report

Merging #322 (2a90ba9) into master (9502fce) will increase coverage by 0.00%.
Report is 1 commits behind head on master.
The diff coverage is 91.80%.

❗ Current head 2a90ba9 differs from pull request most recent head 4081a63. Consider uploading reports for the commit 4081a63 to get more accurate results

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@           Coverage Diff           @@
##           master     #322   +/-   ##
=======================================
  Coverage   84.78%   84.79%           
=======================================
  Files         124      124           
  Lines        9127     9159   +32     
=======================================
+ Hits         7738     7766   +28     
- Misses       1389     1393    +4     
Files Coverage Δ
cobaya/input.py 83.13% <100.00%> (ø)
cobaya/likelihood.py 93.33% <ø> (ø)
cobaya/output.py 88.74% <100.00%> (+0.03%) ⬆️
cobaya/post.py 89.94% <100.00%> (+0.12%) ⬆️
cobaya/samplers/mcmc/mcmc.py 91.64% <ø> (ø)
cobaya/collection.py 89.48% <90.56%> (-0.26%) ⬇️

@cmbant
Copy link
Collaborator

cmbant commented Oct 9, 2023

Looks OK, I guess whatever you do it will be a bit messy

@JesusTorrado
Copy link
Contributor Author

Looks OK, I guess whatever you do it will be a bit messy

Yeah, no clean solution. Finally added with_batch in the minimal number of places where spurious relative weights between chains can be introduced: when detempering and reweighting. I have added a test for this features in test_post_prior, on top of the equivalent getdist one already there.

I have also tried to solve #306

Waiting for your review/go-ahead.

@JesusTorrado JesusTorrado changed the title post: bugfix: chains should be detempered at one (or bad weights) post: bugfix: chains should be detempered/reweighted at once (or bad weights) Oct 10, 2023
@@ -105,8 +132,10 @@ def test_post_prior(tmpdir, temperature):
new_mean, new_cov = mpi.share()
# Noisier with higher temperature
atol, rtol = (1e-8, 1e-5) if temperature == 1 else (5e-4, 1e-3)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason the results aren't basically identical regardless of temperature?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted. This was there since a while ago, recent fixes make it unnecessary. Removing. (If anything, if detempering loaded chains we lose some precision due to the ".8g" txt formatting.)

@@ -58,8 +58,7 @@ def current_logp(self) -> float:
:return: log likelihood from the current state as a scalar
"""
value = self.current_state["logp"]
# Unfortunately, numpy arrays are not derived from Sequence, so the test is ugly
if hasattr(value, "__len__"):
if isinstance(value, Sized):
value = value[0]
Copy link
Collaborator

@cmbant cmbant Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sized does not imply indexable?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the Sized form is also nearly ten times slower than hasattr, which I think may be why we were using that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sized simply implies that it has length (as opposed to Sequence, which is not compatible with numpy arrays). Reverting where applicable. I though you preferred stronger type checks and less duck-typing, but fine to me.

@@ -202,13 +209,15 @@ class SampleCollection(BaseCollection):

def __init__(self, model, output=None, cache_size=_default_cache_size, name=None,
extension=None, file_name=None, resuming=False, load=False,
temperature=None, onload_skip=0, onload_thin=1, sample_type=None):
temperature=None, onload_skip=0, onload_thin=1, sample_type=None,
is_batch=False):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_batch is not used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops!

@JesusTorrado
Copy link
Contributor Author

JesusTorrado commented Oct 11, 2023

OK to merge if tests pass?

Should we merge #319 and release 3.3.3? or better 3.4 since the there were a couple of major fixes?

tests/test_post.py Outdated Show resolved Hide resolved
@cmbant
Copy link
Collaborator

cmbant commented Oct 11, 2023

Sounds good to me, 3.4 could make sense.

@cmbant
Copy link
Collaborator

cmbant commented Oct 11, 2023

Which changes addressed #306?

@JesusTorrado
Copy link
Contributor Author

Which changes addressed #306?

3957c79

Raises rtol from default 1e-5 to 1e-4. Could not verify, bc I have failed to reproduce the issue myself (reload with custom priors, at different temperatures).

@cmbant
Copy link
Collaborator

cmbant commented Oct 11, 2023

Thanks. I guess it depends on the order of magnitude of the various terms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants