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)
static get_path_to_node(node)

Return the path from the root to the node.

Parameters:node (OptNode) –
Returns:
Return type:list of OptNode
get_transformation_dict(node)

Return a dictionary of parameters from the path walked from the root to the passed node.

Parameters:node (OptNode) –
Returns:
Return type:dict
class espei.optimizers.graph.OptNode(parameters, datasets, node_id=None)

Bases: object

Node as the result of an optimization.

parameters
Type:dict
datasets
Type:PickleableTinyDB
id
Type:int
parent
Type:OptNode
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
Returns:

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, 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.
Returns:

Return type:

ndarray

static predict(params, **ctx)

Calculate lnprob = lnlike + lnprior

save_sampler_state()

Convenience function that saves the trace and lnprob. 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
Returns:

Return type:

float

espei.optimizers.utils module

exception espei.optimizers.utils.OptimizerError

Bases: BaseException

Module contents