| | |
- builtins.object
-
- Optimizer
class Optimizer(builtins.object) |
| |
Optimizer(simulate_object, cost='MSE', cfunc=None, param_evolution_func=None, param_evolution_func_extra_vary_params=None, custom_model_y_func=None)
An object which will take in a Simulate, Model and Data object in order to
fit the model to the data with various methods.
Parameters
----------
simulate_object : multilayerpy.simulate.Simulate
A Simulate object created in the model building process.
cost : str, optional
The cost function to use in the optimisation process.
Current option is mean-squared error 'MSE'.
cfunc : func, optional
A function which takes in data (in the Data object format) and model_y
values supplied to the cost_func method.
Returns the value of the cost function. Lower values = better fit.
param_evolution_func : func, optional
A function which is called f(t,y,param_dict,param_evolution_func_extra_vary_params).
This is taken from the supplied Simulate object.
It returns the param_dict and allows for model parameters to evolve
over time, values of y (array of ODE solutions evaluated at time t).
Parameters can be parameterised with extra parameters which themselves can
vary. See param_evolution_func_extra_vary_params below.
param_evolution_func_extra_vary_params : list, optional
List of Parameter objects which represent additional parameters used
to evolve/parameterise model input parameters. These values can be optimised
during model optimisation. Supplied to the param_evolution_func function.
This is taken from the supplied Simulate object. |
| |
Methods defined here:
- __init__(self, simulate_object, cost='MSE', cfunc=None, param_evolution_func=None, param_evolution_func_extra_vary_params=None, custom_model_y_func=None)
- Initialize self. See help(type(self)) for accurate signature.
- cost_func(self, model_y, weighted=False)
- Will calculate the cost function used in the optimisation process.
A custom cost function will be used if suppled via the cfunc attribute
of the Optimizer object.
Parameters
----------
model_y : np.ndarray
Model y values.
Returns
-------
float
Cost function value.
- fit(self, weighted=False, method='least_squares', component_no='1', n_workers=1, popsize=15)
- Use either a local or global optimisation algorithm to fit the model
to the data.
Parameters
----------
weighted : bool, optional
Whether to weight the fit to datapoint uncertainties (requires the data to have uncertainties)
method : str, optional
Algorithm to be used. 'least_squares' (local) or 'differential_evolution' (global).
The default is 'least_squares' Nelder-Mead (simplex).
component_no : int, optional
The model component to fit to the data. The default is '1'.
n_workers : int, optional
Number of cores to use (only for 'differential_evolution' method).
The default is 1.
popsize : int, optional
The multiplyer used by the differential_evolution implementation in SciPy.
The total population size of parameter sets in the algorithm is len(varying_parameters) * popsize.
returns
----------
optimize_result : scipy.optimize.OptimizeResult
The optimization result represented as a OptimizeResult object.
Important attributes are: x the solution array, success a Boolean
flag indicating if the optimizer exited successfully and message
which describes the cause of the termination.
See the documentationfor more details:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.differential_evolution.html
- get_chain_outputs(self, n_samples='all', parallel=False, component_number=1, n_burn=0, thin=1, override_stop_run=False)
- Runs the model with parameters randomly sampled from the MCMC chains created during the MCMC sampling procedure.
Outputs are returned as a numpy array.
Parameters
----------
n_samples : int or 'all', optional
The number of random samples to take from the MCMC chains. Default is 'all' of the samples.
parallel : bool, optional
Run each sample in parallel across all processors.
WARNING: if a parameter evolution function is used by the Simulate object, set this to False. Otherwise the sampling freezes.
component_number : int, optional
The component number of the model component to be used as the model output.
n_burn : int, optional
The number of initial steps to burn (discard) in the MCMC chain.
thin : int, optional
Use every 'thin' number of samples along the MCMC chains (called thinning).
override_stop_run : bool, optional
Overrides stopping running this function if there is already a set of model runs sampled from the MCMC procedure.
Returns
-------
xy_array_output : list
A list of model xy outputs (first column = time, second column = model output).
- lnlike(self, vary_params)
- Calculates the log-likelihood of the model-data fit for MCMC sampling.
Parameters
----------
vary_params : np.ndarray
An array of the varying model parameters.
Returns
-------
loglike : float
The log-likelihood of the model fit.
- lnprior(self, vary_params)
- Calculate the log-prior of the parameter set for MCMC sampling.
Parameters
----------
vary_params : np.ndarray
An array of the varying model parameters.
Returns
-------
float
The log-prior probability of the selected parameter set.
- lnprob(self, vary_params)
- Calculate the log-probability for a parameter set, considering the priors.
Parameters
----------
vary_params : np.ndarray
An array of the varying model parameters..
Returns
-------
float
The log-probability for the model-data system, considering priors.
- plot_chain_outputs(self)
- Plots the experimental data with n_samples number of model runs from MCMC sampling.
Requires that a sample of model outputs from the MCMC chains has been taken using Optimizer.get_chain_outputs.
Returns
-------
Plot of experimental data with normalised model output vs time for a number of MCMC samples.
- plot_chains(self, discard=0, thin=1)
- Plots the MCMC chains saved during the sampling run. Each plot is the
parameter value at each sampling iteration.
Parameters
----------
discard : int, optional
The number of setps to burn-in (discard) from the chains before plotting.
thin : int, optional
Only take every 'thin' number of samples from the chain (called thinning).
Returns
-------
A multi-panel plot of parameter values vs step number
- sample(self, samples, n_walkers=100, n_burn=0, pool=None, **kwargs)
- Runs the MCMC sampling procedure using the emcee package.
Parameters
----------
samples : int
The number of MCMC samples (steps) to take.
n_walkers : int, optional
The number of walkers to initialise for MCMC sampling.
n_burn : int, optional
Number of burn-in steps before the production MCMC run.
pool : optional
An object with a map method that follows the same calling sequence
as the built-in map function. This is generally used to compute the
log-probabilities for the ensemble in parallel.
Further details about the emcee EnsembleSampler object are available
in its documentation: https://emcee.readthedocs.io/en/stable/user/sampler/
Returns
-------
sampler : emcee.EnsembleSampler
EnsembleSampler object.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
| |