#!/usr/bin/env python3
# Source: t3-probability-information.html
import numpy as np

rng = np.random.default_rng(1)
n = 100_000

# Goal: P(X > 4) for X ~ N(0, 1). True value is about 3.167e-5.
hits = rng.standard_normal(n) > 4
naive = hits.mean()

# Importance sampling: draw from q = N(4, 1), reweight by w = p(x) / q(x).
x = rng.normal(4.0, 1.0, n)
w = np.exp(-4.0 * x + 8.0)          # p(x) / q(x) for these two Gaussians
terms = (x > 4) * w
est = terms.mean()

print(f"naive      : {naive:.3e}  ({hits.sum()} hits)")
print(f"importance : {est:.3e}  rel. std err {terms.std() / np.sqrt(n) / est:.2%}")
# naive      : 2.000e-05  (2 hits)
# importance : 3.162e-05  rel. std err 0.67%
