<?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 Gabriel Gomes on Medium]]></title>
        <description><![CDATA[Stories by Gabriel Gomes on Medium]]></description>
        <link>https://medium.com/@gabrielgomesunifei?source=rss-347f12ef94a1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*JYAFboMlPfk9oT9x.jpg</url>
            <title>Stories by Gabriel Gomes on Medium</title>
            <link>https://medium.com/@gabrielgomesunifei?source=rss-347f12ef94a1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 10:48:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gabrielgomesunifei/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 Simulation For Humans]]></title>
            <link>https://medium.com/@gabrielgomesunifei/monte-carlo-simulation-for-humans-8849c47914de?source=rss-347f12ef94a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/8849c47914de</guid>
            <category><![CDATA[sampling-distribution]]></category>
            <category><![CDATA[simple-monte-carlo]]></category>
            <category><![CDATA[monte-carlo-method]]></category>
            <category><![CDATA[monte-carlo-simulation]]></category>
            <dc:creator><![CDATA[Gabriel Gomes]]></dc:creator>
            <pubDate>Fri, 03 Apr 2020 13:21:32 GMT</pubDate>
            <atom:updated>2020-04-03T21:58:27.739Z</atom:updated>
            <content:encoded><![CDATA[<h3>Analyzing the birthday paradox through Monte Carlo Simulation</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AGYjFxFRM33cjgcW" /><figcaption>Photo by <a href="https://unsplash.com/@macauphotoagency?utm_source=medium&amp;utm_medium=referral">Macau Photo Agency</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Monte Carlo is a method that relies on massive random sampling to obtain numerical results.This phrase is hard to understand at a first glance. So let’s take a look in an example:</p><p>Imagine that a professor of yours send you the following charade:</p><p><strong>How many people do you need to put inside a room to have a 50% chance of two people having a birthday on the same day?</strong></p><p>Then you ask him for the right answer. He says: 2<strong>3 people</strong>. At a first it seems not right. But stop for 30 seconds and take a breath. It makes sense. But to check it you have two options: make this calculation by hand or simulate it. So, Let’s simulate :).</p><p>Let’s create a scenario in which we take 23 aleatory numbers between 1 and 365 to simulate the days of the year. After that, let’s check if at least two numbers are equal (two people make anniversary in the same day). If they do, we add one to a counter. After it, let’s run this simulation for 1000 times and we will see the probability that this fact is true. The code is presented bellow:</p><pre>import pandas as pd<br>import numpy as np<br>import random</pre><pre>n_tries = 1000<br>yes = 0<br>for _ in range(n_tries):<br>    random_nums = [random.randint(1,365) for i in range(23)]<br>    if len(np.unique(random_nums)) &lt; 23:<br>        yes = yes + 1<br>    <br>print(&quot;The probability of having at least two people making aniversary in the same day is: &quot;, \<br>      yes*100/n_tries, &quot;%&quot;)</pre><p>As result, we got: “The probability of having at least two people making anniversary in the same day is: 49.9 %”. This seems right, but we need to confess that if we run it some times the result will have a big standard deviation, which means that the probability will vary a lot and the confidence of our result is low. To check it, we can add an outer for loop (to simulate how our probability changes) and then plot the histogram of probability values:</p><pre>#number of probabilities sampling<br>n_probabilities = 100</pre><pre>#list to hold probability values<br>probability = []<br>for _ in range(n_probabilities):<br>    n_tries = 1000<br>    count = 0<br>    for _ in range(n_tries):<br>        random_nums = [random.randint(1,365) for i in range(23)]<br>        if len(np.unique(random_nums)) &lt; 23:<br>            count = count + 1<br>        <br>    probability.append(count*100/n_tries)</pre><pre>probability = pd.Series(probability)<br>probability.hist()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/377/1*HxvFJ5x5YFoABTT3N2aeMg.jpeg" /><figcaption>Standard deviation for 1000 of simulations</figcaption></figure><p>So we simulated the event 1000 times and got a mean value around 50 % but with a range of values going from ~48 to 56. If we would like to increase the confidence of our result, we can increase the number of tries (n_tries). In this case, the probability converges better to 0.5 and the standard deviation gets smaller, as we can see in the figure bellow for n_tries = 10000:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*EuHar8LCBEy6HCWZJQcMFg.png" /><figcaption>Histogram for 10000 of samples</figcaption></figure><p>This is the same for the case of flipping a coin. It is obvious that the probability of heads and tails are equal. But if you flip a coin just only a few times, you’ll probably get a different number of tails and heads.</p><p>As much as you increase the number of times that you flip a coin, closer the number o heads and tails becomes. This is called simulating probabilistic events. This simulation can be also called “sampling”. In a more formal term, we generated random numbers to sample the probability distribution of an event with two possibilities: having or not at least two people born in the same day in a room. <strong>This is the simplest Monte Carlo simulation case</strong>.</p><h3>Review and Conclusions</h3><ul><li>Monte Carlo simulation samples a probability distribution in order to know how this distribution is.</li><li>It is useful when we calculating by hand is not a good option and also when the nature of the event is not deterministic, but probabilistic;</li><li>To do so, we generate arbitrary scenarios of our event according to some random number;</li><li>The more we sample the event, the more we know about its probability distribution and more confidence we have in our result, nonetheless, it gets more computationally expensive.</li></ul><p>This type of analysis can be used for many applications that go far beyond from this simple example . In a further article, Monte Carlo simulation will be used and discussed for a system reliability analysis. In the mean time, try to simulate the case of tails and heads and tell me the results in the comments. How many tries do you need to use to obtain a confidence interval of 1 %?</p><h3>See you there !</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8849c47914de" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>