Approximate a 1-D target density by a single Gaussian in two ways, working on a fixed grid of points :
Implement:
fit_forward_kl(grid, p) returning (mu, sigma, kl): the Gaussian minimizing the forward (inclusive) KL . Use the fact that for this family the minimizer matches the mean and standard deviation of the gridded ; kl is at that fit (with gridded as above).fit_reverse_kl(grid, p, mus, sigmas) returning (mu, sigma, kl): the candidate minimizing the reverse (exclusive) KL over the grid of all pairs from mus and sigmas. Loop over mus in the outer loop and sigmas in the inner loop; a later pair replaces the current best only if its KL is smaller by more than (so near-ties keep the earlier pair).p holds nonnegative density values at the grid points. Return plain floats.