<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amit Goyal on Medium]]></title>
        <description><![CDATA[Stories by Amit Goyal on Medium]]></description>
        <link>https://medium.com/@amit.kr.goyal?source=rss-f789b84ed6e8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qhUI2afKiiBb5uYNmfakDA.jpeg</url>
            <title>Stories by Amit Goyal on Medium</title>
            <link>https://medium.com/@amit.kr.goyal?source=rss-f789b84ed6e8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 00:31:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amit.kr.goyal/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Visualising Probability Distributions in Python]]></title>
            <link>https://medium.com/@amit.kr.goyal/visualising-probability-distributions-in-python-ebef944489bc?source=rss-f789b84ed6e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/ebef944489bc</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[probability]]></category>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[random-variable]]></category>
            <category><![CDATA[distribution]]></category>
            <dc:creator><![CDATA[Amit Goyal]]></dc:creator>
            <pubDate>Tue, 26 Sep 2023 12:51:38 GMT</pubDate>
            <atom:updated>2026-04-16T06:38:57.588Z</atom:updated>
            <content:encoded><![CDATA[<p>In what follows, we’ll learn how to visualize the PMF/PDF of important probability distributions in python.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VMsf60eZFfK_eQgI_q7CuQ.png" /></figure><p>Here are the necessary imports.</p><pre>import matplotlib.pyplot as plt<br>import numpy as np</pre><p>Bernoulli distribution:</p><pre>from scipy.stats import binom<br><br>n=1<br>x = np.arange(0,n+1,1)<br>xs = [str(x[i]) for i in range(len(x))]<br>p = [0.3,0.5,0.7]<br>width = 0.35  # the width of the bars<br>cols = [&#39;r&#39;, &#39;b&#39;, &#39;g&#39;]<br>fig, axs = plt.subplots(1, len(p), figsize=(3*len(p), 3), sharey=True)<br>for i in range(len(p)):<br>    axs[i].bar(xs, binom.pmf(x, n, p[i]), alpha=0.5, color=cols[i])<br>    axs[i].set_title(&#39;Bernoulli PMF for &#39; + r&#39;$p={}$&#39;.format(p[i]))<br>fig.text(0.5, 0.04, r&#39;$x\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;)<br>fig.text(0.06, 0.5, r&#39;$p_X\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;, rotation=&#39;vertical&#39;)<br><br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FpPaIjWX7zsfKs9ziWSigw.png" /></figure><p>Binomial distribution:</p><pre>from scipy.stats import binom<br><br>n=10<br>x = np.arange(0,11,1)<br>p1=0.3<br>p2=0.7<br>y1 = binom.pmf(x, n, p1)<br>y2 = binom.pmf(x, n, p2)<br><br>width = 0.35  # the width of the bars<br><br>fig, ax = plt.subplots()<br>ax.grid()<br>rects1 = ax.bar(x - width/2, y1, width, alpha = 0.6, label=&#39;$p={}$&#39;.format(p1))<br>rects2 = ax.bar(x + width/2, y2, width, alpha = 0.6, label=&#39;$p={}$&#39;.format(p2))<br><br># Add some text for labels, title and custom x-axis tick labels, etc.<br>ax.set_ylabel(r&#39;$p_X\rightarrow$&#39;)<br>ax.set_xlabel(r&#39;$x\rightarrow$&#39;)<br>ax.set_title(&#39;Binomial PMFs when $n=10$ and $p={},{}$&#39;.format(p1,p2))<br>ax.legend()<br><br>fig.tight_layout()<br><br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*a4t5XUcs9stan0t0bhN_EQ.png" /></figure><pre>from scipy.stats import binom<br><br>n=10<br>x = np.arange(0,n+1,1)<br>xs = [str(x[i]) for i in range(len(x))]<br>p = [0.3,0.5,0.7]<br>width = 0.35  # the width of the bars<br>cols = [&#39;r&#39;, &#39;b&#39;, &#39;g&#39;]<br>fig, axs = plt.subplots(1, len(p), figsize=(4*len(p), 4), sharey=True)<br>for i in range(len(p)):<br>    axs[i].bar(xs, binom.pmf(x, n, p[i]), alpha=0.5, color=cols[i])<br>    axs[i].set_title(&#39;Binomial PMF for $n={}$, $p={}$&#39;.format(n,p[i]))<br><br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AylL6Rws6zi896lss6mfHw.png" /></figure><p>Geometric distribution:</p><pre>from scipy.stats import geom<br><br>n=10<br>x = np.arange(1,n+1,1)<br>xs = [str(x[i]-1) for i in range(len(x))]<br>p = [0.3,0.5,0.7]<br>width = 0.35  # the width of the bars<br>cols = [&#39;r&#39;, &#39;b&#39;, &#39;g&#39;]<br>fig, axs = plt.subplots(1,len(p), figsize=(4*len(p),4), sharey=True)<br>for i in range(len(p)):<br>    axs[i].bar(xs, geom.pmf(x, p[i]), alpha=0.5, color=cols[i])<br>    axs[i].set_title(&#39;Geometric PMF for $p={}$&#39;.format(p[i]))<br>    axs[i].grid()<br>fig.text(0.5, 0.04, r&#39;$x\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;)<br>fig.text(0.06, 0.5, r&#39;$p_X\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;, rotation=&#39;vertical&#39;)<br><br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ziwU1s3nKuLTGE-_cKknyw.png" /></figure><p>Poisson distribution:</p><pre>from scipy.stats import poisson<br><br>n=10<br>x = np.arange(0,n+1,1)<br>xs = [str(x[i]) for i in range(len(x))]<br>param = [1,2,5]<br>width = 0.35  # the width of the bars<br>cols = [&#39;r&#39;, &#39;b&#39;, &#39;g&#39;]<br>fig, axs = plt.subplots(1,len(param), figsize=(4*len(param),4), sharey=True)<br>for i in range(len(p)):<br>    axs[i].bar(xs, poisson.pmf(x, param[i]), alpha=0.5, color=cols[i])<br>    axs[i].set_title(&#39;Poisson PMF for $\lambda={}$&#39;.format(param[i]))<br><br>fig.text(0.5, 0.04, r&#39;$x\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;)<br>fig.text(0.06, 0.5, r&#39;$p_X\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;, rotation=&#39;vertical&#39;)<br><br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NBE4BkEVpN-eg6yTTUPZ9w.png" /></figure><p>Negative Binomial distribution:</p><pre>from scipy.stats import nbinom<br><br>r = np.array([2, 5, 10])<br>p = 0.5<br>cols = [&#39;r&#39;, &#39;b&#39;, &#39;g&#39;]<br>fig, ax = plt.subplots(1,len(r),figsize=(4*len(r),4), sharey=True)<br>x = np.arange(nbinom.ppf(0.01, r, p).min(), nbinom.ppf(0.99, r, p).max())<br>for i in range(len(r)):<br>    rv = nbinom(r[i], p)<br>    ax[i].bar(x, rv.pmf(x), alpha=0.5, color=cols[i])<br>    ax[i].set_title(&#39;Neg. binomial PMF for $r={}, p={}$&#39;.format(r[i], p))<br><br>fig.text(0.5, 0.04, r&#39;$x\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;)<br>fig.text(0.06, 0.5, r&#39;$p_X\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;, rotation=&#39;vertical&#39;)<br><br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ps9AZp5xmEre_7KslRTrSQ.png" /></figure><p>Hypergeometric distribution:</p><pre>from scipy.stats import hypergeom<br><br>N, m = 52, 4<br>n = [5, 15, 25]<br>x = np.arange(0, m+1)<br><br>cols = [&#39;r&#39;, &#39;b&#39;, &#39;g&#39;]<br>fig, ax = plt.subplots(1,len(n),figsize=(4.8*len(n),4), sharey=True)<br><br>for i in range(len(n)):<br>    rv = hypergeom(N, m, n[i])<br>    ax[i].bar(x, rv.pmf(x), alpha=0.5, color=cols[i])<br>    ax[i].set_title(&#39;PMF of number of aces for $n={}$&#39;.format(int(n[i])))<br>    ax[i].grid()<br><br>fig.text(0.5, 0.04, r&#39;$x\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;)<br>fig.text(0.09, 0.5, r&#39;$p_X\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;, rotation=&#39;vertical&#39;)<br><br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ziKA-_ESvp5I2GtwI514Rg.png" /></figure><p>Discrete Uniform distribution:</p><pre>from scipy.stats import randint<br><br>low = [1,-3,0]<br>high = [6,5,21]<br><br>cols = [&#39;r&#39;, &#39;b&#39;, &#39;g&#39;]<br>fig, ax = plt.subplots(1,len(low),figsize=(4.8*len(low),4), sharey=True)<br><br>for i in range(len(low)):<br>    rv = randint(low[i], high[i])<br>    x = np.arange(low[i], high[i])<br>    ax[i].bar(x, rv.pmf(x), alpha=0.5, color=cols[i])<br>    ax[i].set_title(&#39;PMF of DUnif over ${},{},\ldots, {}$&#39;.format(x.min(),x.min()+1, x.max()))<br>    ax[i].grid()<br><br>fig.text(0.5, 0.04, r&#39;$x\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;)<br>fig.text(0.08, 0.5, r&#39;$p_X\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;, rotation=&#39;vertical&#39;)<br><br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R63Ug9M7IqRwGkjdGSIHig.png" /></figure><p>Uniform distribution:</p><pre>from scipy.stats import uniform<br><br>fig, ax = plt.subplots(1,2,figsize=(4.8*2,4), sharey=True)<br><br>a = 2<br>b = 4<br>rv = uniform(a, b-a)<br>x = np.arange(a, b, 0.01)<br>ax[0].plot(x, rv.pdf(x), alpha=0.5, color=cols[0])<br>ax[0].set_title(&#39;PDF of Unif over $[{},{}]$&#39;.format(a,b))<br>x = np.arange(a-0.2, a, 0.01)<br>ax[0].plot(x, rv.pdf(x), alpha=0.5, color=cols[0])<br>x = np.arange(b+0.01, b+0.2, 0.01)<br>ax[0].plot(x, rv.pdf(x), alpha=0.5, color=cols[0])<br>ax[0].scatter([a,b], rv.pdf([a,b]), s=50, alpha=0.5, color=cols[0])<br>ax[0].scatter([a,b], rv.pdf([a-0.01,b+0.01]), s=50, facecolors=&#39;none&#39;, edgecolors=&#39;r&#39;)<br>ax[0].plot([a,a], rv.pdf([a-0.01,a]), &#39;--&#39;, alpha=0.5, color=cols[0])<br>ax[0].plot([b,b], rv.pdf([b,b+0.01]), &#39;--&#39;, alpha=0.5, color=cols[0])<br>x = np.arange(a-0.2, b+0.2, 0.01)<br><br>ax[1].plot(x, rv.cdf(x), alpha=0.5, color=cols[1])<br>ax[1].set_title(&#39;CDF of Unif over $[{},{}]$&#39;.format(a,b))<br>ax[1].grid()<br><br>fig.text(0.5, 0.04, r&#39;$x\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;)<br>fig.text(0.08, 0.5, r&#39;$f_X\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;, rotation=&#39;vertical&#39;)<br>fig.text(0.52, 0.5, r&#39;$F_X\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;, rotation=&#39;vertical&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*95Ddbd0EXAWFhxusCGbABg.png" /></figure><p>Normal distribution:</p><pre>from scipy.stats import norm<br><br>fig, ax = plt.subplots(1,2,figsize=(4.8*2,4), sharey=True)<br><br>a = 0<br>b = [0.7,1,2]<br>x = np.arange(a-3*max(b), a+3*max(b), 0.01)<br>for i in range(len(b)):<br>    rv = norm(a, b[i])<br>    ax[0].plot(x, rv.pdf(x), alpha=0.5, color=cols[i],label=&#39;$N(\mu={},\sigma={})$&#39;.format(a,b[i]))<br>    ax[1].plot(x, rv.cdf(x), alpha=0.5, color=cols[i])<br>ax[0].set_title(&#39;PDF of Normal&#39;)<br>ax[1].set_title(&#39;CDF of Normal&#39;)<br>ax[1].grid()<br>ax[0].legend(bbox_to_anchor=(2.2, 1.0), loc=&#39;upper left&#39;)<br><br>fig.text(0.5, 0.04, r&#39;$x\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;)<br>fig.text(0.08, 0.5, r&#39;$f_X\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;, rotation=&#39;vertical&#39;)<br>fig.text(0.52, 0.5, r&#39;$F_X\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;, rotation=&#39;vertical&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gaqtfnUlqqUqCSJMr07ysA.png" /></figure><p>Exponential distribution:</p><pre>from scipy.stats import expon<br><br>la = [0.2,0.5,1]<br>x = np.arange(0, expon.ppf(0.99, scale = 1/min(la)), 0.01)<br><br>fig, ax = plt.subplots(1,2,figsize=(4.8*2,4), sharey=True)<br><br>for i in range(len(la)):<br>    rv = expon(scale = 1/la[i])<br>    ax[0].plot(x, rv.pdf(x), alpha=0.5, color=cols[i],label=&#39;Expo$(\lambda={})$&#39;.format(la[i]))<br>    ax[1].plot(x, rv.cdf(x), alpha=0.5, color=cols[i])<br>ax[0].set_title(&#39;PDF of Exponential&#39;)<br>ax[1].set_title(&#39;CDF of Exponential&#39;)<br>ax[1].grid()<br>ax[0].legend(bbox_to_anchor=(2.2, 1.0), loc=&#39;upper left&#39;)<br><br>fig.text(0.5, 0.04, r&#39;$x\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;)<br>fig.text(0.08, 0.5, r&#39;$f_X\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;, rotation=&#39;vertical&#39;)<br>fig.text(0.52, 0.5, r&#39;$F_X\rightarrow$&#39;, ha=&#39;center&#39;, va=&#39;center&#39;, rotation=&#39;vertical&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qsK-SI8AY1AxsTtr2A681w.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ebef944489bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>