beetroots.sampler package
Subpackages
- beetroots.sampler.saver package
- Submodules
- beetroots.sampler.saver.abstract_saver module
SaverSaver.DSaver.D_samplingSaver.LSaver.NSaver.batch_sizeSaver.check_need_to_save()Saver.check_need_to_update_memory()Saver.final_next_batch_sizeSaver.freq_saveSaver.initialize_memory()Saver.list_idx_samplingSaver.memorySaver.next_batch_sizeSaver.results_pathSaver.save_additional()Saver.save_forward_map_evalsSaver.save_to_file()Saver.scalerSaver.set_results_path()Saver.t_last_initSaver.t_last_saveSaver.update_memory()
- beetroots.sampler.saver.my_saver module
MySaverMySaver.DMySaver.D_samplingMySaver.LMySaver.NMySaver.batch_sizeMySaver.final_next_batch_sizeMySaver.freq_saveMySaver.initialize_memory()MySaver.list_idx_samplingMySaver.memoryMySaver.next_batch_sizeMySaver.results_pathMySaver.save_forward_map_evalsMySaver.scalerMySaver.t_last_initMySaver.t_last_saveMySaver.update_memory()
- Module contents
- beetroots.sampler.utils package
Submodules
beetroots.sampler.abstract_sampler module
- class beetroots.sampler.abstract_sampler.Sampler(D: int, L: int, N: int, rng: ~numpy.random._generator.Generator = Generator(PCG64) at 0x7FC163622580)[source]
Bases:
ABCabstract class designed to sample from a distribution defined with the
Posteriorclass.- Parameters:
D (int) – total number of physical parameters to reconstruct
L (int) – number of observables per component \(n\)
N (int) – total number of pixels to reconstruct
rng (np.random.Generator) – numpy random generator (for sampling reproducibility), by default np.random.default_rng(42)
- D
total number of physical parameters to reconstruct
- Type:
int
- ESS_OPTIM = 1000
number of random reproduced observations to draw to evaluate the model checking p-value for optimization procedures
- L
number of observables per component \(n\)
- Type:
int
- N
total number of pixels to reconstruct
- Type:
int
- generate_random_start_Theta(posterior: Posterior)[source]
generates a random element of the hypercube defined by the lower and upper bounds with uniform distribution
- Parameters:
posterior (Posterior) – contains the lower and upper bounds of the hypercube
- Returns:
Theta – random element of the hypercube defined by the lower and upper bounds with uniform distribution
- Return type:
np.array of shape (N, D)
- abstract generate_random_start_Theta_1pix(Theta: ndarray, posterior: Posterior, idx_pix: ndarray) ndarray[source]
draws a random vectors for components \(n\) (e.g., a pixel \(\theta_n\)).
- Parameters:
Theta (np.ndarray) – current iterate
posterior (Posterior) – contains the lower and upper bounds of the hypercube
idx_pix (np.ndarray) – indices of the pixels
- Returns:
random element of the hypercube defined by the lower and upper bounds with uniform distribution
- Return type:
np.array of shape (n_pix, self.k_mtm, D)
- get_rng_state()[source]
extracts the current state and inc of the random generator
- Returns:
rng_state_array (np.array of ints) – current state of the random generator
rng_inc_array (np.array of ints) – current inc of the random generator
Note
state and inc are very large integers, and thus need to be converted to hex format (later to an array of ints) to be saved in a .h5 file https://docs.python.org/3/library/stdtypes.html#int.to_bytes
need 32 bytes in length: otherwise, the inverse operation int.from_bytes(state_array,byteorder) does not coincide with the original int value
- rng
numpy random generator (for sampling reproducibility)
- Type:
np.random.Generator
- abstract sample(posterior: Posterior, saver: Saver, max_iter: int, Theta_0: ndarray | None, T_BI: int | None, **kwargs) None[source]
main method of the class, runs the sampler on the input posterior distribution
- Parameters:
posterior (Posterior) – probability distribution to be sampled
saver (Saver) – object responsible for progressively saving the Markov chain data during the run
max_iter (int) – total duration of a Markov chain
Theta_0 (Optional[np.ndarray], optional) – starting point
T_BI (int, optional) – duration of the Burn-in phase
- sample_regu_hyperparams(posterior: Posterior, regu_weights_optimizer: EBayesMMLE, t: int, current_Theta: ndarray) ndarray[source]
updates the spatial regularization weight vector (denoted \(\tau\) in Palud et al. [2023]) using the marginal likelihood optimizer defined in Vidal et al. [2020]
- Parameters:
posterior (Posterior) – posterior distribution
regu_weights_optimizer (EBayesMMLE) – marginal likelihood optimizer
t (int) – time step
current_Theta (np.ndarray) – current iterate
- Returns:
updated spatial regularization weight vector
- Return type:
np.ndarray
beetroots.sampler.my_sampler module
Contains a class of sampler used in the Meudon PDR code Bayesian inversion problems
- class beetroots.sampler.my_sampler.MySampler(my_sampler_params: ~beetroots.sampler.utils.my_sampler_params.MySamplerParams, D: int, L: int, N: int, rng: ~numpy.random._generator.Generator = Generator(PCG64) at 0x7FC1608BFAC0)[source]
Bases:
SamplerDefines the sampler proposed in Palud et al. [2023] that randomly combines two transition kernels :
a independent MTM-chromatic Gibbs transition kernel
a position-dependent MALA transition kernel with the RMSProp pre-conditioner
- Parameters:
my_sampler_params (MySamplerParams) – contains the main parameters of the algorithm
D (int) – total number of physical parameters to reconstruct
L (int) – number of observables per component \(n\)
N (int) – total number of pixels to reconstruct
rng (numpy.random.Generator, optional) – random number generator (for reproducibility), by default np.random.default_rng(42)
- D
total number of physical parameters to reconstruct
- Type:
int
- L
number of observables per component \(n\)
- Type:
int
- N
total number of pixels to reconstruct
- Type:
int
- alpha
weight of past values of \(v\) in the exponential decay (cf RMSProp preconditioner), denoted \(\alpha \in ]0,1[\) in the article
- Type:
float
- compute_correction_term
wether or not to use the correction term (denoted \(\gamma\) in the article) during the sampling (only used if is_stochastic=True)
- Type:
bool
- compute_derivatives_2nd_order
wether to compute the expensive second order derivatives terms. Only true when the sampler runs a Markov chain (2nd order never used in optimization) and when the correction term denoted \(\gamma\) is to be computed.
- Type:
bool
- current
contains all the data about the current iterate (including the evaluations of the forward map and derivatives, etc.)
- Type:
dict
- eps0
step size used in the Position-dependent MALA transition kernel, denoted \(\epsilon > 0\) in the article
- Type:
float
- generate_new_sample_mtm(t: int, posterior: Posterior)[source]
generates a new sample using the MTM transition kernel
- Parameters:
t (int) – current iteration index
posterior (Posterior) – target posterior distribution to sample from
- Returns:
accepted (bool) – wether or not the candidate was accepted
log_proba_accept (float) – log of the acceptance proba
- generate_new_sample_pmala_rmsprop(t: int, posterior: Posterior)[source]
generates a new sample using the position-dependent MALA transition kernel
- Parameters:
t (int) – current iteration index
posterior (Posterior) – negative log posterior class
- Returns:
accepted (bool) – wether or not the candidate was accepted
log_proba_accept (float) – log of the acceptance proba
- generate_random_start_Theta_1pix(Theta: ndarray, posterior: Posterior, idx_pix: ndarray) ndarray[source]
draws a random vectors for components \(n\) (e.g., a pixel \(\theta_n\)). The distribution used to draw these vectors is:
the smooth indicator prior
a combination of the smooth indicator prior and of a Gaussian mixture defined with the set of all combinations of neighbors of component \(n\)
- Parameters:
Theta (np.ndarray) – current iterate
posterior (Posterior) – contains the lower and upper bounds of the hypercube
idx_pix (np.ndarray) – indices of the pixels
- Returns:
random element of the hypercube defined by the lower and upper bounds with uniform distribution
- Return type:
np.array of shape (n_pix, self.k_mtm, D)
:raises ValueError : if
posterior.prior_indicatoris None:
- k_mtm
number of candidates in the MTM kernel, denoted \(K\) in the article
- Type:
int
- lambda_
limit value that avoids division by zero when computing the RMSProp preconditioner, denoted \(\eta > 0\) in the article
- Type:
float
- rng
random number generator (for reproducibility)
- Type:
numpy.random.Generator
- sample(posterior: Posterior, saver: Saver, max_iter: int, Theta_0: ndarray | None = None, disable_progress_bar: bool = False, regu_spatial_N0: int | float = inf, regu_spatial_scale: float = 1.0, regu_spatial_vmin: float = 1e-08, regu_spatial_vmax: float = 100000000.0, T_BI: int = 0) None[source]
main method of the class, runs the sampler
- Parameters:
posterior (Posterior) – probability distribution to be sampled
saver (Saver) – object responsible for progressively saving the Markov chain data during the run
max_iter (int) – total duration of a Markov chain
Theta_0 (Optional[np.ndarray], optional) – starting point, by default None
disable_progress_bar (bool, optional) – wether to disable the progress bar, by default False
regu_spatial_N0 (Union[int, float], optional) – number of iterations defining the initial update phase (for spatial regularization weight optimization). np.infty means that the optimization phase never starts, and that the weight optimization is not applied. by default np.infty
regu_spatial_scale (Optional[float], optional) – scale parameter involved in the definition of the projected gradient step size (for spatial regularization weight optimization). by default 1.0
regu_spatial_vmin (Optional[float], optional) – lower limit of the admissible interval (for spatial regularization weight optimization), by default 1e-8
regu_spatial_vmax (Optional[float], optional) – upper limit of the admissible interval (for spatial regularization weight optimization), by default 1e8
T_BI (int, optional) – duration of the Burn-in phase, by default 0
- selection_probas
vector of selection probabilities for the MTM and PMALA kernels, respectively, i.e., \([p_{MTM}, 1 - p_{MTM}]\)
- Type:
np.ndarray
- stochastic
if True, the algorithm performs sampling, and optimization otherwise
- Type:
bool
- v
RMSProp gradient variance vector, denoted \(v\) in the article
- Type:
np.ndarray
Module contents
Contains the definition of the sampler and of a saver class