lf2i.utils package#

Submodules#

lf2i.utils.calibration_diagnostics_inputs module#

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, critical_values: ndarray, parameters: ndarray, param_dim: int) Tuple[ndarray, ndarray, ndarray][source]#
lf2i.utils.calibration_diagnostics_inputs.preprocess_indicators_posterior(parameters: Tensor, samples: Tensor, parameter_grid: Tensor, param_dim: int, batch_size: int, posterior: Any | Sequence[Any]) Tuple[Tensor, Tensor, Tensor, Iterator[Any]][source]#
lf2i.utils.calibration_diagnostics_inputs.preprocess_indicators_prediction(parameters: ndarray | Tensor, samples: ndarray | Tensor, param_dim: int) Tuple[ndarray, ndarray, ndarray][source]#
lf2i.utils.calibration_diagnostics_inputs.preprocess_neyman_inversion(test_statistic: ndarray, critical_values: ndarray, parameter_grid: ndarray | Tensor, param_dim: int) Tuple[ndarray, ndarray, ndarray][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_train_quantile_regression(test_statistics: ndarray | Tensor, parameters: ndarray | Tensor, param_dim: int) Tuple[ndarray | Tensor][source]#

lf2i.utils.miscellanea module#

lf2i.utils.miscellanea.np_to_pd(array: ndarray, names: Sequence[str])[source]#
lf2i.utils.miscellanea.to_np_if_torch(inp: ndarray | Tensor) ndarray[source]#

lf2i.utils.odds_inputs module#

lf2i.utils.odds_inputs.preprocess_for_odds_cs(parameter_grid: ndarray | Tensor, samples: ndarray | Tensor, poi_dim: int, batch_size: int, data_dim: int, estimator: Any) 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.

  • samples (Union[np.ndarray, torch.Tensor]) – Array of samples. Should have shape (n_samples, batch_size, data_dim).

  • poi_dim (int) – Dimensionality of the space of parameters of interest.

  • batch_size (int) – Number of samples in a batch from a specific parameter configuration.

  • data_dim (int) – Dimensionality of each single sample.

Returns:

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).

Return type:

np.ndarray

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) 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:

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).

Return type:

Tuple[Union[np.ndarray, torch.Tensor]]

lf2i.utils.odds_inputs.preprocess_odds_estimation(labels: ndarray | Tensor, parameters: ndarray | Tensor, samples: ndarray | Tensor, param_dim: int, estimator: Any) Tuple[ndarray | Tensor][source]#
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, fixed_poi: ndarray | Tensor, opt_params: Tuple[ndarray], sample: ndarray | Tensor, param_dim: int, batch_size: int) ndarray | Tensor[source]#

lf2i.utils.other_methods module#

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:

Array of dimensions (n_samples, 2), where the columns are for the lower and upper bounds of the prediction sets.

Return type:

np.ndarray

Raises:

NotImplementedError – Not yet implemented for non-scalar parameters.

lf2i.utils.other_methods.hpd_region(posterior: NeuralPosterior | KDEWrapper, param_grid: Tensor, x: Tensor, confidence_level: float, num_p_levels: int = 100000, tol: float = 0.01) Tuple[float, Tensor][source]#

Compute a high-posterior-density region at the desired credible level.

Parameters:
  • posterior (Union[NeuralPosterior, KDEWrapper]) – Estimated posterior distribution. Must implement log_prob(…) method.

  • param_grid (torch.Tensor) – Fine grid of evaluation points in the support of the posterior.

  • x (torch.Tensor) – Observed sample given which the posterior p(theta|x) is evaluated.

  • confidence_level (float) – Desired confidence level for the credible region.

  • num_p_levels (int, optional) – Number of level sets to examine, by default 100_000. More level sets imply higher precision in the confidence level.

  • tol (float, optional) – Coverage levels within tol of confidence_level will be accepted, by default 0.01.

Returns:

Final confidence level, hpd credible region

Return type:

Tuple[float, torch.Tensor]

Raises:

ValueError – If posterior is not an instance of NeuralPosterior or KDEWrapper from the sbi package.

lf2i.utils.waldo_inputs module#

lf2i.utils.waldo_inputs.epsilon_variance_correction(conditional_var: List | ndarray, param_dim: int, epsilon: float = 0.001) List | ndarray[source]#
lf2i.utils.waldo_inputs.preprocess_waldo_computation(parameters: ndarray | Tensor, conditional_mean: ndarray | List, conditional_var: ndarray | List, param_dim: int) Tuple[ndarray, ndarray, ndarray][source]#
lf2i.utils.waldo_inputs.preprocess_waldo_estimation(parameters: ndarray | Tensor, samples: ndarray | Tensor, estimation_method: str, estimator: object, param_dim: int) Tuple[ndarray | Tensor, ...][source]#
lf2i.utils.waldo_inputs.preprocess_waldo_evaluation(parameters: ndarray | Tensor, samples: ndarray | Tensor, estimation_method: str, estimator: object, param_dim: int) Tuple[ndarray | Tensor, ...][source]#