Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/notebooks/book1/02/student_laplace_pdf_plot.ipynb
1192 views
Kernel: Python [conda env:probml_murphy]

Univariate Gaussian, Laplace and Student t-distribution

import jax import jax.numpy as jnp import seaborn as sns import matplotlib.pyplot as plt try: from probml_utils import latexify, savefig, is_latexify_enabled except ModuleNotFoundError: %pip install -qq git+https://github.com/probml/probml-utils.git from probml_utils import latexify, savefig, is_latexify_enabled from jax.scipy.stats import t, norm, laplace
latexify(fig_width=0.45 * 6, fig_height=1.5)
/home/patel_zeel/miniconda3/envs/probml_murphy/lib/python3.9/site-packages/probml_utils/plotting.py:26: UserWarning: LATEXIFY environment variable not set, not latexifying warnings.warn("LATEXIFY environment variable not set, not latexifying")
x = jnp.linspace(-4, 4, 100) normal = norm.pdf(x, loc=0, scale=1) laplace_ = laplace.pdf(x, loc=0, scale=1 / (2**0.5)) student_t1 = t.pdf(x, df=1, loc=0, scale=1) student_t2 = t.pdf(x, df=2, loc=0, scale=1)
WARNING:absl:No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)
LEGEND_SIZE = 7 if is_latexify_enabled() else None plt.figure() ax = plt.gca() (t1_plot,) = plt.plot(x, student_t1, "b--", label="Student\n" + r"$(\nu=1)$") (t2_plot,) = plt.plot(x, student_t2, "g--", label="Student\n" + r"$(\nu=2)$") (norm_plot,) = plt.plot(x, normal, "k:", label="Gaussian") (laplace_plot,) = plt.plot(x, laplace_, "r-", label="Laplace") legend1 = plt.legend(handles=[t1_plot, norm_plot], loc="upper right", prop={"size": LEGEND_SIZE}) ax.add_artist(legend1) legend2 = plt.legend(handles=[t2_plot, laplace_plot], loc="upper left", prop={"size": LEGEND_SIZE}) ax.add_artist(legend2) plt.ylabel("pdf") plt.xlabel("$x$") sns.despine() savefig("studentLaplacePdf2.pdf")
/home/patel_zeel/miniconda3/envs/probml_murphy/lib/python3.9/site-packages/probml_utils/plotting.py:79: UserWarning: set FIG_DIR environment variable to save figures warnings.warn("set FIG_DIR environment variable to save figures")
Image in a Jupyter notebook
plt.figure() plt.plot( x, jnp.log(normal), "k:", x, jnp.log(student_t1), "b--", x, jnp.log(student_t2), "g--", x, jnp.log(laplace_), "r-" ) plt.ylabel("log pdf") plt.xlabel("$x$") plt.legend(("Gaussian", "Student " + r"$(\nu=1)$", "Student " + r"$(\nu=2)$", "Laplace"), prop={"size": LEGEND_SIZE}) sns.despine() savefig("studentLaplaceLogpdf2.pdf")
/home/patel_zeel/miniconda3/envs/probml_murphy/lib/python3.9/site-packages/probml_utils/plotting.py:79: UserWarning: set FIG_DIR environment variable to save figures warnings.warn("set FIG_DIR environment variable to save figures")
Image in a Jupyter notebook

Demo

You can visualize the effect of loc, scale, and degree of freedom on the above three distributions by changing them.

def make_graph(data): plt.figure(figsize=(8, 6)) x = jnp.linspace(-4, 4, 100) deg_of_freedom = data["dof"] loc = data["loc"] scale = data["scale"] student_t = t.pdf(x, df=deg_of_freedom, loc=loc, scale=scale) normal = norm.pdf(x, loc=loc, scale=scale) laplace_ = laplace.pdf(x, loc=loc, scale=scale) plt.plot(x, normal, "k:", x, student_t, "g--", x, laplace_, "r-") plt.ylim(0, 0.6) plt.legend(("Gaussian", "Student", "Laplace"), bbox_to_anchor=(1.1, 1)) sns.despine()
from ipywidgets import interact @interact(dof=(0.1, 10), loc=(-1.1, 1.1), scale=(0.7, 2)) def generate_random(dof, loc, scale): data = {} data["dof"] = dof data["loc"] = loc data["scale"] = scale make_graph(data);
interactive(children=(FloatSlider(value=5.05, description='dof', max=10.0, min=0.1), FloatSlider(value=0.0, de…