PyVBMC Example 6: Noisy log-likelihood evaluations#
In this notebook, we demonstrate running PyVBMC with noisy evaluations of the log-likelihood. Here we emulate a noisy scenario by adding random Gaussian noise to the log-likelihood. In practice, noisy evaluations often emerge from estimation techniques for simulation-based models, such as Inverse Binomial Sampling (IBS). Its Python implementation, PyIBS, is one of the lab’s tools for model fitting. However the noise estimate is obtained, it is allowed to be (and in practice, often is) heteroskedastic: that is, the amount of noise in the estimate may vary across parameter space.
This notebook is Part 6 of a series of notebooks in which we present various example usages for VBMC with the PyVBMC package. The code used in this example is available as a script here.
import sys
if "google.colab" in sys.modules: # Colab lacks PyVBMC: install it
%pip install --upgrade pyvbmc
import numpy as np
import scipy.stats as scs
from pyvbmc import VBMC
import matplotlib.pyplot as plt
1. Model definition: log-prior, noisy log-likelihood and log-joint#
We use the same toy target function as in Example 1, a broad Rosenbrock’s banana function in \(D = 2\). But this time, we add random heteroskedastic Gaussian noise to the function evaluations.
D = 2 # We'll use a 2-D problem, again for speed
prior_mu = np.zeros(D)
prior_var = 3 * np.ones(D)
LB = np.full((1, D), -np.inf) # Lower bounds
UB = np.full((1, D), np.inf) # Upper bounds
PLB = np.full((1, D), prior_mu - np.sqrt(prior_var)) # Plausible lower bounds
PUB = np.full((1, D), prior_mu + np.sqrt(prior_var)) # Plausible upper
def log_prior(theta):
"""Multivariate normal prior on theta, same as before."""
cov = np.diag(prior_var)
return scs.multivariate_normal(prior_mu, cov).logpdf(theta)
The setup and prior function remain unchanged. For the likelihood, we’ll take the Rosenbrock function and add some simulated noise to it. The noise here is arbitrary, but notice that the amount depends on the parameters \(\theta\), with more noise when \(\theta\) is farther from the origin: It is typical to have noisier estimates in lower-density regions of the posterior.
In the noisy-likelihood setting, the log-joint (or log-likelihood, if the prior is passed to PyVBMC separately) should return a pair of values: the noisy log-density as the first, and an estimate of the standard deviation of the noise as the second.
# log-likelihood (Rosenbrock)
def noise_free_log_likelihood(theta):
"""D-dimensional Rosenbrock's banana function."""
theta = np.atleast_2d(theta)
x, y = theta[:, :-1], theta[:, 1:]
return -np.sum((x**2 - y) ** 2 + (x - 1) ** 2 / 100, axis=1)
def log_likelihood(theta):
"""Rosenbrock log-likelihood with synthetic noise."""
theta = np.atleast_2d(theta)
noise_sd = np.sqrt(1.0 + 0.5 * np.linalg.norm(theta, axis=1) ** 2)
noisy_estimate = noise_free_log_likelihood(
theta
) + noise_sd * np.random.normal(size=theta.shape[0])
return noisy_estimate, noise_sd
Since the log-likelihood function now has two outputs, we cannot directly sum the log-prior and log-likelihood. So our log-joint function looks slightly different. If you choose to pass the log-likelihood and log-prior separately, PyVBMC will handle this part for you.
# Full model:
def log_joint(theta, data=np.ones(D)):
"""log-density of the joint distribution."""
log_p = log_prior(theta)
log_l, noise_est = log_likelihood(theta)
# For the joint, we have to add log densities and carry-through the noise estimate.
return log_p + log_l, noise_est
x0 = np.zeros((1, D)) # Initial point
We omit optimizing x0 to find a good starting point, as previously suggested, because naive optimization would fail badly for a noisy objective anyway. For noisy optimization, consider using PyBADS, the Python port of BADS from the lab’s model-fitting toolkit.
2. Setting up a noisy VBMC instance#
As a final step, we have to tell PyVBMC that we are working with a noisy likelihood, by turning on the specify_target_noise option. Otherwise PyVBMC will throw an InvalidFuncValue error, indicating that the target function is returning unexpected values.
np.random.seed(42)
options = {"specify_target_noise": True}
vbmc = VBMC(
log_joint,
x0,
LB,
UB,
PLB,
PUB,
options=options,
)
vp, results = vbmc.optimize()
Beginning variational optimization assuming NOISY observations of the log-joint
Performance settings: saved calibration.
Tip: Use vp.plot() to inspect parameter uncertainty and trade-offs after fitting. Broad marginal distributions indicate uncertainty; tilted or curved joint contours can reveal parameter trade-offs.
https://acerbilab.org/pyvbmc/api/classes/variational_posterior.html#pyvbmc.VariationalPosterior.VariationalPosterior.plot
Iteration f-count Mean[ELBO] Std[ELBO] sKL-iter[q] K[q] Convergence Action
0 10 -1.47 0.32 14118.20 2 inf start warm-up
1 15 -2.31 0.32 0.03 2 inf
2 20 -1.32 0.43 1.59 2 38.8
3 25 -1.66 0.30 0.14 2 3.96
4 30 -1.91 0.26 0.09 2 2.75 end warm-up
5 35 -2.11 0.23 0.10 2 2.8
6 40 -2.29 0.20 0.14 2 3.58
7 45 -2.19 0.22 0.07 3 2.08
8 50 -1.78 0.46 0.63 4 15.8
9 55 -1.78 0.25 0.02 4 0.668
10 60 -1.79 0.21 0.01 7 0.469
11 65 -1.76 0.20 0.00 10 0.247 rotoscale, undo rotoscale
12 70 -1.68 0.19 0.01 11 0.414
13 75 -1.61 0.20 0.03 11 1
14 80 -1.72 0.17 0.02 12 0.784
15 85 -1.70 0.17 0.00 12 0.206
16 90 -1.65 0.16 0.00 12 0.266
17 95 -1.68 0.16 0.00 13 0.222
18 100 -1.70 0.15 0.00 13 0.19
19 105 -1.67 0.16 0.00 13 0.243
20 110 -1.68 0.15 0.00 16 0.199
21 115 -1.64 0.14 0.00 16 0.225
22 120 -1.67 0.14 0.00 17 0.229 rotoscale, undo rotoscale
23 125 -1.66 0.14 0.01 16 0.26
24 130 -1.67 0.14 0.01 16 0.308 stable
inf 130 -1.67 0.14 0.00 50 0.308 finalize
Inference terminated: variational solution stable for options.tol_stable_count fcn evaluations.
Estimated ELBO: -1.670 +/-0.139.
PyVBMC found a solution, even in the presence of noise. Let’s compare it to a fit of the same problem without noise, as in Example 4.
def noise_free_log_joint(theta):
return log_prior(theta) + noise_free_log_likelihood(theta)
noise_free_vbmc = VBMC(
noise_free_log_joint,
x0,
LB,
UB,
PLB,
PUB,
seed=42,
options={"display": "off"},
)
noise_free_vp, noise_free_results = noise_free_vbmc.optimize()
# KL divergence between this VP and the noise-free VP:
print(vp.kl_div(vp2=noise_free_vp))
[0.08043828 0.10800525]
The KL divergence in both directions is small. We can also compare the densities visually and see that they are quite similar despite the noisy target:
vp.plot(title="Noisy VP");
noise_free_vp.plot(title="Noise-Free VP");
3. Conclusions#
In this notebook, we have illustrated how to use PyVBMC on a model with a noisy likelihood. Even in the presence of significant noise, PyVBMC can find a good variational solution in relatively few function evaluations.
Acknowledgments#
Work on the PyVBMC package is supported by the Research Council of Finland (grants 356498 and 358980 to Luigi Acerbi) and its Flagship programme: Finnish Center for Artificial Intelligence FCAI.