-
Notifications
You must be signed in to change notification settings - Fork 56
Adds Monte Carlo Samplers #340
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
Merged
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
89ae3e6
feat: initial Monte Carlo classes
BradyPlanden 3c48f88
feat: updt __init__.py, add LogPosterior
BradyPlanden a62a126
tests: add unit tests for MCMC samplers
BradyPlanden 90bb173
Merge branch 'develop' into monte-carlo-methods
BradyPlanden fbe56a4
fix parallel for windows
BradyPlanden 8f74b6d
tests: additional unit tests, refactors priors class
BradyPlanden 2d83315
tests: increase coverage, adds monte carlo integration test
BradyPlanden 5ed3d23
tests: increase coverage, bugfix multi_log_pdf logic
BradyPlanden ca961a2
tests: increase coverage, update priors on intesampling integration t…
BradyPlanden da21506
tests: increment coverage, refactor prior np.inf catch
BradyPlanden ce1cb54
refactor: removes redundant code
BradyPlanden c86531b
Merge branch 'develop', updts for Parameters class
BradyPlanden 3e4c01e
refactor: adds improvements from parameters class
BradyPlanden b5ec8fe
feat: Adds burn-in functionality for sampling class
BradyPlanden f71bf6a
Merge branch 'develop' into monte-carlo-methods
BradyPlanden 1f7c6cb
Merge branch 'develop' into monte-carlo-methods
BradyPlanden c8db9f5
fix: correct sigma0 to cov0
BradyPlanden eaaebb2
Merge branch 'develop' into monte-carlo-methods
BradyPlanden fb97b5d
refactor: move general methods into parent class, replace burn_in wit…
BradyPlanden 942dc5e
Merge branch 'develop' into monte-carlo-methods
BradyPlanden 990c590
Apply suggestions from code review
BradyPlanden 5f89231
Merge branch 'develop' into monte-carlo-methods
BradyPlanden 74b1f30
Merge branch 'develop' into monte-carlo-methods
BradyPlanden 13aa83f
refactor: log_pdf to base class, update docstrings.
BradyPlanden 0b32889
Adds catches and initialisation for x0, update tests
BradyPlanden 0117066
Merge branch 'refs/heads/develop' into monte-carlo-methods
BradyPlanden 251f86f
feat: updates for transformation class, cleanup
BradyPlanden 5bb4d94
fix: uniformly apply bound transformations, update LogPosterior
BradyPlanden a5244a4
Merge branch 'refs/heads/develop' into monte-carlo-methods
BradyPlanden 255aa5d
Merge branch 'refs/heads/develop' into monte-carlo-methods
BradyPlanden c9946da
Merge branch 'refs/heads/develop' into monte-carlo-methods
BradyPlanden cd07072
refactor: ComposedLogPrior -> JointLogPrior, prior.evaluateS1 -> logp…
BradyPlanden 08dc407
fix: update tests for low convergence sampler
BradyPlanden c225065
refactor: update priors, refactor JointLogPrior
BradyPlanden 4df0885
tests: update unit tests and increase coverage.
BradyPlanden e50812a
refactor: base_sampler init, update docstrings, update tests, remove …
BradyPlanden 7a000cf
tests: increase coverage, remove redundant ValueError, sampler.chains…
BradyPlanden 711dcc8
tests: restore parallel optimisation with thread limit to 1
BradyPlanden df1cc73
Merge branch 'refs/heads/develop' into monte-carlo-methods
BradyPlanden bca3bbb
Merge branch 'refs/heads/develop' into monte-carlo-methods
BradyPlanden 248b161
Merge branch 'refs/heads/develop' into monte-carlo-methods
BradyPlanden 503af19
Refactor and bugfixes. Adds gradient-based integration sampling tests…
BradyPlanden 85e1ce1
Remainder review suggestions, update assert tolerances, small array d…
BradyPlanden 8a928af
tests: increment iterations from scheduled test run
BradyPlanden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import numpy as np | ||
| import plotly.graph_objects as go | ||
| import pybamm | ||
|
|
||
| import pybop | ||
|
|
||
| # Parameter set and model definition | ||
| solver = pybamm.IDAKLUSolver() | ||
| parameter_set = pybop.ParameterSet.pybamm("Chen2020") | ||
| parameter_set.update( | ||
| { | ||
| "Negative electrode active material volume fraction": 0.63, | ||
| "Positive electrode active material volume fraction": 0.71, | ||
| } | ||
| ) | ||
| synth_model = pybop.lithium_ion.DFN(parameter_set=parameter_set, solver=solver) | ||
|
|
||
| # Fitting parameters | ||
| parameters = pybop.Parameters( | ||
| pybop.Parameter( | ||
| "Negative electrode active material volume fraction", | ||
| prior=pybop.Gaussian(0.68, 0.02), | ||
| transformation=pybop.LogTransformation(), | ||
| ), | ||
| pybop.Parameter( | ||
| "Positive electrode active material volume fraction", | ||
| prior=pybop.Gaussian(0.65, 0.02), | ||
| transformation=pybop.LogTransformation(), | ||
| ), | ||
| ) | ||
|
|
||
| # Generate data | ||
| init_soc = 0.5 | ||
| sigma = 0.002 | ||
| experiment = pybop.Experiment( | ||
| [ | ||
| ("Discharge at 0.5C for 6 minutes (5 second period)",), | ||
| ] | ||
| ) | ||
| values = synth_model.predict( | ||
| initial_state={"Initial SoC": init_soc}, experiment=experiment | ||
| ) | ||
|
|
||
|
|
||
| def noise(sigma): | ||
| return np.random.normal(0, sigma, len(values["Voltage [V]"].data)) | ||
|
|
||
|
|
||
| # Form dataset | ||
| dataset = pybop.Dataset( | ||
| { | ||
| "Time [s]": values["Time [s]"].data, | ||
| "Current function [A]": values["Current [A]"].data, | ||
| "Voltage [V]": values["Voltage [V]"].data + noise(sigma), | ||
| "Bulk open-circuit voltage [V]": values["Bulk open-circuit voltage [V]"].data | ||
| + noise(sigma), | ||
| } | ||
| ) | ||
|
|
||
| model = pybop.lithium_ion.SPM(parameter_set=parameter_set, solver=pybamm.IDAKLUSolver()) | ||
| model.build(initial_state={"Initial SoC": init_soc}) | ||
| signal = ["Voltage [V]", "Bulk open-circuit voltage [V]"] | ||
|
|
||
| # Generate problem, likelihood, and sampler | ||
| problem = pybop.FittingProblem(model, parameters, dataset, signal=signal) | ||
| likelihood = pybop.GaussianLogLikelihoodKnownSigma(problem, sigma0=0.002) | ||
| posterior = pybop.LogPosterior(likelihood) | ||
|
|
||
| optim = pybop.DifferentialEvolutionMCMC( | ||
| posterior, | ||
| chains=3, | ||
| max_iterations=300, | ||
| warm_up=100, | ||
| verbose=True, | ||
| # parallel=True, # uncomment to enable parallelisation (MacOS/WSL/Linux only) | ||
| ) | ||
| result = optim.run() | ||
|
|
||
| # Create a histogram | ||
| fig = go.Figure() | ||
| for _i, data in enumerate(result): | ||
| fig.add_trace(go.Histogram(x=data[:, 0], name="Neg", opacity=0.75)) | ||
| fig.add_trace(go.Histogram(x=data[:, 1], name="Pos", opacity=0.75)) | ||
|
|
||
| # Update layout for better visualization | ||
| fig.update_layout( | ||
| title="Posterior distribution of volume fractions", | ||
| xaxis_title="Value", | ||
| yaxis_title="Count", | ||
| barmode="overlay", | ||
| ) | ||
|
|
||
| # Show the plot | ||
| fig.show() |
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.