bayesbay.prior.UniformPrior

class bayesbay.prior.UniformPrior(name, vmin, vmax, perturb_std, perturb_std_birth=None, position=None)

Class for defining the prior probability of a free parameter distributed according to a uniform probability distribution

Parameters:
  • name (str) – name of the parameter, for display and storing purposes

  • vmin (Union[Number, np.ndarray]) – the lower bound for this parameter. This can either be a scalar or an array if the defined probability distribution is a function of position in the discretization domain

  • vmax (Union[Number, np.ndarray]) – the upper bound for this parameter. This can either be a scalar or an array if the defined probability distribution is a function of position in the discretization domain

  • perturb_std (Union[Number, np.ndarray]) – standard deviation of the Gaussians used to randomly perturb the parameter. This can either be a scalar or an array if the defined probability distribution is a function of position in the discretization domain

  • position (np.ndarray, optional) – position in the discretization domain, used to define a position-dependent probability distribution. None by default

Reference Details

name
add_hyper_params(hyper_params)

Sets the attributes from the given dict and checks for errors

Parameters:

hyper_params (Dict[str, Union[Number, np.ndarray]]) – dictionary of attributes to be set

get_delta(position=None)

get the range \(\Delta v = v_{max} - v_{min}\), which may be dependent on the specified position in the discretization domain

Parameters:

position (Union[Number, np.ndarray], optional) – the position in the discretization domain at which the uniform distribution range will be returned. None by default

Returns:

the range \(\Delta v = v_{max} - v_{min}\)

Return type:

Number

get_hyper_param(hyper_param, position=None)

Retrieves the value corresponding to the specified attribute, which may be a function of position

Parameters:
  • hyper_param (str) – the name of the attribute

  • position (Union[np.ndarray, Number], optional) – the position (in the discretization domain) associated with the value, None by default

Returns:

value corresponding to the specified attribute

Return type:

Union[Number, np.ndarray]

get_perturb_std(position=None)

get the standard deviation of the Gaussian used to perturb the parameter, which may be dependent on the position in the discretization domain

Parameters:

position (Union[Number, np.ndarray], optional) – the position in the discretization domain at which the standard deviation of the Gaussian used to perturb the parameter is returned. Default is None

Returns:

standard deviation of the Gaussian used to perturb the parameter, possibly at the specified position

Return type:

Number

get_perturb_std_birth(position=None)

get the standard deviation of the Gaussian used to perturb the parameter at birth, which may be dependent on the position in the discretization domain

Parameters:

position (Union[Number, np.ndarray], optional) – the position in the discretization domain at which the standard deviation of the Gaussian used to perturb the parameter at birth is returned. Default is None

Returns:

standard deviation of the Gaussian used to perturb the parameter at birth, possibly at the specified position

Return type:

Number

get_vmin_vmax(position=None)

get the lower and upper bounds of the parameter, which may be dependent on position in the discretization domain

Parameters:

position (Union[Number, np.ndarray], optional) – the position in the discretization domain at which the the lower and upper bounds of the parameter will be returned. None by default

Returns:

the lower (vmin) and upper (vmax)

Return type:

Tuple[Number, Number]

has_hyper_param(hyper_param)

Whether or not the Prior instance has the specified attribute

Parameters:

hyper_param (str) – the name of the attribute

initialize(positions=None)

initializes the values of this (possibly position-dependent) parameter

This is the vectorized version of the method sample().

Parameters:

positions (np.ndarray, optional) – the position (in the discretization domain) associated with the value, None by default

Returns:

an array of values or one value corresponding to the prior probability (at the given positions)

Return type:

np.ndarray

log_prior(value, position=None)

calculates the log of the prior probability density for the given value, which may be dependent on the position in the discretization domain

Parameters:
  • value (Number) – the value to calculate the probability density for

  • position (Union[Number, np.ndarray], optional) – the position in the discretization domain at which the prior probability of the parameter value is to be retrieved

Returns:

the log of the prior probability \(p(v) = \frac{1}{\Delta v}\), where \(v\) denotes the value passed by the user and \(\Delta v = v_{max} - v_{min}\) the range within which the uniform parameter is allowed to vary. This may be dependent on position in the discretization domain

Return type:

Number

perturb_value(value, position=None, is_birth=False)

perturbs the given value, in a way that may depend on the position in the discretization domain and calculates the log of the corresponding partial acceptance probability,

\[\begin{split}\underbrace{\alpha_{p}}_{\begin{array}{c} \text{Partial} \\ \text{acceptance} \\ \text{probability} \end{array}} = \underbrace{\frac{p\left({\bf m'}\right)}{p\left({\bf m}\right)}}_{\text{Prior ratio}} \underbrace{\frac{q\left({\bf m} \mid {\bf m'}\right)}{q\left({\bf m'} \mid {\bf m}\right)}}_{\text{Proposal ratio}} \underbrace{\lvert \mathbf{J} \rvert}_{\begin{array}{c} \text{Jacobian} \\ \text{determinant} \end{array}},\end{split}\]

which in this case equals zero. In the above equation, \(\bf m'\) denotes the perturbed model as obtained through a random deviate from a normal distribution \(\mathcal{N}(v, \sigma)\), where \(v\) denotes the original value and \(\sigma\) the standard deviation of the Gaussian used for the perturbation. \(\sigma\) may be dependent on the specified position (UniformPrior.perturb_std).

Parameters:
  • value (Number) – the current value to be perturbed from

  • position (Union[Number, np.ndarray], optional) – the position in the discretization domain at which the parameter value is to be perturbed

  • is_birth (bool, optional) – whether the perturbation is a birth or not, False by default

Returns:

the perturbed value and \(\alpha_{p} = \log( \frac{p({\bf m'})}{p({\bf m})} \frac{q\left({\bf m} \mid {\bf m'}\right)}{q\left({\bf m'} \mid {\bf m}\right)} \lvert \mathbf{J} \rvert) = 0\)

Return type:

Tuple[Number, Number]

sample(position=None)

sample a new value from the prior

Paramters

positionUnion[np.ndarray, Number], optional

the position (in the discretization domain) associated with the value, None by default

returns:

a value corresponding to the prior probability (at the given position)

rtype:

Number

set_custom_initialize(initialize_func)

sets a custom initialization function

Parameters:

initialize_func (Callable[["Prior", np.ndarray], np.ndarray]) – The function to use for initialization. This function should take a Prior instance and optionally an array of positions as input arguments, and produce an array of values as output.

Examples

def my_init(
    param: bb.prior.Prior,
    position: np.ndarray
) -> np.ndarray:
    print("This is my custom init!")
    return np.ones(len(position))

my_param.set_custom_initialize(my_init)

back to top