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()andlog_prob_and_grad()on batches of points of shape(n, d), whether or not the user’s functions are vectorized.- Parameters:
- log_prob
callable()orNone log_prob(x, *args, **kwargs)returns a float forxof shape(d,), or an array of shape(n,)forxof shape(n, d)whenvectorizeis true.-infmarks zero density. It must be deterministic: the same point must always give the same value.Noneis for Gibbs kernels, which need no log-density: every point then gets 0.- grad
callable(), optional \(\nabla\log\pi\) with the same signature, returning shape
(d,)or(n, d).- log_prob_and_grad
callable(), 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’svalue_and_grad. Its values must be those oflog_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.- args
tupleorlist 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.
- log_prob
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
logpdfof one point, is reshaped. The countersn_log_probandn_gradcount evaluations point by point, andn_grad_zeroedthe gradients taken as 0 because they were not finite where the log-density is. A point thatlog_prob_and_gradevaluates counts inn_log_prob, and inn_gradwhere its value is finite, as withgrad.- log_prob(x)¶
Log-densities at the rows of
x; raisesValueErroron NaN or+inf.
- log_prob_and_grad(x, *, start=False)¶
Log-densities and gradients at the rows of
x.One call of
log_prob_and_gradevaluates both at every row; otherwisegradis 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 inn_grad_zeroed; at the states that chainsstartfrom, it raisesValueErrorwith the point.