Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/notebooks/book2/11/rejection_sampling_demo.ipynb
1193 views
Kernel: Python [conda env:py3713]
# -*- coding: utf-8 -*- from math import floor from numpy import arange from scipy.stats import gamma import matplotlib.pyplot as plt import os import seaborn as sns try: import probml_utils as pml except ModuleNotFoundError: %pip install -qq git+https://github.com/probml/probml-utils.git import probml_utils as pml
os.environ["LATEXIFY"] = "" os.environ["FIG_DIR"] = "figures" pml.latexify(width_scale_factor=2)
alpha = 5.7 lam = 2 k = floor(alpha) M = gamma.pdf(alpha - k, alpha, scale=1 / lam) / gamma.pdf(alpha - k, k, scale=1 / (lam - 1)) xs = arange(0, 10, 0.01) fig, ax = plt.subplots() ax.set_xlim(0, 10) ax.set_ylim(0, 1.4) ax.plot(xs, gamma.pdf(xs, alpha, scale=1 / lam), "b-", label="target p(x)", lw=1) ax.plot(xs, M * gamma.pdf(xs, k, scale=1 / (lam - 1)), "r:", label="comparison\nfunction Mq(x)", lw=1.5) ax.legend(frameon=False, bbox_to_anchor=(0.5, 0.2), fontsize=7) sns.despine() pml.savefig("rejectionSamplingDemo_latexified") plt.show()
saving image to figures/rejectionSamplingDemo_latexified.pdf Figure size: [3. 1.5]
Image in a Jupyter notebook