forcepho.model#

Classes in the forcepho.model module are used to transform between constrained and unconstrained parameter spaces, apply priors, and to organize and compute posterior probabilities and their gradients for use in sampling or optimization tasks.

They generally require a forcepho.patches.Patch instance and a forcepho.sources.Scene instance as attributes or inputs. Transforms and priors are optional.

model.py

These are the Posterior objects that take a proposal and evaluate its probabiility, storing the ln-probablity and gradients thereof.

There is also a class that wraps such model objects in theano ops, and classes for transforming parameters while keeping track of the Jacobians of the transform.

class forcepho.model.BoundedTransform(lower, upper)#

Use a sigmoid transform to go from the unconstrained sampling space z to the constrained parameter space q or Theta.

This could be made slightly more efficient by caching sigmoid evaluations

jacobian(z)#

J = dq/dz

lndetjac_grad(z)#

d |J| / dz

transform(z)#

Transform from the sampling variable z to the constrained variable Theta. I.e., this is the function g s.t. Theta = g(z)

This uses the sigmoid function.

class forcepho.model.GPUPosterior(proposer, patch, scene=None, lnprior=None, transform=None, name='', print_interval=1000, verbose=False, debug=False, logging=False, sampling=True, **kwargs)#

Alias for backwards compat.

class forcepho.model.Posterior(**kwargs)#

Base class for models. Includes the basic caching mechanism for probabilities and gradients, as well as a numerical check on the gradients.

check_grad(z, delta=1e-05)#

Compare numerical gradients to analytic gradients.

Parameters:
  • z (ndarray, shape (ndim,)) – The parameter location at which to check gradients

  • delta (float or ndarray, optional, default=1e-5) – Change in parameter values to use for calculating numerical gradients

Returns:

  • dlnp (ndarray, shape (ndim,)) – the analytic gradients

  • dlnp_num (ndarray, shape (ndim,)) – The numerical gradients

evaluate(z)#

Method to actually evaluate ln-probability and its gradient, at the sampling aparameter position z, and cache these quantities. Other values may be cached or actions performed at this time. This should be subclassed.

Populates the following attributes * _z - the parameter posiiton * _lnp - the ln-likelihood at z * - lnp_grad - the gradient of the ln-likelihood with respect to z * ncall - the number of likelihood calls is incremented by 1

Parameters:

z (ndarray of shape (ndim,)) – A parameter vector.

lnprior(q)#

The prior probability and its gradient with respect to the scene parameters. By default this returns 0.0 for both, but this method can be overridden.

Parameters:

q (ndarray of shape (ndim,)) – The array of parameters in the scene space

Returns:

  • lpr (float) – The ln of the prior probability for the parameters q

  • lpr_grad (ndarray of shape (ndim,) or 0.0) – The gradient of lpr with respect to the scene parameters q

lnprob(z)#

Get the ln-probability at parameter position z. This uses the cached value of the ln-probability if z has not changed, and otherwise calls the evaluate method.

Parameters:

z (ndarray of shape (ndim,)) – The parameter position (in the unconstrained sampling parameter space)

Returns:

lnp – The ln-probability at the parameter position z

Return type:

np.float

lnprob_and_grad(z)#

Some samplers want a single function to return lnp, dlnp. This is that.

lnprob_grad(z)#

Get the gradients of the ln-probability with respect to z, at parameter position z. This uses the cached value of the ln-probability gradient if z has not changed, and otherwise calls the evaluate method.

Parameters:

z (ndarray of shape (ndim,)) – The parameter position (in the unconstrained sampling parameter space)

Returns:

lnp_grad – The gradient of the ln-probability with repsect to z, at the parameter position z

Return type:

ndarray of shape (ndim,)

make_transform(z)#

Transform the sampling parameters z in the unconstrained space to the constrained scene parameters q using the transform attribute. This also caches the jacobian of the transform, the determinant of that jacobian, and the gradient of the determinant.

Parameters:

z (ndarray of shape (ndim,)) – The parameters in the (unconstrained) sampling space

Returns:

q – The parameters in the scene space

Return type:

ndarray

nll(z)#

A shortcut for the negative ln-priobability, for use with minimization algorithms. Returns both the NLL and it’s gradient. Note this removes the jacobian volume correction necessary for good sampling.

nll_nograd(z)#

A shortcut for the negative ln-priobability, for use with minimization algorithms. Returns both the NLL and it’s gradient. Note this removes the jacobian volume correction necessary for good sampling, but that hinders optimization.