bayesbay.prior.CustomPrior
- class bayesbay.prior.CustomPrior(name, log_prior, sample, perturb_std, perturb_std_birth=None, position=None)
Class enabling the definition of an arbitrary prior for a free parameter
- Parameters:
name (str) – name of the current parameter, for display and storing purposes
log_prior (Callable[[Number, Number], Number])
sample (Callable[[Number], Number]) – a function that samples a new value (optionally dependent on a position). The parameter initialization will also be done by calling this function
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 domainposition (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_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
- 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 prior probability density
- 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}\]where \(\bf m'\) denotes the perturbed model as obtained through a random deviate from a normal distribution \(\mathcal{N}(v, \theta)\), where \(v\) denotes the original
value
and \(\theta\) the standard deviation of the Gaussian used for the perturbation. \(\theta\) may be dependent on the specified position in the discretization domain (CustomPrior.perturb_std
).- Parameters:
value (Number) – the current value to be perturbed from
position (Number, optional) – the position of the value 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)\)
- 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)