Target

class telescopic.Target(
log_prob,
grad=None,
*,
log_prob_and_grad=None,
args=(),
kwargs=None,
vectorize=False,
)

A log-density \(\log\pi\) known up to a constant, with an optional gradient.

Kernels call log_prob() and log_prob_and_grad() on batches of points of shape (n, d), whether or not the user’s functions are vectorized.

Parameters:
log_probcallable() or None

log_prob(x, *args, **kwargs) returns a float for x of shape (d,), or an array of shape (n,) for x of shape (n, d) when vectorize is true. -inf marks zero density. It must be deterministic: the same point must always give the same value. None is for Gibbs kernels, which need no log-density: every point then gets 0.

gradcallable(), optional

\(\nabla\log\pi\) with the same signature, returning shape (d,) or (n, d).

log_prob_and_gradcallable(), optional

In place of grad: \((\log\pi, \nabla\log\pi)\) in one call, with the same signature, returning a pair (value, gradient) of a float and shape (d,), or of shapes (n,) and (n, d), as from JAX’s value_and_grad. Its values must be those of log_prob, which still serves the evaluations that need no gradient. Where its value is -inf, its gradient must still be real numbers of the gradient’s shape, whose values are not used: states there carry a gradient of 0.

argstuple or list

Extra positional arguments passed to the functions, such as (data,).

kwargsmapping, optional

Extra keyword arguments passed to them.

vectorizebool

Whether the functions accept a batch of points. Kernels then also compute their matrix products with BLAS, for a batch of chains at once.

Notes

The functions receive read-only arrays: a function that modifies its input raises instead of corrupting the chains. Their outputs are copied, so a function may reuse its output array, and an output whose shape differs from the expected one only by axes of length 1, as SciPy’s logpdf of one point, is reshaped. The counters n_log_prob and n_grad count evaluations point by point, and n_grad_zeroed the gradients taken as 0 because they were not finite where the log-density is. A point that log_prob_and_grad evaluates counts in n_log_prob, and in n_grad where its value is finite, as with grad.

property has_grad

Whether a gradient was given, as grad or log_prob_and_grad.

Returns:
bool
property has_log_prob

Whether a log-density was given.

Returns:
bool
log_prob(x)

Log-densities at the rows of x; raises ValueError on NaN or +inf.

Parameters:
xndarray, shape (n, d)

The points, one per row.

Returns:
ndarray, shape (n,)

\(\log\pi\) at each row, -inf at zero density; 0 for a target without a log-density.

log_prob_and_grad(x, *, start=False)

Log-densities and gradients at the rows of x.

One call of log_prob_and_grad evaluates both at every row; otherwise grad is evaluated only where the log-density is finite. The gradient is 0 where the log-density is -inf. Where it is finite, a gradient that is not finite is taken as 0 too, and counted in n_grad_zeroed; at the states that chains start from, it raises ValueError with the point.

Parameters:
xndarray, shape (n, d)

The points, one per row.

startbool

Whether chains start at the rows of x.

Returns:
log_probndarray, shape (n,)
gradndarray, shape (n, d)