bayesbay.prior.GaussianPrior
- class bayesbay.prior.GaussianPrior(name, mean, std, perturb_std, perturb_std_birth=None, position=None)
Class for defining the prior probability of a free parameter distributed according to a Gaussian distribution, \(\mathcal{N}(\mu, \sigma)\), where \(\mu\) denotes the mean and \(\sigma\) the standard deviation
- Parameters:
name (str) – name of the parameter, for display and storing purposes
mean (Union[Number, np.ndarray]) – mean of the Gaussian. This can either be a scalar or an array if the defined probability distribution is a function of
position
in the discretization domainstd (Union[Number, np.ndarray]) – standard deviation of the Gaussian. This can either be a scalar or an array if the defined probability distribution is a function of
position
in the discretization domainperturb_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_mean(position=None)
get the mean of the Gaussian parameter, which may be dependent on position in the discretization domain
- Parameters:
position (Union[Number, np.ndarray], optional) – position in the discretization domain at which the mean of the Gaussian will be returned. None by default
- Returns:
mean at the given position
- Return type:
Number
- 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_std(position=None)
get the standard deviation of the Gaussian, which may be dependent on position in the discretization domain
- Parameters:
position (Union[Number, np.ndarray], optional) – position in the discretization domain at which the standard deviation of the Gaussian will be returned. None by default
- Returns:
standard deviation at the given 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)
initialize the parameter, possibly at specific positions in the discretization domain
- Parameters:
positions (np.ndarray, optional) – the positions in the discretization domain at which the parameter is initialized. None by default
- Returns:
an array of values or one value corresponding to the given positions, chosen according to the normal distribution defined by
mean
andstd
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}{\sigma \sqrt{2 \pi}} \exp \Big \lbrace -\frac{\left( v - \mu \right)^2} {2\sigma^2} \Big \rbrace\), where \(\mu\) and \(\sigma\) denote the (prior) mean and standard deviation of the Gaussian and \(v\) the
value
at which the prior probability is to be retrieved. \(\mu\) and \(\sigma\) 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, 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 (GaussianPrior.perturb_std
).- Parameters:
value (Number) – the current value to be perturbed from
position (Number) – 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)