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

Adds Monte Carlo Samplers #340

Open
wants to merge 40 commits into
base: develop
Choose a base branch
from
Open

Adds Monte Carlo Samplers #340

wants to merge 40 commits into from

Conversation

BradyPlanden
Copy link
Member

@BradyPlanden BradyPlanden commented Jun 3, 2024

Description

Adds Pints-based Monte Carlo samplers. The full list of composed samplers can be found here.

This PR includes a base sampling class BaseSampler, of which PintsBaseSampler is a child. The Pints MCMC samplers are composed of PyBOP classes, which are constructed within PintsBaseSampler. This follows a similar structure to the BasePintsOptimiser class. A generic MCMCSampler class is included for use as a default, which is conceptually aligned with the Optimisation class.

Additions to the Priors class include GaussianLogPrior, ComposedLogPrior.

Two additional PR's will be chained around this one. The order of merge should be:

Issue reference

Fixes #6

Review

Before you mark your PR as ready for review, please ensure that you've considered the following:

  • Updated the CHANGELOG.md in reverse chronological order (newest at the top) with a concise description of the changes, including the PR number.
  • Noted any breaking changes, including details on how it might impact existing functionality.

Type of change

  • New Feature: A non-breaking change that adds new functionality.
  • Optimization: A code change that improves performance.
  • Examples: A change to existing or additional examples.
  • Bug Fix: A non-breaking change that addresses an issue.
  • Documentation: Updates to documentation or new documentation for new features.
  • Refactoring: Non-functional changes that improve the codebase.
  • Style: Non-functional changes related to code style (formatting, naming, etc).
  • Testing: Additional tests to improve coverage or confirm functionality.
  • Other: (Insert description of change)

Key checklist:

  • No style issues: $ pre-commit run (or $ nox -s pre-commit) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)
  • All unit tests pass: $ nox -s tests
  • The documentation builds: $ nox -s doctest

You can run integration tests, unit tests, and doctests together at once, using $ nox -s quick.

Further checks:

  • Code is well-commented, especially in complex or unclear areas.
  • Added tests that prove my fix is effective or that my feature works.
  • Checked that coverage remains or improves, and added tests if necessary to maintain or increase coverage.

Thank you for contributing to our project! Your efforts help us to deliver great software.

@BradyPlanden BradyPlanden linked an issue Jun 3, 2024 that may be closed by this pull request
Copy link

codecov bot commented Jun 3, 2024

Codecov Report

Attention: Patch coverage is 99.26471% with 3 lines in your changes missing coverage. Please review.

Project coverage is 98.81%. Comparing base (6a6b7dd) to head (bca3bbb).
Report is 1 commits behind head on develop.

Files Patch % Lines
pybop/parameters/priors.py 98.48% 1 Missing ⚠️
pybop/samplers/base_pints_sampler.py 99.28% 1 Missing ⚠️
pybop/samplers/mcmc_sampler.py 94.11% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #340      +/-   ##
===========================================
+ Coverage    98.76%   98.81%   +0.05%     
===========================================
  Files           48       52       +4     
  Lines         3153     3557     +404     
===========================================
+ Hits          3114     3515     +401     
- Misses          39       42       +3     

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

@BradyPlanden BradyPlanden mentioned this pull request Jun 28, 2024
15 tasks
pybop/parameters/priors.py Outdated Show resolved Hide resolved
pybop/parameters/priors.py Outdated Show resolved Hide resolved
pybop/samplers/base_mcmc.py Outdated Show resolved Hide resolved
@BradyPlanden
Copy link
Member Author

This is ready for a first review. There is a bit more work to do on integration testing for the slower convergence samplers, as well as integrating #357. I believe this PR is in a good state to be integrated into develop, with subsequent PRs improving the design structure and robustness all around.

pybop/costs/_likelihoods.py Show resolved Hide resolved
pybop/parameters/priors.py Show resolved Hide resolved
pybop/parameters/priors.py Outdated Show resolved Hide resolved
"""
raise NotImplementedError

def verify(self, x):
Copy link
Member

Choose a reason for hiding this comment

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

I would not expect a dictionary to be passed here, so let's remove the verify function unless there is a particular reason for it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Given the call stack to this point, it's likely to have this as a dict, as parameters.verify(x) within cost.evaluate provides a dict for the cost.compute(). In this situation, the x dict for the prior calculation in the LogPosterior class needs to be converted from dict to ndarray for the prior methods.

I think this is probably the best spot for the conversion, as the alternative would be to individually apply the conversion to each cost.compute, or refactor the prior methods to accept dicts. Do you have a preference?

pybop/parameters/priors.py Outdated Show resolved Hide resolved
@@ -372,6 +372,12 @@ def get_sigma0(self) -> list:

return sigma0

def priors(self) -> list:
Copy link
Member

Choose a reason for hiding this comment

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

Should this be a JointPrior instead (see suggestion below)

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if I follow on this one, are you suggesting it should be renamed joint_priors or something else?

@NicolaCourtier NicolaCourtier self-requested a review August 27, 2024 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

Hamiltonian Monte Carlo (HMC)
2 participants