-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from pybop-team/248-add-maximum-a-posteriori
Add Maximum a Posteriori (MAP)
- Loading branch information
Showing
7 changed files
with
194 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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,71 @@ | ||
import numpy as np | ||
|
||
import pybop | ||
|
||
# Define model | ||
parameter_set = pybop.ParameterSet.pybamm("Chen2020") | ||
model = pybop.lithium_ion.SPM(parameter_set=parameter_set) | ||
|
||
# Fitting parameters | ||
parameters = [ | ||
pybop.Parameter( | ||
"Negative electrode active material volume fraction", | ||
prior=pybop.Gaussian(0.6, 0.05), | ||
bounds=[0.5, 0.8], | ||
), | ||
pybop.Parameter( | ||
"Positive electrode active material volume fraction", | ||
prior=pybop.Gaussian(0.48, 0.05), | ||
bounds=[0.4, 0.7], | ||
), | ||
] | ||
|
||
# Set initial parameter values | ||
parameter_set.update( | ||
{ | ||
"Negative electrode active material volume fraction": 0.63, | ||
"Positive electrode active material volume fraction": 0.51, | ||
} | ||
) | ||
# Generate data | ||
sigma = 0.005 | ||
t_eval = np.arange(0, 900, 2) | ||
values = model.predict(t_eval=t_eval) | ||
corrupt_values = values["Voltage [V]"].data + np.random.normal(0, sigma, len(t_eval)) | ||
|
||
# Form dataset | ||
dataset = pybop.Dataset( | ||
{ | ||
"Time [s]": t_eval, | ||
"Current function [A]": values["Current [A]"].data, | ||
"Voltage [V]": corrupt_values, | ||
} | ||
) | ||
|
||
# Generate problem, cost function, and optimisation class | ||
problem = pybop.FittingProblem(model, parameters, dataset) | ||
cost = pybop.MAP(problem, pybop.GaussianLogLikelihoodKnownSigma) | ||
optim = pybop.Optimisation(cost, optimiser=pybop.CMAES) | ||
optim.set_max_unchanged_iterations(20) | ||
optim.set_min_iterations(20) | ||
optim.set_max_iterations(100) | ||
|
||
# Run the optimisation | ||
x, final_cost = optim.run() | ||
print("Estimated parameters:", x) | ||
|
||
# Plot the timeseries output | ||
pybop.quick_plot(problem, parameter_values=x[0:2], title="Optimised Comparison") | ||
|
||
# Plot convergence | ||
pybop.plot_convergence(optim) | ||
|
||
# Plot the parameter traces | ||
pybop.plot_parameters(optim) | ||
|
||
# Plot the cost landscape | ||
pybop.plot2d(cost, steps=15) | ||
|
||
# Plot the cost landscape with optimisation path | ||
bounds = np.array([[0.55, 0.77], [0.48, 0.68]]) | ||
pybop.plot2d(optim, bounds=bounds, steps=15) |
This file contains 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 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 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 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