optimize API reference#

Parameters#

class optimize.parameters.BayesianParameter(name=None, value=None, vary=True, priors=None, scale=None, latex_str=None, unc=None)#

Bases: optimize.parameters.Parameter

A class for a Bayesian model parameter.

name#

The name of the parameter.

Type

str

value#

The current value of the parameter.

Type

str

vary#

Whether or not to vary (optimize) this parameter.

Type

bool

priors#

A list of priors to apply.

Type

list

scale#

A search scale to initiate mcmc walkers.

Type

float

latex_str#

A string for plot formatting, most likely using latex formatting.

Type

str

Creates a Bayesian Parameter object.

Parameters
  • name (str) – The name of the parameter.

  • value (float) – The starting value of the parameter.

  • vary (bool) – Whether or not to vary (optimize) this parameter.

  • priors (list) – A list of priors to apply.

  • latex_str (str) – A string for plot formatting, most likely using latex formatting.

  • scale (float) – A search scale to initiate mcmc walkers.

  • unc (tuple) – A tuple containing the uncertainty in this parameter (-, +).

add_prior(prior)#

Adds a new prior. Equivalent to self.priors.append(prior)

Parameters

prior (Prior) – The prior to add.

property hard_bounds#

Gets the hard bounds from uniform priors or Jeffreys prior if present, otherwise assumes +/- inf.

Returns

The lower bound. float: The upper bound.

Return type

float

has_prior(prior_type)#

Checks whether or not the parameter contains a certain type of prior

Parameters

prior_type (type) – The type of the prior.

Returns

True if the parameter has the prior, False otherwise.

Return type

bool

property lower_bound#

The lower bound.

Returns

The lower bound.

Return type

float

property scale#

The scale for the parameter to initiate MCMC walkers.

Returns

The parameter scale.

Return type

float

property upper_bound#

The upper bound.

Returns

The upper bound.

Return type

float

class optimize.parameters.BayesianParameters#

Bases: optimize.parameters.Parameters

property scales#

The scales for each parameter.

Returns

The scales for each parameter.

Return type

np.ndarray

class optimize.parameters.BoundedParameter(name=None, value=None, vary=True, lower_bound=- inf, upper_bound=inf, latex_str=None)#

Bases: optimize.parameters.Parameter

A class for a bounded model parameter.

name#

The name of the parameter.

Type

str

value#

The current value of the parameter.

Type

str

vary#

Whether or not to vary (optimize) this parameter.

Type

bool

lower_bound#

The lower bound.

Type

float

upper_bound#

The upper bound.

Type

float

latex_str#

A string for plot formatting, most likely using latex formatting.

Type

str

Creates a Parameter object.

Parameters
  • name (str) – The name of the parameter.

  • value (float) – The starting value of the parameter.

  • vary (bool) – Whether or not to vary (optimize) this parameter.

  • lower_bound (float) – The lower bound.

  • upper_bound (float) – The upper bound.

  • latex_str (str) – A string for plot formatting, most likely using latex formatting.

property hard_bounds#

Gets the current hard bounds.

Returns

The lower bound. float: The upper bound.

Return type

float

property in_bounds#

Determines whether or not the current parameter value is in bounds.

Returns

True if the parameter is in bounds, False if not.

Return type

bool

property out_of_bounds#

Determines whether or not the current parameter value out of bounds.

Returns

True if the parameter is out of bounds, False if not.

Return type

bool

property scale#

The scale for the parameter.

Returns

The parameter scale.

Return type

float

class optimize.parameters.BoundedParameters#

Bases: optimize.parameters.Parameters

property all_in_bounds#

Checks whether or not all parameters are in bounds (or not.)

Returns

True if the parameters are all in bounds, False otherwise.

Return type

bool

property any_out_of_bounds#

Checks whether or not any parameters are out of bounds.

Returns

True if any parameters are out of bounds, False otherwise.

Return type

bool

property hard_bounds#

The hard bounds as a tuple.

Returns

The lower bounds. np.ndarray: The upper bounds.

Return type

np.ndarray

property hard_bounds_varied#

Gets the hard bounds, but only the varied params.

Returns

The lower bounds. np.ndarray: The upper bounds.

Return type

np.ndarray

property lower_bounds#

The lower bounds.

Returns

The lower bounds.

Return type

np.ndarray

property num_in_bounds#

The number of parameters in bounds.

Returns

The number of parameters in bounds.

Return type

int

property num_out_of_bounds#

The number of parameters out of bounds.

Returns

The number of parameters out of bounds.

Return type

int

property upper_bounds#

The upper bounds.

Returns

The upper bounds.

Return type

np.ndarray

class optimize.parameters.Parameter(name=None, value=None, vary=True, latex_str=None)#

Bases: object

A base class for a scalar model parameter.

name#

The name of the parameter.

Type

str

value#

The current value of the parameter.

Type

str

vary#

Whether or not to vary this parameter.

Type

bool

latex_str#

A string for plot formatting, most likely using latex math-mode formatting $$.

Type

str

Creates a Parameter object.

Parameters
  • name (str) – The name of the parameter.

  • value (float) – The starting value of the parameter.

  • vary (bool) – Whether or not to vary (optimize) this parameter.

  • latex_str (str) – A string for latex formatting in math mode.

set_name(name)#

Sets the name for the parameter. Also sets the latex string of not already set.

Parameters

name (str) – The parameter name.

property value_str#

The current value of the parameter as a string

Returns

The value as a string

Return type

str

class optimize.parameters.Parameters#

Bases: dict

A container for a set of model parameters which extends the Python 3 dictionary, which is ordered by default.

add_par(par)#

Adds a parameter to the Parameters dictionary with par.name as a key.

Parameters

par (Parameter) – The parameter to add.

get_locked()#

Gets the locked parameters in a new parameters object

Returns

A parameters object containing pointers to only the locked parameters.

Return type

Parameters

get_varied()#

Gets the varied parameters in a new parameters object

Returns

A parameters object containing pointers to only the varied parameters.

Return type

Parameters

get_view(par_names)#

Gets a subspace of parameter objects.

Parameters

pars (iterable of str) – An iterable of string objects (the names of the parameters to fetch).

Returns

A parameters object containing pointers to the desired parameters. The class will be the same as the current instance.

Return type

type(self)

index_from_par(name, rel_vary=False)#

Gets the index of a given parameter name.

Parameters
  • name (str) – The name of the parameter.

  • rel_vary (bool, optional) – Whether or not to return an index which is relative to all parameters or only varied parameters. Defaults to False.

Returns

The numerical index of the parameter.

Return type

int

property num_locked#

The number of locked parameters.

Returns

The number of locked parameters.

Return type

int

property num_varied#

The number of varied parameters.

Returns

The number of varied parameters.

Return type

int

par_from_index(k, rel_vary=False)#

Gets the parameter at a given numerical index.

Parameters
  • k (int) – The numerical index.

  • rel_vary (bool, optional) – Whether or not this index is relative to all parameters or only varied parameters. Defaults to False.

Returns

The parameter at the given index.

Return type

Parameter

set_vec(x, attr, varied=False)#

Sets an attribute for all parameters given a vector.

Parameters
  • x (np.ndarray) – The vector to set.

  • attr (str) – The attribute to set for each parameter.

  • varied (bool, optional) – Whether or not to only set the varied parameters. Defaults to False.

unpack(keys=None, vary_only=False)#

Unpacks values to a dict of numpy arrays.

Parameters

keys (iterable or string) – A tuple of strings containing the keys to unpack, defaults to None for all keys.

Returns

A dictionary containing the numpy arrays.

Return type

dict

property varied_inds#

The indices of the varied parameters.

Returns

The indices of the varied parameters.

Return type

np.dnarray

class optimize.priors.Gaussian(mu, sigma)#

Bases: optimize.priors.Prior

A prior defined by a normal distribution.

mu#

The center of the distribution.

Type

float

sigma#

The stddev. of the distribution.

Type

float

Constructor for a Gaussian prior.

Parameters
  • mu (float) – The center of the distribution.

  • sigma (float) – The stddev. of the distribution.

logprob(x)#

Computes the log probability given the current parameter value, x.

Parameters

x (float) – The current parameter value.

Returns

The logarithm of the probability distribution for this value.

Return type

float

class optimize.priors.JeffreysSG(lower_bound, upper_bound, knee=0)#

Bases: optimize.priors.Prior

A Jeffrey’s prior for the stddev of a Gaussian likelihood, proportional to 1 / x.

lower_bound#

The lower bound.

Type

float

upper_bound#

The upper bound.

Type

float

Constructs a Jeffrey’s prior for a Gaussian likelihood.

Parameters
  • lower_bound (float) – The lower bound of the prior.

  • upper_bound (float) – The upper bound of the prior.

  • knee (int, optional) – The knee of the distribution (x0) The distribution is proportional to 1 / (x - x0). Defaults to 0.

logprob(x)#

Computes the log probability given the current parameter value, x.

Parameters

x (float) – The current parameter value.

Returns

The logarithm of the probability distribution for this value.

Return type

float

class optimize.priors.Negative#

Bases: optimize.priors.Prior

A prior to force x < 0.

Constructs a negative prior.

logprob(x)#

Computes the log probability given the current parameter value, x.

Parameters

x (float) – The current parameter value.

Returns

The logarithm of the probability distribution for this value.

Return type

float

class optimize.priors.Positive#

Bases: optimize.priors.Prior

A prior to force x > 0.

Constructs a positive prior.

logprob(x)#

Computes the log probability given the current parameter value, x.

Parameters

x (float) – The current parameter value.

Returns

The logarithm of the probability distribution for this value.

Return type

float

class optimize.priors.Prior#

Bases: object

An interface for a general Bayesian prior.

logprob(x)#

Computes the logprobability given the current parameter value, x.

Parameters

x (float) – The current parameter value.

Returns

The logarithm of the probability distribution for this value.

Return type

float

class optimize.priors.Uniform(lower_bound, upper_bound)#

Bases: optimize.priors.Prior

A prior defined by hard bounds.

lower_bound#

The lower bound.

Type

float

upper_bound#

The upper bound.

Type

float

Constructor for a Uniform prior.

Parameters
  • lower_bound (float) – The lower bound.

  • upper_bound (float) – The upper bound.

logprob(x)#

Computes the log probability given the current parameter value, x.

Parameters

x (float) – The current parameter value.

Returns

The logarithm of the probability distribution for this value.

Return type

float