lf2i.utils package¶
Submodules¶
lf2i.utils.calibration_diagnostics_inputs module¶
- lf2i.utils.calibration_diagnostics_inputs.preprocess_train_quantile_regression(test_statistics: ndarray | Tensor, parameters: ndarray | Tensor, param_dim: int, estimator: Any) Tuple[ndarray | Tensor][source]¶
- lf2i.utils.calibration_diagnostics_inputs.preprocess_predict_quantile_regression(parameters: ndarray | Tensor, estimator: Any, param_dim: int) ndarray | Tensor[source]¶
- lf2i.utils.calibration_diagnostics_inputs.preprocess_fit_p_values(inp: ndarray | Tensor, rejection_probs_model: Any) ndarray | Tensor[source]¶
- lf2i.utils.calibration_diagnostics_inputs.preprocess_predict_p_values(mode: str, test_stats: ndarray | Tensor, poi: ndarray | Tensor, rejection_probs_model: Any) ndarray | Tensor[source]¶
- lf2i.utils.calibration_diagnostics_inputs.preprocess_diagnostics(indicators: ndarray | Tensor, parameters: ndarray | Tensor, new_parameters: ndarray | Tensor | None, param_dim: int) Tuple[ndarray, ndarray, ndarray][source]¶
- lf2i.utils.calibration_diagnostics_inputs.preprocess_indicators_lf2i(test_statistics: ndarray | Tensor, critical_values: ndarray | Tensor | None, p_values: ndarray | Tensor | None, parameters: ndarray | Tensor, param_dim: int) Tuple[ndarray, ndarray, ndarray, ndarray][source]¶
lf2i.utils.confidence_regions module¶
- lf2i.utils.confidence_regions.preprocess_neyman_inversion(test_statistics: ndarray | torch.Tensor | None, critical_values: ndarray | torch.Tensor | None, p_values: ndarray | torch.Tensor | None, parameter_grid: ndarray | torch.Tensor, param_dim: int) Tuple[int, ndarray, ndarray, ndarray, ndarray][source]¶
lf2i.utils.miscellanea module¶
- lf2i.utils.miscellanea.check_for_nans(inp: ndarray | Series | DataFrame | Tensor) ndarray | Series | DataFrame | Tensor[source]¶
lf2i.utils.odds_inputs module¶
- lf2i.utils.odds_inputs.preprocess_odds_estimation(parameters: ndarray | Tensor, samples: ndarray | Tensor, param_dim: int, estimator: Any, parameter_space_bounds: List[Tuple[float]] | None = None) Tuple[ndarray | Tensor][source]¶
- lf2i.utils.odds_inputs.preprocess_odds_relabel(parameters: ndarray | Tensor, samples: ndarray | Tensor, use_distant_pairs: bool = False) Tuple[source]¶
Create labels by splitting data into two halves, keeping one matched and permuting the other.
- Parameters:
parameters (array of shape (n_samples, param_dim))
samples (array of shape (n_samples, batch_size, data_dim))
use_distant_pairs (bool) – If True, use farthest parameter pairs for negative class. If False, use random permutation within the second half.
- Returns:
Tuple of (all_parameters, all_samples, all_labels)
- lf2i.utils.odds_inputs.preprocess_for_odds_cv(parameters: ndarray | Tensor, samples: ndarray | Tensor, param_dim: int, batch_size: int, data_dim: int, estimator: Any, parameter_space_bounds: List[Tuple[float]] | None = None) Tuple[ndarray | Tensor][source]¶
Flatten samples along batch_size dimension and stack them with corresponding repeated parameters column-wise. This is done to simultaneously estimate odds at all samples, given the corresponding parameters.
Inputs are converted to correct format depending on estimator type.
- Parameters:
parameters (Union[np.ndarray, torch.Tensor]) – Array of parameters, one for each batch of size batch_size.
samples (Union[np.ndarray, torch.Tensor]) – Array of samples. Assumed to have shape (n_samples, batch_size, data_dim).
param_dim (int) – Dimensionality of the parameter space.
batch_size (int) – Number of samples in a batch from a specific parameter configuration.
data_dim (int) – Dimensionality of each single sample.
- Returns:
Tuple[Union[np.ndarray, torch.Tensor]] – Parameters, samples, and stacked parameters and samples. The stacked vector is flattened along dim 1, with output shape (n_samples*batch_size, param_dim+data_dim).
- lf2i.utils.odds_inputs.preprocess_for_odds_cs(parameter_grid: ndarray | Tensor, samples: ndarray | Tensor, param_dim: int, batch_size: int, data_dim: int, estimator: Any, parameter_space_bounds: List[Tuple[float]] | None = None) Tuple[ndarray | Tensor][source]¶
- Repeat and tile both parameter_grid and samples to achieve the following data structure:
param_grid_0, samples_0_0 param_grid_0, samples_0_1 param_grid_1, samples_0_0 param_grid_1, samples_0_1 … param_grid_0, samples_1_0 param_grid_0, samples_1_1 param_grid_1, samples_1_0 param_grid_1, samples_1_1 …
This is done to simultaneously estimated odds across all parameters for each sample.
- Parameters:
parameter_grid (Union[np.ndarray, torch.Tensor]) – Array of parameters over which odds have to be evaluated for each sample. Note that parameter_grid is expected to be of shape (-1, poi_dim + nuisance_dim).
samples (Union[np.ndarray, torch.Tensor]) – Array of samples. Should have shape (n_samples, batch_size, data_dim).
param_dim (int) – Dimensionality of the parameter space.
batch_size (int) – Number of samples in a batch from a specific parameter configuration.
data_dim (int) – Dimensionality of each single sample.
- Returns:
np.ndarray – Parameter grid, samples, and stacked parameter grid and samples. The stacked vector has output shape (param_grid_size*n_samples*batch_size, param_dim+data_dim).
- lf2i.utils.odds_inputs.preprocess_odds_integration(estimator: Any, fixed_poi: ndarray | Tensor, integ_params: List[float], sample: ndarray | Tensor, param_dim: int, batch_size: int) ndarray | Tensor[source]¶
- lf2i.utils.odds_inputs.preprocess_odds_maximization(estimator: Any, nominal_params: Tensor, opt_param: ndarray | Tensor, opt_param_index: int, sample: ndarray | Tensor, parameter_space_bounds: List[Tuple[float]] | None = None) ndarray | Tensor[source]¶
Preprocessing for one-at-a-time optimization of the odds ratio. Given a sample, a nominal parameter vector, and a particular component j of the full parameter vector, concatenate the parameters by swapping the j-th component of the nominal parameter with the optimization variable.
- Args:
estimator nominal_params: Of shape (poi_dim + nuisance_dim,) opt_param: Scalar value for the optimization variable (the j-th component of the parameter vector) opt_param_index: Index of the optimization variable in the full parameter vector sample: One sample, of shape (batch_size, data_dim)
lf2i.utils.other_methods module¶
- lf2i.utils.other_methods.hpd_region(posterior: AbstractNeuralPosterior | AbstractKDE | Distribution, param_grid: Tensor, x: Tensor, credible_level: float, num_level_sets: int = 100000, tol: float = 0.01, **posterior_kwargs) Tuple[float, Tensor][source]¶
Compute the highest posterior density (HPD) region for an estimated posterior distribution. Currently compatible with the posterior estimators commonly seen in the sbi and bayesflow software libraries.
- Parameters:
posterior (Union[AbstractNeuralPosterior, AbstractKDE, Distribution]) – The estimated posterior distribution from which to compute the HPD region. These types of objects are typically returned by the sbi and bayesflow software libraries: - AbstractNeuralPosterior from sbi methods involving underlying neural networks, e.g. SNPE, FMPE. - AbstractKDE from sbi methods involving kernel density estimation, e.g. SBCABC. - Distribution from torch.distributions or other libraries.
param_grid (torch.Tensor) – Grid of parameter values over which to evaluate the posterior.
x (torch.Tensor) – Observed data or summary statistics.
credible_level (float) – The desired credible level for the HPD region (e.g., 0.95 for a 95% credible region).
num_level_sets (int, optional) – Number of level sets to consider when descending the posterior vis a vis a binary search, by default 100_000.
tol (float, optional) – Tolerance for the credible level, by default 0.01.
**posterior_kwargs (Any) – Any keyword argument needed when calling the log_prob method of the posterior.
- Returns:
Tuple[float, torch.Tensor] – The achieved credible level and the parameter values within the HPD region.
- Raises:
ValueError – If the posterior type is not recognized.
- lf2i.utils.other_methods.gaussian_prediction_sets(conditional_mean_estimator: Any, conditional_variance_estimator: Any, samples: Tensor | ndarray, confidence_level: float, param_dim: int) ndarray[source]¶
Compute prediction sets centered around the point estimate using a Gaussian approximation: \(\mathbb{E}[\theta|X] \pm z_{1-\alpha/2} \cdot \sqrt{\mathbb{V}[\theta|X]}\).
- Parameters:
conditional_mean_estimator (Any) – Prediction algorithm to estimate the conditional mean under squared error loss. Must implement predict(X=…) method.
conditional_variance_estimator (Any) – Prediction algorithm to estimate the conditional variance under squared error loss. Must implement predict(X=…) method. One way to get this is to use the conditional_mean_estimator, compute the squared residuals, and regress them against the data.
samples (Union[torch.Tensor, np.ndarray]) – Array of samples given which to compute the prediction sets. The 0-th dimension indexes samples coming from different parameters. One prediction set for each “row” will be computed.
confidence_level (float) – Desired confidence level of the resulting prediction sets. It determines the Gaussian percentile to use as multiplier for the error estimate.
param_dim (int) – Dimensionality of the parameter.
- Returns:
np.ndarray – Array of dimensions (n_samples, 2), where the columns are for the lower and upper bounds of the prediction sets.
- Raises:
NotImplementedError – Not yet implemented for non-scalar parameters.
- lf2i.utils.other_methods.monte_carlo_confidence_region(*args, **kwargs)¶
- lf2i.utils.other_methods.monte_carlo_critical_values(*args, **kwargs)¶
- lf2i.utils.other_methods.monte_carlo_coverage(*args, **kwargs)¶
- lf2i.utils.other_methods.monte_carlo_coverage_posterior(*args, **kwargs)¶
- lf2i.utils.other_methods.monte_carlo_pvalue_diagnostics(*args, **kwargs)¶
lf2i.utils.parallel module¶
- lf2i.utils.parallel.tqdm_joblib(tqdm_object)[source]¶
Context manager to patch joblib to report into tqdm progress bar given as argument
This wrapped context manager obtains the number of finished tasks from the tqdm print function and uses it to update the pbar, as suggested in https://stackoverflow.com/a/61689175. See #419, #421
lf2i.utils.posterior_ts_inputs module¶
lf2i.utils.waldo_inputs module¶
- lf2i.utils.waldo_inputs.preprocess_waldo_estimation(parameters: ndarray | Tensor, samples: ndarray | Tensor, estimation_method: str, estimator: Any, param_dim: int) Tuple[ndarray | Tensor, ...][source]¶
- lf2i.utils.waldo_inputs.preprocess_waldo_evaluation(parameters: ndarray | Tensor, samples: ndarray | Tensor, estimation_method: str, estimator: Any, param_dim: int) Tuple[ndarray | Tensor, ...][source]¶