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

Made a new branch called logLmax with the updated anesthetic.examples… #348

Merged
merged 22 commits into from
Feb 6, 2024

Conversation

DilyOng
Copy link
Collaborator

@DilyOng DilyOng commented Oct 15, 2023

….perfect_ns.correlated_gaussian.

Description

A bug was found in the function anesthetic.examples.perfect_ns.correlated_gaussian(). The generated Gaussian likelihood is not normalised and the evidence is not unity. Three changes have been made.

  • The function description is updated to remind the users that the correlated_gaussian() does not produce a correlated gaussian likelihood which is normalised and the evidence is not unity.
  • An extra argument called LogLmax is introduced to the function correlated_gaussian().
  • Within the correlated_gaussian() function, the definition of logLike() is corrected.

Fixes # (issue)

Checklist:

  • I have performed a self-review of my own code
  • My code is PEP8 compliant (flake8 anesthetic tests)
  • My code contains compliant docstrings (pydocstyle --convention=numpy anesthetic)
  • New and existing unit tests pass locally with my changes (python -m pytest)
  • I have added tests that prove my fix is effective or that my feature works
  • I have appropriately incremented the semantic version number in both README.rst and anesthetic/_version.py

@codecov
Copy link

codecov bot commented Oct 15, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (7c30e72) 100.00% compared to head (44f6dd5) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #348   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           34        34           
  Lines         2984      2978    -6     
=========================================
- Hits          2984      2978    -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@williamjameshandley williamjameshandley left a comment

Choose a reason for hiding this comment

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

Many thanks for this proposal @DilyOng.

At the moment the tests are failing because your changes are not flake8 PEP compliant. You can see this by clicking 'Details' next to the failed test, and see it locally by running python -m flake8 anesthetic tests. You also need to increment the version number as the Checklist at the top of the PR suggests.

Could you add a test to tests.test_examples.test_perfect_ns_correlated_gaussian which checks that the logLmax functionality is working. The best thing to do would be to make a run with logLmax equal to some number (e.g. 10), and check that samples.logL.mean() is close to logLmax-d/2, where d is the number of parameters.

One other thing we should consider is putting in a logLmax into the gaussian function as well. This would also need a test.

anesthetic/examples/perfect_ns.py Outdated Show resolved Hide resolved
anesthetic/examples/perfect_ns.py Outdated Show resolved Hide resolved
…, which checks that the logLmax functionality is working
… of the other tests. I am using numpy.testing.assert_allclose() but not sure what to put for rtol and atol. I have also updated the .gitignore file to exclude the .DS_Store file. In the __init__.py, I added a temporary line in line 12 to check if the correct anesthetic is being imported locally. This line will need to be removed later.
…te tolerance to the assert_allclose function in test_logLmax. The test is passed. I previously accidentally deleted tests/test.ipynb so I added it back. Deleted the line, print test logLmax in anesthetic/__init__.py
Copy link
Collaborator

@williamjameshandley williamjameshandley left a comment

Choose a reason for hiding this comment

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

Many thanks @DilyOng.

  1. Could you remove the tests/tests.ipynb file
  2. Could you move test_perfect_ns_correlated_gaussian.py int othe file tests/test_examples.py
  3. Could you add the logLmax variable to gaussian
  4. The version number should be 2.5.0 as we have added functionality
  5. Could you modify & simplify the planck_gaussian function so that it uses your new functionality!

tests/test_perfect_ns_correlated_gaussian.py Outdated Show resolved Hide resolved
DilyOng and others added 9 commits November 9, 2023 03:27
…st_perfect_ns_correlated_gaussian.py to tests/test_examples.py, then removed test_perfect_ns_correlated_gaussian.py. Added logLmax variable to gaussian() in anesthetic/examples/perfect_ns.py. Changed version number to 2.5.0 becuase we have added functionality. Modified the planck_gaussian() function in examples/perfect_ns.py, added logLmax when calling the correlated_gaussian() function.
Copy link
Collaborator

@williamjameshandley williamjameshandley left a comment

Choose a reason for hiding this comment

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

Hi @DilyOng,

I have pushed a minor but structural change, which implements the functionality that in gaussian, wedding_cake and correlated_gaussian the 'rest of the arguments' *args, **kwargs are passed on to the NestedSamples constructor.

The main reason for doing this is that it allows us to pass columns and params to correlated_gaussian in the planck_gaussian function.

One further minor change is that with logL_max being set means that the logL_mean comes with an additional nested sampling error, so the tests associated with logL_mean` had to be adjusted.

If you're happy with these changes, you should add yourself to the author list in pyproject.toml, and then I'll approve these changes to be merged.

Copy link
Collaborator

@williamjameshandley williamjameshandley left a comment

Choose a reason for hiding this comment

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

Many thanks @DilyOng. Please press 'squash and merge'.

Copy link
Collaborator

@lukashergt lukashergt left a comment

Choose a reason for hiding this comment

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

Hi @DilyOng, just one request from me. Could we add a docstring entries for logLmax, please?

Comment on lines +8 to +9
def gaussian(nlive, ndims, sigma=0.1, R=1, logLmin=-1e-2, logLmax=0,
*args, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

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

logLmax is lacking a docstring entry.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for your comment. But logLmax needs to be a number. I'm not sure what you mean I am sorry...

Copy link
Collaborator

Choose a reason for hiding this comment

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

The docstring underneath the function definition is the text that describes what the function does such that a new user can quickly inform themselves on the correct usage. Such a function docstring should include a list of (at least) all the explicit function arguments. By numpy convention (which is the one that we are using), all these function arguments get listed underneath a "Parameters" heading. In jupyter notebooks this docstring is easily accessible by pressing shift+tab inside a function.

Here, the docstring currently is:

    """Perfect nested sampling run for a spherical Gaussian & prior.

    Up to normalisation this is identical to the example in John Skilling's
    original paper https://doi.org/10.1214/06-BA127 . Both spherical Gaussian
    width sigma and spherical uniform prior width R are centered on zero

    Parameters
    ----------
    nlive : int
        number of live points#

    ndims : int
        dimensionality of gaussian

    sigma : float
        width of gaussian likelihood

    R : float
        radius of gaussian prior

    logLmin : float
        loglikelihood at which to terminate

    The remaining arguments are passed to the
    :class:`anesthetic.samples.NestedSamples` constructor.

    Returns
    -------
    samples : :class:`anesthetic.samples.NestedSamples`
        Nested sampling run
    """

So this currently lists nlive, ndims, sigma, R, and logLmin, but it lacks a description of logLmax.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thank you very much for explaining! I've implemented your suggestion.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I was hoping for a bit more description...

Then again, the docstring for the other arguments is also very minimalistic... and possibly a bit misleading... Is this really a nested sampling run of a spherical Gaussian and prior? Or would it be more accurate to call it a nested sampling run of a spherical Gaussian likelihood from a spherical top-hat prior? In which case the docstring for R should be rather the radius of the spherical top-hat prior...

Anyhow, it's not right to ask you to clean all that up (unless you want to), but it would be nice if you could make the logLmax docstring a bit more descriptive. Something along the lines that, yes, this defines the maximum of the Gaussian likelihood, but in effect this only shifts the log-likelihood up and down by a constant. Is there a reason anyone should choose a value different from the default logLmax=0? What prompted you to introduce this argument the first place? If that is a gotcha that would be good for other people to be aware of, then this is a good place to warn about it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hi @lukashergt I am terribly sorry for my late reply, I somehow missed your newest comment.

The reason why we need to add LogLmax is because we’ve found an issue with the definition of combined Gaussian likelihood. We are recently adding a new function (in the branch called tension) to anesthetic for calculating tension statistics between datasets. The initial test involves creating 2 mock datasets A and B with d-dimensional Gaussian likelihood and a combined dataset AB (combining A and B on the likelihood level). We discovered that the quantity of logLmax is actually needed explicitly in the calculation of combined likelihoods. Besides, we have noticed that the correlated_gaussian() function does not produce a normalised correlated gaussian likelihood and the evidence isn’t unity. Therefore, we need to update the function description. Please refer to equations 14-21 in the paper Quantifying tensions in cosmological parameters: Interpreting the DES evidence ratio [1902.04029]. Thank you very much!

Comment on lines +65 to +66
def correlated_gaussian(nlive, mean, cov, bounds=None, logLmax=0,
*args, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

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

docstring entry for logLmax

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

@williamjameshandley williamjameshandley dismissed their stale review November 17, 2023 20:33

Please implement changes requested by @!ukashergt

Copy link
Collaborator

@lukashergt lukashergt left a comment

Choose a reason for hiding this comment

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

Sorry, I did not mean to halt this completely, just thought it best to document things thoroughly while the reason for changes/additions are fresh in our minds, but not at the cost of causing a standstill, so I'd rather approve this now.

@DilyOng DilyOng merged commit b93dec5 into master Feb 6, 2024
22 checks passed
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