GradientCheck

class telescopic.diagnostics.GradientCheck(draw, gap, tolerance, predicted, n_skipped, n_unusable)

What the gradient check of a sampler built with a gradient compared.

At each of 8 draws \(x\) of \(\pi_0\) the check compares, along the direction \(u\) between two further draws, the change of \(\log\pi\) that the gradient predicts with central differences of the log-density at steps \(h u\), \(h = \epsilon^{1/3}\) for the epsilon \(\epsilon\) of the dtype of the log-density’s values:

\[\Delta = \big|\nabla\log\pi(x)\cdot u - D_u\big|, \qquad D_u = \frac{\log\pi(x + hu) - \log\pi(x - hu)}{2h},\]

and a line disagrees when \(\Delta\) exceeds its tolerance, 10 times the estimated error of the comparison: the truncation of the differences, from a second difference at \(2hu\), and the rounding of the log-density and of the gradient. All the figures are in nats along \(u\), across the spread of \(\pi_0\). The gradient and the log-density are grad and log_prob, or the two parts of log_prob_and_grad.

Attributes:
drawndarray of int, shape (n,)

Which of the 8 draws, numbered from 1, each compared line starts from.

gapndarray, shape (n,)

\(\Delta\) of each line.

tolerancendarray, shape (n,)

The tolerance of each line.

predictedndarray, shape (n,)

The change \(\nabla\log\pi(x)\cdot u\) that the gradient predicts on each line.

n_skippedint

Draws skipped because the density is zero or the gradient is not finite there.

n_unusableint

Lines not compared because they reach zero density, or because their steps round away in the dtype of log_prob, as with a float32 log_prob and a start whose spread is tiny beside its distance from 0.

property ratio

The gap of each line over its tolerance, which sizes each line’s disagreement.

inf for a gap above a tolerance of 0, and 0 for a gap of 0. The check decides on gap > tolerance itself, and uses the ratio only to choose the line that its warning describes.

Returns:
ndarray, shape (n,)