<?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 Andile Mhlongo on Medium]]></title>
        <description><![CDATA[Stories by Andile Mhlongo on Medium]]></description>
        <link>https://medium.com/@andilemhlongo?source=rss-a786d4e98b92------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*9QSMWnAW6iuSqr_X</url>
            <title>Stories by Andile Mhlongo on Medium</title>
            <link>https://medium.com/@andilemhlongo?source=rss-a786d4e98b92------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 15:36:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@andilemhlongo/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[How to Simulate Penalty Shootouts on Python using Monte Carlo Simulation: Feature Generation]]></title>
            <link>https://medium.com/@andilemhlongo/how-to-simulate-penalty-shootouts-on-python-using-monte-carlo-simulation-part-1-feature-generation-ac0c05498b98?source=rss-a786d4e98b92------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac0c05498b98</guid>
            <category><![CDATA[monte-carlo]]></category>
            <category><![CDATA[fifa-world-cup-2022-qatar]]></category>
            <category><![CDATA[football]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[uefa-champions-league]]></category>
            <dc:creator><![CDATA[Andile Mhlongo]]></dc:creator>
            <pubDate>Mon, 16 Jan 2023 17:32:03 GMT</pubDate>
            <atom:updated>2024-05-10T08:20:28.872Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*96BDrckOH0D761mvDsJELg.jpeg" /></figure><p>Penalty Shootouts were a huge ordeal during the FIFA World Cup. They were a source of a alot of controversy from social media and news outlets. Even the final match of the tournament was decided on penalties. Lionel Messi, the most decorated player in all of football was the said to have won the 2022 World Cup in Qatar because of penalties awarded to him unceremoniously, but that is subject to speculation now.</p><p>So I thought it would be cool to simulate a penalty shootout using python.</p><blockquote>This approach was inspired by a book called Monte Carlo or Bust by Joseph Buchandal</blockquote><p>The core premise was to look at possible scenerios when the a team might win a penalty shootout</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HPDsbDi1WfKkxmwiTXPfYA.jpeg" /><figcaption>A hand drawn table depicting possible penalty shoot-out scenarios</figcaption></figure><p>From the table above we can see that the fastest way a team can win a shootout is when a team scores all their first three goals without the other team scoring. All the other highlighted scores depict a scenario that might end a shootout. This works well for either team. If none of the scenarios pen out (excuse the pun), then we go into sudden death(which is as bad as it sounds).</p><p>The first priority is to define these scores in python</p><pre>def sim_penalty():<br>    x=[]<br>    y=[]<br>    for i in range(10000):<br>        x_result = random.randint(0,1)<br>        y_result = random.randint(0,1)<br>        x.append(x_result)<br>        y.append(y_result)<br>        x_sum = pd.Series(x).sum()<br>        y_sum = pd.Series(y).sum()<br>        if i&gt;=3 and i&lt;=5:                                                              <br>            if x_sum&gt;3 and y_sum&lt;3:<br>                winner = &#39;X&#39;<br>                break<br>            if y_sum&gt;3 and x_sum&lt;3:<br>                winner = &#39;Y&#39;<br>                break<br>            if y_sum==3 and x_sum==0:<br>                winner = &#39;Y&#39;<br>                break    <br>            if x_sum==3 and y_sum==0:<br>                winner = &#39;X&#39;<br>                break<br>        if i==5:<br>            if x_sum &gt; y_sum:<br>                winner = &#39;X&#39;<br>                break<br>            if x_sum &lt; y_sum:<br>                winner = &#39;Y&#39;<br>                break<br>        if i&gt;5:<br>            if x_result &gt; y_result:<br>                winner = &#39;X&#39;<br>                break<br>            if y_result &gt; x_result:<br>                winner = &#39;Y&#39;<br>                break<br><br><br>    return {&#39;Team A&#39;:x_sum,&#39;Team B&#39;:y_sum,&#39;Total Penalties&#39;:i+1,&#39;Winner&#39;:winner*2}</pre><p>The above <strong>function</strong> simulates a single penalty shootout</p><p>Two <strong>lists</strong> are instantiated for either team to capture each of their penalty shootouts</p><pre>x=[]<br>y=[]</pre><p>We would like to limit the amount of iterations that might occur to 10000 (If a shootout reaches sudden death and no team scores or they both score, then shootout might go on infinitely) ,so we introduce a <strong>for loop</strong> to run the iteration</p><pre>for i in range(10000):</pre><p>With each iteration a function from the random module called <strong>randint </strong>is called upon.</p><p>This generates a pseudo random number of either <strong>0</strong> for <strong>missing</strong> a penalty or <strong>1 </strong>for a player <strong>scoring</strong> a penalty.</p><p>The <strong>result</strong> then is assigned to each respective variable for later processing and</p><pre>x_result = random.randint(0,1)<br>y_result = random.randint(0,1)</pre><p>It is then added to the <strong>list </strong>of previous penalties that were taken using the append function.</p><pre>x.append(x_result)<br>y.append(y_result)</pre><p>We then use <strong>pandas</strong> to create a <strong>series </strong>object so we can get the sum of the list of previous penalties. Pandas aggregation functions are vectorized which makes them more efficient than writing your own function</p><pre>x_sum = pd.Series(x).sum()<br>y_sum = pd.Series(y).sum()</pre><p><strong>if </strong>statements captures a scenarios where a shootout has to end:</p><p><strong>Prematurely</strong>, whereby the other team fails to catch up.</p><pre>if i&gt;=3 and i&lt;=5:                                                              <br>            if x_sum&gt;3 and y_sum&lt;3:<br>                winner = &#39;X&#39;<br>                break<br>            if y_sum&gt;3 and x_sum&lt;3:<br>                winner = &#39;Y&#39;<br>                break<br>            if y_sum==3 and x_sum==0:<br>                winner = &#39;Y&#39;<br>                break    <br>            if x_sum==3 and y_sum==0:<br>                winner = &#39;X&#39;<br>                break</pre><p><strong>After 5 shots at goal, </strong>whereby a penalty shootout reaches the designated end and the team with the bigger score wins</p><pre>if i==5:<br>      if x_sum &gt; y_sum:<br>        winner = &#39;X&#39;<br>        break<br>      if x_sum &lt; y_sum:<br>        winner = &#39;Y&#39;<br>        break</pre><p><strong>Sudden Death, </strong>whereby if Team A scores and Team B doesn’t, then Team A wins</p><pre>if i&gt;5:<br>    if x_result &gt; y_result:<br>      winner = &#39;X&#39;<br>      break<br>    if y_result &gt; x_result:<br>      winner = &#39;Y&#39;<br>      break</pre><p>The above function <strong>returns</strong> a dictionary containing The amount of <strong>goals scored </strong>by each team, The total number of <strong>shots </strong>and the <strong>winner.</strong></p><pre>{&#39;Team A&#39;: 3, &#39;Team B&#39;: 4, &#39;Total Penalties&#39;: 6, &#39;Winner&#39;: &#39;Y&#39;}</pre><p>The results above could serve as a basis for a deeper understanding and appreciation of penalty shootouts and how they affect the beautiful game of football.</p><p>On the next part we will use <strong>Monte Carlo Simulation </strong>to analyse how these results (Even though random) can be used to depict a real life penalty shootouts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/1*BxLXbaPjYjnh_dnjM-gRiw.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac0c05498b98" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>