Carrier A's delivery times are normal, with a mean of 4.0 days and a spread of 1.2. The shop's delivery model says 4.5 and 1.5. Check the formula against an integral on a grid:
def normal_logpdf(x, mu, sigma):
return -0.5 * np.log(2 * np.pi * sigma**2) - (x - mu) ** 2 / (2 * sigma**2)
# carrier A's delivery days
mu_p, s_p = 4.0, 1.2
# the delivery model's normal
mu_q, s_q = 4.5, 1.5
x = np.linspace(-6, 15, 20_001)
lp, lq = normal_logpdf(x, mu_p, s_p), normal_logpdf(x, mu_q, s_q)
on_grid = np.trapezoid(np.exp(lp) * (lp - lq), x)
closed = kl_normal(mu_p, s_p, mu_q, s_q)
print(f"KL on a grid {on_grid:.6f}, closed form {closed:.6f}")
KL on a grid 0.098699, closed form 0.098699
The grid works in log densities: it subtracts log q from log p instead of dividing one density by the other, so a density that underflows in a tail can't turn into a 0 / 0.
In the editor, write kl_normal(mu1, s1, mu2, s2) from the closed form. It prints carrier A against the delivery model.