beetroots.modelling.likelihoods package
Submodules
beetroots.modelling.likelihoods.abstract_likelihood module
- class beetroots.modelling.likelihoods.abstract_likelihood.Likelihood(forward_map, D: int, L: int, N: int, y: ndarray)[source]
Bases:
ABCAbstract Base Class for a probability distribution on non-countable set
- evaluate_all_forward_map(Theta: ndarray, compute_derivatives: bool, compute_derivatives_2nd_order: bool) dict[str, float | ndarray][source]
- abstract evaluate_all_nll_utils(forward_map_evals: dict[str, float | ndarray], idx: ndarray | None, compute_derivatives: bool, compute_derivatives_2nd_order: bool) dict[str, float | ndarray][source]
- abstract gradient_neglog_pdf(forward_map_evals: dict[str, ndarray], nll_utils: dict[str, ndarray]) ndarray[source]
- abstract neglog_pdf(forward_map_evals: dict, nll_utils: dict, pixelwise: bool = False, full: bool = False, idx: ndarray | None = None) float | ndarray[source]
beetroots.modelling.likelihoods.approx_censored_add_mult module
Implementation of an approximation of the likelihood function of a mixture of Gaussian and multiplicative noises with censorship with a lower limit.
- class beetroots.modelling.likelihoods.approx_censored_add_mult.MixingModelsLikelihood(forward_map, D: int, L: int, N: int, y: ndarray, sigma_a: float | ndarray, sigma_m: float | ndarray, omega: float | ndarray, path_transition_params: str, list_lines_fit: List[str])[source]
Bases:
LikelihoodClass implementing a Gaussian likelihood model with lower censorship. This likelihood function is introduced in Section II.C of Palud et al. [2023].
Note
This likelihood is a parametric approximation of the true likelihood model. The associated parameter, denoted \(a_\ell\) in the article (as there is one such parameter per observable \(\ell `), should be adjusted before any inversion. To adjust this parameter, see the ``beetroots.approx_optim`\) subpackage.
Constructor of the GaussianLikelihood object.
- Parameters:
forward_map (ForwardMap instance) – forward map
D (int) – number of disinct physical parameters in input space.
L (int) – number of distinct observed physical parameters.
N (int) – number of pixels in each physical dimension
y (np.ndarray of shape (N, L)) – mean of the gaussian distribution
sigma_a (float or np.ndarray of shape (N, L)) – standard deviation of the Gaussian distribution
sigma_m (float or np.ndarray of shape (N, L)) – scale parameter of the lognormal distribution
omega (float or np.ndarray of shape (N, L)) – censorship threshold
- Raises:
ValueError – y must have the shape (N, L)
- D
- L
- N
- P_lambda
- evaluate_all_nll_utils(forward_map_evals: dict, idx: ndarray | None = None, compute_derivatives: bool = True, compute_derivatives_2nd_order: bool = True) dict[source]
- forward_map
- grad_P_lambda
- grad_model_mixing_param(forward_map_evals: dict) ndarray[source]
[summary]
- Parameters:
lambda (np.ndarray of shape (N, L)) – [description]
f_Theta (np.ndarray of shape (N, L)) – [description]
grad_f_Theta (np.ndarray of shape (N, D, L)) – [description]
- Returns:
[description]
- Return type:
np.ndarray of shape (N, D, L)
- gradient_neglog_pdf(forward_map_evals: dict, nll_utils: dict) ndarray[source]
[summary]
[extended_summary]
- Parameters:
x (np.ndarray of shape (N, D)) – [description]
f_Theta (np.ndarray of shape (N, L), optional) – image of x via forward map, by default None
grad_f_Theta (np.ndarray of shape (N, D, L), optional) – [description], by default None
- Returns:
[description]
- Return type:
np.ndarray of shape (N, D, L)
- hess_diag_P_lambda
- hess_diag_model_mixing_param(forward_map_evals: dict) ndarray[source]
[summary]
- Parameters:
lambda (np.ndarray of shape (N, L)) – [description]
grad_lambda (np.ndarray of shape (N, D, L)) – [description]
f_Theta (np.ndarray of shape (N, L)) – [description]
grad_f_Theta (np.ndarray of shape (N, D, L)) – [description]
hess_diag_f_Theta (np.ndarray of shape (N, D, L)) – [description]
- Returns:
[description]
- Return type:
np.ndarray of shape (N, D, L)
- hess_diag_neglog_pdf(forward_map_evals: dict, nll_utils: dict) ndarray[source]
[summary]
[extended_summary]
- Parameters:
x (np.ndarray of shape (N, D)) – [description]
f_Theta (np.ndarray of shape (N, L), optional) – [description], by default None
grad_f_Theta (np.ndarray of shape (N, D, L), optional) – [description], by default None
hess_diag_f_Theta (np.ndarray of shape (N, D, L), optional) – [description], by default None
- Returns:
[description]
- Return type:
np.ndarray of shape (N, D, L)
- log_fm1
- log_fp1
- log_y
- model_mixing_param(forward_map_evals: dict, idx: ndarray | None = None) ndarray[source]
computes the weight of the additive model \(\lambda_{n, \ell}\) (line-wise and pixel-wise). In this model, \(\lambda_{n, \ell}\) is a functino of the observation \(y_{n, \ell}\), and therefore constant during the sampling
\[\lambda_{n, \ell} = \frac{\sigma_a^{2b}}{\sigma_a^{2b} + (a \sigma_m y_{n, \ell})^{2b}}\]with \(a\) a transition location parameter and \(b\) a transition speed parameter
- neglog_pdf(forward_map_evals: dict, nll_utils: dict, pixelwise: bool = False, full: bool = False, idx: ndarray | None = None) float | ndarray[source]
- omega
- sample_observation_model(forward_map_evals: dict, rng: ~numpy.random._generator.Generator = Generator(PCG64) at 0x7F868AB00F20) ndarray[source]
- set_likelihood_approx_parameters(path_transition_params: str, list_lines_fit: List[str]) None[source]
Sets the likelihood approximation parameters by reading them from the csv file indicated in the mixing_model_params_filename entry of the input params yaml file. This mixing_model_params_filename file is obtained with the Bayesian Optimization procedure implemented in the approx_optim subpackage (see documentation for more details on how to obtain this file).
The parameters are called log_fm1 and log_fp1. They correspond to the lower and upper limits of the interval in which the two approximations (additive and multiplicative) are combined (below the lower limit, only the additive model is used, and above the upper limit, only the multiplicative model is used).
These two parameters are computed for each pixel n and line ell from the two parameters that are stored in the .csv file, which are the center and the radius of the interval.
- Parameters:
path_transition_params (str) – path to the file that contains the approximation parameters
list_lines_fit (List[str]) – list of lines used during the fit
- sigma_a
- sigma_m
- y
- beetroots.modelling.likelihoods.approx_censored_add_mult.cost_au(y: ndarray, f_Theta: ndarray, m_a: ndarray, s_a: ndarray) ndarray[source]
- beetroots.modelling.likelihoods.approx_censored_add_mult.cost_mu(log_y: ndarray, log_f_Theta: ndarray, m_m: ndarray, s_m: ndarray) ndarray[source]
beetroots.modelling.likelihoods.gaussian module
Implementation of Gaussian likelihood
- class beetroots.modelling.likelihoods.gaussian.GaussianLikelihood(forward_map, D: int, L: int, N: int, y: ndarray, sigma: float | ndarray)[source]
Bases:
LikelihoodClass implementing a Gaussian likelihood model.
Constructor of the GaussianLikelihood object.
- Parameters:
forward_map (ForwardMap instance) – forward map
D (int) – number of disinct physical parameters in input space.
L (int) – number of distinct observed physical parameters.
N (int) – number of pixels in each physical dimension
y (np.ndarray of shape (N, L)) – mean of the gaussian distribution
sigma (float or np.ndarray of shape (N, L)) – variance of the Gaussian distribution
- Raises:
ValueError – y must have the shape (N, L)
- D
- L
- N
- evaluate_all_forward_map(Theta: ndarray, compute_derivatives: bool, compute_derivatives_2nd_order: bool = True) dict[source]
- evaluate_all_nll_utils(forward_map_evals: dict, idx: ndarray | None = None, compute_derivatives: bool = True, compute_derivatives_2nd_order: bool = True) dict[source]
- forward_map
- gradient_neglog_pdf(forward_map_evals: dict, nll_utils: dict) ndarray[source]
[summary]
[extended_summary]
- Parameters:
x (np.ndarray of shape (N, D)) – [description]
f_Theta (np.ndarray of shape (N, L), optional) – image of x via forward map, by default None
grad_f_Theta (np.ndarray of shape (N, D, L), optional) – [description], by default None
- Returns:
[description]
- Return type:
np.ndarray of shape (N, D)
- neglog_pdf(forward_map_evals: dict, nll_utils: dict, pixelwise: bool = False, idx: ndarray | None = None) float | ndarray[source]
- sample_observation_model(forward_map_evals: dict, rng: ~numpy.random._generator.Generator = Generator(PCG64) at 0x7F868A767820) ndarray[source]
- sigma
- y
beetroots.modelling.likelihoods.gaussian_censored module
Implementation of Gaussian likelihood with censorship (with a lower limit)
- class beetroots.modelling.likelihoods.gaussian_censored.CensoredGaussianLikelihood(forward_map, D: int, L: int, N: int, y: ndarray, sigma: float | ndarray, omega: float | ndarray, bias: float | ndarray = 0.0)[source]
Bases:
LikelihoodClass implementing a Gaussian likelihood model with lower censorship
Constructor of the GaussianLikelihood object.
- Parameters:
forward_map (ForwardMap instance) – forward map
D (int) – number of disinct physical parameters in input space.
L (int) – number of distinct observed physical parameters.
N (int) – number of pixels in each physical dimension
y (np.ndarray of shape (N, L)) – mean of the gaussian distribution
bias (float or np.ndarray of shape (N, L)) – variance of the Gaussian distribution
sigma (float or np.ndarray of shape (N, L)) – variance of the Gaussian distribution
omega (float or np.ndarray of shape (N, L)) – censorship threshold
- Raises:
ValueError – y must have the shape (N, L)
- D
- L
- N
- bias
- evaluate_all_forward_map(Theta: ndarray, compute_derivatives: bool, compute_derivatives_2nd_order: bool = True) dict[source]
- evaluate_all_nll_utils(forward_map_evals: dict, idx: ndarray | None = None, compute_derivatives: bool = True, compute_derivatives_2nd_order: bool = True) dict[source]
- forward_map
- gradient_neglog_pdf(forward_map_evals: dict, nll_utils: dict) ndarray[source]
[summary]
[extended_summary]
- Parameters:
x (np.ndarray of shape (N, D)) – [description]
f_Theta (np.ndarray of shape (N, L), optional) – image of x via forward map, by default None
grad_f_Theta (np.ndarray of shape (N, D, L), optional) – [description], by default None
- Returns:
[description]
- Return type:
np.ndarray of shape (N, D)
- hess_diag_neglog_pdf(forward_map_evals: dict, nll_utils: dict) ndarray[source]
[summary]
[extended_summary]
- Parameters:
x (np.ndarray of shape (N, D)) – [description]
f_Theta (np.ndarray of shape (N, L), optional) – [description], by default None
grad_f_Theta (np.ndarray of shape (N, D, L), optional) – [description], by default None
hess_diag_f_Theta (np.ndarray of shape (N, D, L), optional) – [description], by default None
- Returns:
[description]
- Return type:
np.ndarray of shape (N, D, L)
- neglog_pdf(forward_map_evals: dict, nll_utils: dict, pixelwise: bool = False, full: bool = False, idx: ndarray | None = None) float | ndarray[source]
[summary]
\[p(y_{n,\ell} \vert \theta_n) \propto \exp \left\{- [y_{n,\ell} = \omega] \Phi( \frac{\omega - f_{\ell}(\theta_n)}{\sigma^2} \right) - [y_{n,\ell} > \omega] \frac{\omega - f_{\ell}(\theta_n)}{\sigma^2} \right\}\]
- neglog_pdf_ac(forward_map_evals: dict, nll_utils: dict, y: ndarray, sigma: ndarray, omega: ndarray, bias: ndarray) ndarray[source]
- neglog_pdf_au(forward_map_evals: dict, nll_utils: dict, y: ndarray, sigma: ndarray, omega: ndarray, bias: ndarray) ndarray[source]
- omega
- sample_observation_model(forward_map_evals: dict, rng: ~numpy.random._generator.Generator = Generator(PCG64) at 0x7F868A495040) ndarray[source]
- sigma
- y
beetroots.modelling.likelihoods.log_normal module
Implementation of log-normal likelihood
- class beetroots.modelling.likelihoods.log_normal.LogNormalLikelihood(forward_map, D: int, L: int, N: int, y: ndarray, sigma: float | ndarray)[source]
Bases:
LikelihoodClass implementing a log-normal likelihood model.
Constructor of the LogNormalLikelihood object.
- Parameters:
forward_map (ForwardMap instance) – forward map, involved in the mean of the distribution.
D (int) – number of disinct physical parameters in input space.
L (int) – number of distinct observed physical parameters.
N (int) – number of pixels in each physical dimension
y (np.ndarray of shape (N, L)) – parameter of the log-normal distribution
sigma (float or np.ndarray of shape (N, L)) – variance of the log-normal distribution
- Raises:
ValueError – y must have the shape (N, L)
Note
Derivatives and Hessians are taken with respect of the mean of the distribution.
y provided in log space already? (saving computations)
- D
- L
- N
- evaluate_all_forward_map(Theta: ndarray, compute_derivatives: bool, compute_derivatives_2nd_order: bool = True) dict[source]
- evaluate_all_nll_utils(forward_map_evals: dict, idx: int | None = None, compute_derivatives: bool = False, compute_derivatives_2nd_order: bool = True) dict[source]
- forward_map
- hess_diag_neglog_pdf(forward_map_evals: dict, nll_utils: dict) ndarray[source]
Hessian w.r.t to the parameter of the log-normal distribution.
- Parameters:
x (np.ndarray of shape (N, D)) – [description]
f_Theta (np.ndarray of shape (N, L), optional) – [description], by default None
grad_f_Theta (np.ndarray of shape (N, D, L), optional) – [description], by default None
hess_diag_f_Theta (np.ndarray of shape (N, D, L), optional) – [description], by default None
- Returns:
[description]
- Return type:
np.ndarray of shape (N, D)
- logy
- neglog_pdf(forward_map_evals: dict, nll_utils: dict, pixelwise: bool = False, full: bool = False, idx: ndarray | None = None) float | ndarray[source]
- sigma
- y
beetroots.modelling.likelihoods.log_normal_censored module
Implementation of LogNormal likelihood with censorship (with a lower limit)
- class beetroots.modelling.likelihoods.log_normal_censored.CensoredLogNormalLikelihood(forward_map, D: int, L: int, N: int, y: ndarray, sigma: float | ndarray, omega: float | ndarray)[source]
Bases:
LikelihoodClass implementing a LogNormal likelihood model with lower censorship
Constructor of the LogNormalLikelihood object.
- Parameters:
forward_map (ForwardMap instance) – forward map
D (int) – number of disinct physical parameters in input space.
L (int) – number of distinct observed physical parameters.
N (int) – number of pixels in each physical dimension
y (np.ndarray of shape (N, L)) – mean of the LogNormal distribution
bias (float or np.ndarray of shape (N, L)) – variance of the LogNormal distribution
sigma (float or np.ndarray of shape (N, L)) – variance of the LogNormal distribution
omega (float or np.ndarray of shape (N, L)) – censorship threshold
- Raises:
ValueError – y must have the shape (N, L)
- D
- L
- N
- evaluate_all_forward_map(Theta: ndarray, compute_derivatives: bool, compute_derivatives_2nd_order: bool) dict[source]
- evaluate_all_nll_utils(forward_map_evals: dict, idx: ndarray | None = None, compute_derivatives: bool = True, compute_derivatives_2nd_order: bool = True) dict[source]
- forward_map
- gradient_neglog_pdf(forward_map_evals: dict, nll_utils: dict) ndarray[source]
[summary]
[extended_summary]
- Parameters:
x (np.ndarray of shape (N, D)) – [description]
f_Theta (np.ndarray of shape (N, L), optional) – image of x via forward map, by default None
grad_f_Theta (np.ndarray of shape (N, D, L), optional) – [description], by default None
- Returns:
[description]
- Return type:
np.ndarray of shape (N, D)
- hess_diag_neglog_pdf(forward_map_evals: dict, nll_utils: dict) ndarray[source]
[summary]
[extended_summary]
- Parameters:
x (np.ndarray of shape (N, D)) – [description]
f_Theta (np.ndarray of shape (N, L), optional) – [description], by default None
grad_f_Theta (np.ndarray of shape (N, D, L), optional) – [description], by default None
hess_diag_f_Theta (np.ndarray of shape (N, D, L), optional) – [description], by default None
- Returns:
[description]
- Return type:
np.ndarray of shape (N, D, L)
- log_omega
- logy
- neglog_pdf(forward_map_evals: dict, nll_utils: dict, pixelwise: bool = False, idx: ndarray | None = None) float | ndarray[source]
[summary]
\[p(y_{n,\ell} \vert x) \propto \exp \left\{- [y_{n,\ell} = \omega] \Phi( \frac{\omega - f_{\ell}(x_n)}{\sigma^2} \right) - [y_{n,\ell} > \omega] \frac{\omega - f_{\ell}(x_n)}{\sigma^2} \right\}\]
- neglog_pdf_ac(forward_map_evals: dict, nll_utils: dict, log_y: ndarray, sigma: ndarray, log_omega: ndarray) ndarray[source]
- neglog_pdf_au(forward_map_evals: dict, nll_utils: dict, log_y: ndarray, sigma: ndarray, log_omega: ndarray) ndarray[source]
- omega
- sigma
- y
beetroots.modelling.likelihoods.utils module
Utils functions for censored likelihoods
- beetroots.modelling.likelihoods.utils.logpdf_normal(Theta: ndarray) ndarray[source]
- beetroots.modelling.likelihoods.utils.logpdf_normal(Theta: float | int) float
log pdf of the standard gaussian distribution
- Parameters:
Theta (np.ndarray) – points at which the function is to be evaluated in a vectorized way
- Returns:
log pdf of the standard gaussian distribution
- Return type:
np.ndarray
- beetroots.modelling.likelihoods.utils.norm_pdf_cdf_ratio(Theta: ndarray) ndarray[source]
- beetroots.modelling.likelihoods.utils.norm_pdf_cdf_ratio(Theta: float | int) float
computes the ratio of the pdf and cdf of the standard gaussian distribution at a given point
- Parameters:
Theta (float or np.array) – current point
- Returns:
ratio of the pdf and cdf of the standard gaussian distribution (has the same shape as Theta, if Theta is a np.array)
- Return type:
float or np.array