espei.optimizers package#
Submodules#
espei.optimizers.graph module#
Defines a OptNode and OptGraph to be used by OptimizerBase subclasses. Together they define the path of one or more optimizations and can be used to store and replay optimization history.
- class espei.optimizers.graph.OptGraph(root)#
Bases:
object
Directed acyclic graph of optimal parameters.
Notes
The OptGraph defines a directed acyclic graph of commits. Each commit corresponds to a single OptNode. The root node is intended to be the fresh parameters from the database before any optimization. Therefore, any path from the root node to any other node represents a set of optimizations to the parameters in the database.
- add_node(node, parent)#
- class espei.optimizers.graph.OptNode(parameters, datasets, node_id=None)#
Bases:
object
Node as the result of an optimization.
- parameters#
- Type
dict
- datasets#
- Type
- id#
- Type
int
- children#
- Type
list of OptNode
Notes
OptNodes are individual nodes in the graph that correspond to the result of a call to fit - they represent optimized parameters given the parent state and some data (also part of the OptNode).
Each OptNode can only be derived from one set of parameters, however one parameter state may be a branching point to many new parameter states, so an OptNode can have only one parent, but many children.
espei.optimizers.opt_base module#
- class espei.optimizers.opt_base.OptimizerBase(dbf)#
Bases:
object
Enable fitting and replaying fitting steps
- commit()#
- discard()#
Discard all staged nodes
- fit(symbols, datasets, *args, **kwargs)#
- static predict(params, context)#
Given a set of parameters and a context, return the resulting sum of square error.
- Parameters
params (list) – 1 dimensional array of parameters
context (dict) – Dictionary of arguments/keyword arguments to pass to functions
- Return type
float
- reset_database()#
Set the Database to the state of the current node
espei.optimizers.opt_mcmc module#
- class espei.optimizers.opt_mcmc.EmceeOptimizer(dbf, phase_models=None, scheduler=None)#
Bases:
espei.optimizers.opt_base.OptimizerBase
An optimizer using an EnsembleSampler based on Goodman and Weare [1] implemented in emcee [2]
- scheduler#
An object implementing a map function
- Type
mappable
- save_interval#
Interval of iterations to save the tracefile and probfile.
- Type
int
- tracefile#
Filename to store the trace with NumPy.save. Array has shape (chains, iterations, parameters). Defaults to None.
- Type
str
- probfile#
filename to store the log probability with NumPy.save. Has shape (chains, iterations)
- Type
str
References
[1] Goodman and Weare, Ensemble Samplers with Affine Invariance. Commun. Appl. Math. Comput. Sci. 5, 65-80 (2010). [2] Foreman-Mackey, Hogg, Lang, Goodman, emcee: The MCMC Hammer. Publ. Astron. Soc. Pac. 125, 306-312 (2013).
- do_sampling(chains, iterations)#
- static get_priors(prior, symbols, params)#
Build priors for a particular set of fitting symbols and initial parameters. Returns a dict that should be used to update the context.
- Parameters
prior (dict or PriorSpec or None) – Prior to initialize. See the docs on
symbols (list of str) – List of symbols that will be fit
params (list of float) – List of parameter values corresponding to the symbols. These should be the initial parameters that the priors will be based off of.
- static initialize_chains_from_trace(restart_trace)#
- static initialize_new_chains(params, chains_per_parameter, std_deviation, deterministic=True)#
Return an array of num_samples from a Gaussian distribution about each parameter.
- Parameters
params (ndarray) – 1D array of initial parameters that will be the mean of the distribution.
num_samples (int) – Number of chains to initialize.
chains_per_parameter (int) – number of chains for each parameter. Must be an even integer greater or equal to 2. Defaults to 2.
std_deviation (float) – Fractional standard deviation of the parameters to use for initialization.
deterministic (bool) – True if the parameters should be generated deterministically.
- Return type
ndarray
Notes
Parameters are sampled from
normal(loc=param, scale=param*std_deviation)
. A parameter of zero will produce a standard deviation of zero and therefore only zeros will be sampled. This will break emcee’s StretchMove for this parameter and only zeros will be selected.
- static predict(params, **ctx)#
Calculate lnprob = lnlike + lnprior
- save_sampler_state()#
Convenience function that saves the trace and lnprob if they haven’t been set to None by the user.
Requires that the sampler attribute be set.
espei.optimizers.opt_scipy module#
- class espei.optimizers.opt_scipy.SciPyOptimizer(dbf)#
Bases:
espei.optimizers.opt_base.OptimizerBase
- static predict(params, ctx)#
Given a set of parameters and a context, return the resulting sum of square error.
- Parameters
params (list) – 1 dimensional array of parameters
context (dict) – Dictionary of arguments/keyword arguments to pass to functions
- Return type
float
espei.optimizers.utils module#
- exception espei.optimizers.utils.OptimizerError#
Bases:
BaseException