Tools#

Contains some random helper functions

This module contains general tools used in different parts of the code.

tools.kl_norm(mean_0, cov_0, mean_1, cov_1)[source]#

Computes the KL divergence between two normal distributions defined by their means and covariance matrices.

May raise numpy.linalg.LinAlgError.

tools.kl_mc(X, logq_func, logp=None, logp_func=None, weight=None)[source]#

Computes KL(P||Q) divergence using Monte Carlo samples X of P, and the logpdf of Q.

All functions assumed vectorised.

The logpdf’s must be both normalised, or with the same off-normalisation factor, e.g. different normalised likelihods with the same prior which is much larger than the mode.

tools.is_valid_covmat(covmat)[source]#

Returns True for a Real, positive-definite, symmetric matrix.

tools.gaussian_distance(points, mean, covmat)[source]#

Computes radial Gaussian distance in units of standar deviations (Mahalanobis distance).

tools.nstd_of_1d_nstd(n1, d, warn_inf=True)[source]#

Radius of (hyper)volume in units of std’s of a multivariate Gaussian of dimension d for a credible (hyper)volume defined by the equivalent 1-dimensional n1-sigma interval.

tools.delta_logp_of_1d_nstd(n1, d)[source]#

Difference between the peak/mean of a Gaussian log-probability and the level corresponding to the credible (hyper)volume defined by the equivalent 1-dimensional n1-sigma interval in d dimensions.

tools.credibility_of_nstd(n, d)[source]#

Posterior mass inside of the (hyper)volume of radius n (in units of std’s) of a multivariate Gaussian of dimension d.

tools.volume_sphere(r, dim=3)[source]#

Volume of a sphere of radius r in dimension dim.

tools.check_random_state(seed, convert_to_random_state=False)[source]#

Extension to sklearn.utils for numpy Generators to pass through.

Includes workaround from scikit-learn/scikit-learn#16988

tools.generic_params_names(n, prefix='x_')[source]#

Returns n generic parameter names (1-based) as [prefix]1, [prefix]2, etc.

class tools.NumpyErrorHandling(all)[source]#

Bases: object

Context for manual handling of numpy errors (e.g. ignoring, just printing…).

NB: the call to deepcopy at init can become expensive if this with context

is used repeatedly. One may want to put it at an upper level then.

tools.get_Xnumber(value, X_letter, X_value=None, dtype=<class 'int'>, varname=None)[source]#

Reads a value out of an X-number, e.g.: “5X” as 5 times the value of X.

If X_value is not defined, returns a tuple (value, has_X, X_power).

tools.check_candidates(gpr, new_X, tol=1e-08)[source]#

Method for determining whether points which have been found by the acquisition algorithm are already in the GP or appear multiple times so that they can be removed. Returns two boolean arrays, the first one indicating whether the point is already in the GP and the second one indicating whether the point appears multiple times.

tools.is_in_bounds(points, bounds, check_shape=False)[source]#

Checks if a point or set of points is within the given bounds.

Parameters:
  • points (numpy.ndarray) – An (N, d) array of points to check

  • bounds (numpy.ndarray) – An (d, 2) array of parameter bounds

  • check_shape (bool (default: False)) – Whether to check for consistency of array shapes.

Returns:

A boolean array of length N indicating whether each point is within the bounds.

Return type:

numpy.ndarray

tools.check_and_return_bounds(bounds)[source]#

Returns the passed bounds as a (dim, 2)-shaped array if it can be mapped to one, and raises TypeError otherwise.

tools.shrink_bounds(bounds, samples, factor=1)[source]#

Reduces the given bounds to the minimal hypercube containing a set of samples.

If factor != 1, the width is multiplied by that factor, while keeping the hypercube centered.

If the samples span a longer region that that defined by the bounds, the given bounds are preferred.

Parameters:
  • bounds (numpy.ndarray) – An (d, 2) array of parameter bounds

  • samples (numpy.ndarray) – An (N, d) array of sampling locations

  • factor (float) – A factor by which to multiply the hypercube width

Returns:

An (d, 2) array of updated parameter bounds

Return type:

numpy.ndarray

Raises:

TypeError: – If bounds or samples are not well formatted or inconsistent with each other.

tools.wrap_likelihood(loglike, ndim)[source]#

Wraps a likelihood function to accept a single argument (a vector of parameters) if it takes multiple arguments.

tools.remove_0_weight_samples(weights, *arrays)[source]#

Removes the elements of arrays (at axis 0) corresponding to the null weights.

Returns a tuple with the non-null weights as the first element, and the rest of them being copies of the given arrays without null-weighted samples in the order with which they were passed. If an element of the list of arrays is None, None is returned in its place.

tools.mean_covmat_from_samples(X, w=None)[source]#

Returns an estimation of the mean and covariance of a set X of points, using their logp as weights.

tools.mean_covmat_from_evals(X, logp)[source]#

Returns an estimation of the mean and covariance of a set X of points, using their logp as weights.