Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/notebooks/book1/02/binom_dist_plot.ipynb
1193 views
Kernel: Python 3 (ipykernel)

Plots the pmfs of binomial distributions with varying probability of success parameter

import jax.numpy as jnp import jax 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 scipy.stats import binom
latexify(width_scale_factor=2, fig_height=1.5)
/home/patel_zeel/miniconda3/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")
N = 10 thetas = [0.25, 0.5, 0.75, 0.9] x = jnp.arange(0, N + 1) def make_graph(data): plt.figure() x = data["x"] n = data["n"] theta = data["theta"] probs = binom.pmf(x, n, theta) title = r"$\theta=" + str(theta) + "$" plt.bar(x, probs, align="center") plt.xlim([min(x) - 0.5, max(x) + 0.5]) plt.ylim([0, 0.4]) plt.xticks(x) plt.xlabel("$x$") plt.ylabel("$p(x)$") plt.title(title) sns.despine() savefig("binomDistTheta" + str(int(theta * 100)) + ".pdf") for theta in thetas: data = {"x": x, "n": N, "theta": theta} make_graph(data)
WARNING:absl:No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.) /home/patel_zeel/miniconda3/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 notebookImage in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebook

Demo

You can see different examples of binomial distributions by changing the theta in the following demo.

from ipywidgets import interact @interact(theta=(0.1, 0.9)) def generate_random(theta): n = 10 data = {"x": jnp.arange(0, n + 1), "n": n, "theta": theta} make_graph(data)
interactive(children=(FloatSlider(value=0.5, description='theta', max=0.9, min=0.1), Output()), _dom_classes=(…