Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/notebooks/book2/11/ars_envelope.ipynb
1193 views
Kernel: Python 3 (ipykernel)
# -*- coding: utf-8 -*- # based on https://github.com/probml/pmtk3/blob/master/demos/arsEnvelope.m # author : Ang Ming Liang import numpy as np import matplotlib.pyplot as plt import matplotlib %matplotlib inline try: from scipy.stats import norm except ModuleNotFoundError: %pip install -qq scipy.stats from scipy.stats import norm try: from probml_utils import savefig, latexify except ModuleNotFoundError: %pip install -qq git+https://github.com/probml/probml-utils.git from probml_utils import savefig, latexify
latexify(width_scale_factor=3, fig_height=1.5)
xs = np.linspace(-1.5, 1.5, 50) ps = norm.logpdf(xs) dy_dx = lambda x: -x fig, ax = plt.subplots() # Hide the right and top spines ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) # Fix the plot size ax.set_xlim(-1.6, 1.6) ax.set_ylim(-2.2, -0.8) # Plotting the log-concave distribution ax.plot(xs, ps, "b-", linewidth=3) # At x=-0.7 x1 = -0.7 ratio1 = dy_dx(x1) ax.plot([x1 - 0.5, x1 + 0.5], [norm.logpdf(x1) - 0.5 * ratio1, norm.logpdf(x1) + 0.5 * ratio1], "-r", "LineWidth", 3) ax.plot([x1, x1], [norm.logpdf(x1), -2.2], "--r", "LineWidth", 3) # At x=0 x2 = 0 ratio2 = dy_dx(x2) ax.plot([x2 - 0.5, x2 + 0.5], [norm.logpdf(x2) - 0.5 * ratio2, norm.logpdf(x2) + 0.5 * ratio2], "-r", "LineWidth", 3) ax.plot([x2, x2], [norm.logpdf(x2), -2.2], "--r", "LineWidth", 3) # At x=0.7 x3 = 0.7 ratio3 = dy_dx(x3) ax.plot([x3 - 0.5, x3 + 0.5], [norm.logpdf(x3) - 0.5 * ratio3, norm.logpdf(x3) + 0.5 * ratio3], "-r", "LineWidth", 3) ax.plot([x3, x3], [norm.logpdf(x3), -2.2], "--r", "LineWidth", 3) # Remove the x-axis and y-axis ticks plt.xticks([]) plt.yticks([]) savefig("ars_envelope") plt.show()
saving image to ./ars_envelope_latexified.pdf Figure size: [2. 1.5]
Image in a Jupyter notebook