figaro.utils module

figaro.utils.get_priors(bounds, samples=None, mean=None, std=None, df=None, k=None, a=None, scale=None, probit=True, hierarchical=False)[source]

This method takes the prior parameters for the Normal-Inverse-Wishart distribution in the natural space and returns them as parameters in the probit space, ordered as required by FIGARO. In the following, D will denote the dimensionality of the inferred distribution.

Either four or two parameters are returned:
  • If hierarchical is False, (k, L, df, mu) are returned:
    • df, is the number of degrees of freedom for the Inverse Wishart distribution,. It must be greater than D+1. If this parameter is None or does not satisfy the condition df > D+1, the default value D+2 is used;

    • k is the scale parameter for the multivariate Normal distribution. Suggested values are k <~ 1e-1. If None, the default value 1e-2 is used.

    • mu is the mean of the multivariate Normal distribution. It can be either estimated from the available samples or passed directly as a 1D array with length D (the keyword argument mean overrides the samples). If None, the default value 0 (corresponding to the parameter space center) is used.

    • L is the expected value for the Inverse Wishart distribution. This parameter can be either:
      • passed as 1D array with shape (D,) or double: vector of standard deviations (if double, it assumes that the same std has to be used for all dimensions) - keyword std;

      • estimated from samples - keyword samples.

  • If hierarchical is True, (L, a) are returned:
    • L is the expected value for the Inverse Gamma distribution. It can be either:
      • passed as 1D array with shape (D,) or double: vector of standard deviations (if double, it assumes that the same std has to be used for all dimensions) - keyword std;

      • estimated from samples - keyword samples;

      • estimated from bounds using the keyword fraction.

    • a is the shape parameter of the gamma distribution. It can be either passed with the dedicated keyword or fixed to the default parameter, 2.

Parameters:
  • bounds (np.ndarray) – boundaries for probit transformation

  • samples (np.ndarray) – 2D [DPGMM] or 3D [(H)DPGMM] array with samples

  • mean (double or np.ndarray) – mean [DPGMM]

  • std (double or np.ndarray) – expected standard deviation (if double, the same std is used for all dimensions, if np.ndarray must match the number of dimensions) [DPGMM and (H)DPGMM]

  • df (int) – degrees of freedom for Inverse Wishart distribution [DPGMM]

  • k (double) – scale parameter for Normal distribution [DPGMM]

  • a (double) – shape parameter for the Inverse Gamma distribution [(H)DPGMM]

  • scale (double) – fraction of samples std [DPGMM]

  • probit (bool) – whether the probit transformation will be applied or not

  • hierarchical (bool) – returns the prior pars for (H)DPGMM rather than for DPGMM

Returns:

prior parameters ordered as in (H)/DPGMM

Return type:

tuple

figaro.utils.gradient_median(x, draws)[source]

Computes the gradient of the median distribution (slow but numerically stable)

Parameters:
  • x (np.ndarray) – points to evaluate the median at

  • draws (iterable) – container for mixture instances

Returns:

gradient

Return type:

np.ndarray

figaro.utils.load_options(opts, parser)[source]

Loads options for the run (reproducibility)

Parameters:
  • opts (optparser.Options) – options object

  • parser (optparse.OptionParser) – parser object

Returns:

options

Return type:

optparser.Options

figaro.utils.make_gaussian_mixture(mu, cov, bounds, out_folder='.', names=None, save=False, save_samples=False, n_samps=3000, probit=True, ext='json')[source]

Builds mixtures composed of equally-weighted Gaussian distribution. WARNING: due to the probit coordinate change, a Gaussian distribution in the natural space does not correspond to a Gaussian distribution in the probit space. The resulting distributions in probit space, therefore, are just an approximation. This approximation holds for distributions which are far from boundaries. In general, a more robust (but slower) approach would be to draw samples from each original Gaussian distribution and to use them to make a hierarchical inference.

Parameters:
  • mu (np.ndarray) – mean for each Gaussian distribution

  • cov (np.ndarray) – covariance matrix for each Gaussian distribution

  • bounds (np.ndarray) – boundaries for probit transformation

  • out_folder (str) – output folder

  • save (bool) – whether to save the draws or not

  • save_samples (bool) – whether to save the samples or not

  • n_samps (int) – number of samples to estimate mean and covariance in probit space

  • probit (bool) – whether to use the probit transformation or not

  • ext (str) – file extension (pkl or json)

Returns:

mixtures

Return type:

np.ndarray

figaro.utils.recursive_grid(bounds, n_pts, get_1d=False)[source]

Recursively generates the n-dimensional grid points (extremes are excluded).

Parameters:
  • bounds (list-of-lists) – extremes for each dimension (excluded)

  • n_pts (int) – number of points for each dimension

  • get_1d (bool) – return list of 1d-arrays (one per dimension)

Returns:

grid np.ndarray: differential for each grid np.ndarray: list of 1d-arrays (one per dimension)

Return type:

np.ndarray

figaro.utils.rejection_sampler(n_draws, f, bounds, selfunc=None)[source]

Rejection sampler, allows for a selection function

Parameters:
  • n_draws (int) – number of draws

  • f (callable) – probability density to sample from

  • bounds (iterable) – upper and lower bound

  • selfunc (callable) – selection function, must support numpy arrays

Returns:

samples

Return type:

np.ndarray

figaro.utils.rvs_median(draws, size=1)[source]

Generates samples from median distribution of a set of draws.

Parameters:
  • draws (iterable) – container for mixture instances

  • size (int) – number of samples

Returns:

samples

Return type:

np.ndarray

figaro.utils.save_options(options, out_folder, name=None)[source]

Saves options for the run (reproducibility)

Parameters:
  • options (optparser.Options) – options

  • out_folder (str or Path) – folder where to save the option file

  • name (str) – name of the run