pyselfi.likelihood module

Routines related to the SELFI effective likelihood.

class pyselfi.likelihood.likelihood(blackbox, theta_0, Ne, Ns, Delta_theta)[source]

Bases: object

This class represents the SELFI likelihood. See equations (11)-(15) and (17)-(18) in Leclercq et al. (2019) for expressions.

Variables
  • blackbox (blackbox) – the blackbox simulator, defined as explained in the SELFI documentation

  • theta_0 (array, double, dimension=S) – expansion point in parameter space

  • Ne (int) – number of simulations at the expansion point, to compute the covariance matrix.

  • Ns (int) – number of simulations per expansion direction in parameter space. (Ne may be larger than Ns, in which case only the first Ns simulations at the expansion point are used to compute the gradient)

  • Delta_theta (double) – step size for finite differencing to compute the blackbox gradient in parameter space

classmethod _average(Phi, N)[source]

Returns the average of the first N of a set of summaries.

Parameters
  • Phi (array, double, dimension=(NN,P)) – a set of NN simulations

  • N (int) – take the average of the first N simulations

_compute_Phi_d(pool_fname, d, N=None, h=None)[source]

Computes a pool of blackbox simulations at one of the required points in parameter space.

Parameters
  • pool_fname (str) – filename of the pool

  • d (int) – direction in parameter space, from 0 to S

  • N (int, optional, default=class value for Ne if d=0, or Ns otherwise) – number of simulations per expansion direction in parameter space

  • h (double, optional, default=class value) – step size for finite differencing to compute the blackbox gradient in parameter space

_compute_f_d(d, Ns)[source]

Computes the average blackbox at one of the required points in parameter space. Assumes Phi_d has already been computed.

Parameters
  • d (int) – direction in parameter space, from 0 to S

  • Ns (int) – number of simulations to be used

_compute_grad_d(d, h)[source]

Computes the gradient in one direction of parameter space, see equation (18) in Leclercq et al. (2019). Assumes that f_0 and f_d have already been computed.

Parameters
  • d (int) – direction in parameter space, from 1 to S

  • h (double) – step size for finite differencing

classmethod _covariance(Phi)[source]

Returns the estimated covariance of a set of summaries, with the prefactor for a virtual signal field. See equations (13)-(14) in Leclercq et al. (2019).

Parameters

Phi (array, double, dimension=(N,P)) – a set of N simulations

Returns

Sigma^hat_theta’ – (N+1)/N * Sigma^hat_theta where Sigma^hat_theta is the unbiased covariance estimator

Return type

array, double, dimension=(P,P)

_get_perturb_theta(d, h)[source]

Computes the perturbed theta = theta_0 + Delta_theta. See equation (18) in Leclercq et al. (2019).

Parameters
  • d (int) – index giving the direction in parameter space, from 0 to S

  • h (double) – step size in parameter space

Returns

theta – theta_0 if d=0, or theta_0 + Delta_theta if d is from 1 to S

Return type

double

_gradient(Ns=None, h=None)[source]

Returns the gradient of the linearised blackbox.

Parameters
  • Ns (int, optional, default=class value) – number of simulations per expansion direction in parameter space

  • h (double, optional, default=class value) – step size for finite differencing to compute the blackbox gradient in parameter space

Returns

grad_f – gradient of the linearised blackbox

Return type

array, double, dimension=(P,S)

classmethod _inverse_covariance(Phi, covariance, EPS_Sigma=1e-07, EPS_residual=0.001)[source]

Computes the unbiased inverse of an estimated covariance matrix, using the Hartlap, Simon & Schneider (2007) correction factor. See equations (13) and (15) in Leclercq et al. (2019).

Parameters
  • Phi (array, double, dimension=(N,P)) – a set of N simulations

  • covariance (array, double, dimension=(P,P)) – estimated covariance matrix

  • EPS_Sigma (double, optional, default=1e-7) – epsilon to be added to the diagonal of the covariance matrix, if necessary

  • EPS_residual (double, optional, default=1e-3) – maximum residual in covariance*covariance^{-1} before attempting regularization

Returns

Sigma^hat_theta^{-1}’ – alpha*(Sigma^hat_theta’)^{-1} where (Sigma^hat_theta’)^{-1} is the inverse sample covariance

Return type

array, double, dimension=(P,P)

compute(Ns=None, h=None)[source]

Computes all the necessary quantities to characterize the likelihood (estimated covariance matrix, its inverse, and the gradient).

Parameters
  • Ns (int, optional, default=class value) – number of simulations per expansion direction in parameter space

  • h (double, optional, default=class value) – step size for finite differencing to compute the blackbox gradient in parameter space

compute_C_0()[source]

Computes the estimated covariance of a set of summaries at the expansion point, C_0, and log|2piC_0|. Assumes that Phi_0 has already been computed.

compute_gradient(Ns=None, h=None)[source]

Computes the gradient of the linearised blackbox.

Parameters
  • Ns (int, optional, default=class value) – number of simulations per expansion direction in parameter space

  • h (double, optional, default=class value) – step size for finite differencing to compute the blackbox gradient in parameter space

compute_inv_C_0()[source]

Computes the estimated inverse covariance of a set of summaries at the expansion point, inv_C_0. Assumes that Phi_0 has already been computed.

data_model(theta)[source]

Evaluates the linearised data model at a given point in parameter space, see equation (17) in Leclercq et al. (2019).

Parameters

theta (array, double, dimension=S) – evaluation point in parameter space

load(fname)[source]

Loads the likelihood from an input file.

Parameters

fname (str) – input filename

logpdf(theta, phi_obs)[source]

Returns the log likelihood probability at a given point in parameter space, see equation (19) in Leclercq et al. (2019).

Parameters
  • theta (array, double, dimension=S) – evaluation point in parameter space

  • phi_obs (array, double, dimension=P) – observed summaries vector

Returns

logpdf – log likelihood probability

Return type

double

run_simulations(pool_prefix, pool_suffix, d=None, Ne=None, Ns=None, h=None)[source]

Runs some or all the necessary simulations to compute the likelihood. All the necessary simulations amount to Ne + Ns*S blackbox evaluations.

Parameters
  • pool_prefix (str) – address and prefix of the filenames for simulation pools

  • pool_suffix (str) – suffix of the filenames for simulation pools

  • d (int or array of int, optional, default=None) – directions in parameter space, from 0 to S. If set to None, all simulations are run or loaded

  • Ne (int, optional, default=class value) – number of simulations at the expansion point, to compute the covariance matrix

  • Ns (int, optional, default=class value) – number of simulations per expansion direction in parameter space. (Ne may be larger than Ns, in which case only the first Ns simulations at the expansion point are used to compute the gradient)

  • h (double, optional, default=class value) – step size for finite differencing to compute the blackbox gradient in parameter space

save(fname)[source]

Saves the likelihood to an output file.

Parameters

fname (str) – output filename