<?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 Eeshan Mishra on Medium]]></title>
        <description><![CDATA[Stories by Eeshan Mishra on Medium]]></description>
        <link>https://medium.com/@mishra-eeshan?source=rss-cb2099252507------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-VssYFidbaSzOcGWZU-FSg.jpeg</url>
            <title>Stories by Eeshan Mishra on Medium</title>
            <link>https://medium.com/@mishra-eeshan?source=rss-cb2099252507------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 15:15:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mishra-eeshan/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[Monte-Carlo Sampling — Coding Experiment]]></title>
            <link>https://mishra-eeshan.medium.com/monte-carlo-sampling-coding-experiment-a9f77d4f8f34?source=rss-cb2099252507------2</link>
            <guid isPermaLink="false">https://medium.com/p/a9f77d4f8f34</guid>
            <category><![CDATA[random]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[sampling]]></category>
            <category><![CDATA[monte-carlo]]></category>
            <category><![CDATA[statistics]]></category>
            <dc:creator><![CDATA[Eeshan Mishra]]></dc:creator>
            <pubDate>Sat, 19 Jun 2021 18:32:19 GMT</pubDate>
            <atom:updated>2021-06-19T18:32:19.269Z</atom:updated>
            <content:encoded><![CDATA[<h3>Monte-Carlo Sampling — Coding Experiment</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/1*RdlzeVfJsuWe1ktRg6f6FA.png" /></figure><p>Sampling, as we all know is the task of selecting one or more samples from a given set of values. Random sampling is a sampling technique in which each sample has an equal probability of selection from a given set of values. While randomness sounds good on paper, it has always been difficult to generate a truly random number because of the deterministic nature of the computing system we all use. Folks in MIT have explained it better — Read here — Can a computer generate a truly random number? Most of the libraries including, Numpy use pseudo-random number generators. While pseudo-random numbers are fairly random up to a certain number of iterations, they repeat at some point.</p><p><a href="https://en.wikipedia.org/wiki/Monte_Carlo_method">Monte-Carlo Sampling </a>refers to a set of techniques where repeated samples are drawn at random to measure or obtain some results. Take an example of a simple and boring dice game. The probability that we will get any number, say a six (6) in our first throw is 1/6. In practice, randomly throwing a dice several times (say 10 times) provides us results that don’t match our theoretical expectations. For example, it is very much possible that out of 10 throws, we get a six only once, meaning that the probability of getting a six from our ten observations is 1/10 or 0.1. Clearly, something doesn’t seem to be right here. It turns out that as we increase the number of random selections, the probability tends to approach our theoretical value, which is 1/6.</p><p>Interesting? Let’s see this in a simple code-based experiment:</p><p>First, we define a simple class <em>randsamp</em>. This class will take two arguments, i.e. ‘<em>max_range</em>‘ and ‘<em>iterations</em>‘. ‘<em>Max_range </em>‘ sets the upper bound (exclusive) for our domain of values from which a random sample will be drawn in each iteration. The lower bound is kept as 1 (inclusive). iterations is another argument that is the same as the number of times one would throw a dice. Our assumption is that the probability of getting any of the six numbers will increase with the number of throws.</p><pre><strong>class randsamp:<br>    def __init__(self, max_range, iterations):<br>        self.maxrange = max_range<br>        self.iter = iterations</strong></pre><p>This class will include two functions, <em>sampler</em> and <em>prob</em>. While <em>sampler</em> throws the dice and records the random number in a list (<em>self.res</em>), <em>prob</em> calculates the probability for a given number of iterations.</p><pre><strong>def sampler(self):<br>        np = __import__(&#39;numpy&#39;)<br>        self.res = []<br>        for i in range(0,self.iter):<br>            self.res.append(np.random.randint(1, self.maxrange))<br>        return self.res<br>    def prob(self, integer):<br>        self.integer = integer<br>        self.count = 0<br>        for i in self.res:<br>            if i == self.integer:<br>                self.count += 1<br>        return self.count, self.count/len(self.res)</strong></pre><p>After running this code, we are ready to move to our next step. We will start with 1 throw, then we will increase the number of throws to 10, 100, 500, 1000, 10000, and then to 100000. For every set of throws, we will record our probability and count.</p><pre><strong>for jump in [1,10,100,500,1000, 10000, 100000]:<br>    x = randsamp(7,jump) #initiate the class and we pass 7 as Maxrange and i is the number of iterations<br>    samples = x.samp()<br>    rescount,prob = x.prob(5)<br>    print(f&#39;count of 5 in {jump} iteration(s) = {rescount}, prob of getting 5 = {prob}&#39;)</strong></pre><p>This is the result. As you can see, increasing the number of throws helps us to reach closer to 1/6 (which is 0.16667).</p><p>count of 5 in 1 iteration(s) = 0, prob of getting 5 = 0.0<br>count of 5 in 10 iteration(s) = 1, prob of getting 5 = 0.1<br>count of 5 in 100 iteration(s) = 18, prob of getting 5 = 0.18<br>count of 5 in 500 iteration(s) = 66, prob of getting 5 = 0.132<br>count of 5 in 1000 iteration(s) = 168, prob of getting 5 = 0.168<br>count of 5 in 10000 iteration(s) = 1680, prob of getting 5 = 0.168<br>count of 5 in 100000 iteration(s) = 16751, prob of getting 5 = 0.16751</p><p>If this doesn’t look impressive, let’s take this a step ahead. We will iterate in sets of 1,10, 100,500,1000, 2000, 3000, 5000, 10000, 50000, 100000. For each set of throws, we will record the probability in a list (<em>prob</em>).</p><pre><strong>import numpy as np<br>prob = []<br>for jump in [1,10, 100,500,1000, 2000, 3000, 5000, 10000, 50000, 100000]:<br>    x = randsamp(7,jump) #initiate the class and we pass 7 as Maxrange and i is the number of iterations<br>    samples = x.samp()<br>    rescount,likelihood = x.prob(5)<br>    prob.append(likelihood)</strong></pre><p>Let&#39;s try to plot a graph this time and see how the probability compares to each set of iterations.</p><pre><strong>from matplotlib import pyplot as plt</strong><br><strong>plt.plot(prob,color=&#39;green&#39;)<br>plt.xlabel(&#39;Sets of Iterations&#39;)<br>plt.ylabel(&#39;Probability of Getting a 5&#39;)<br>plt.plot(1/6*np.ones(10), color=&#39;red&#39;, linestyle=&#39;dotted&#39;)<br>plt.show()</strong></pre><figure><img alt="Probability for a dice throwing experiment reaching its expected value (0.16667)" src="https://cdn-images-1.medium.com/max/640/1*8hhJksB4vajd3pGcRfpPZA.png" /><figcaption>Probability for a dice throwing experiment reaching its expected value (0.16667)</figcaption></figure><p>As we can see, the probability (green line) tends to approach its expected theoretical value of 0.16667 (in red dotted line).</p><p>Thank you for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9f77d4f8f34" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>