.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/lesson5/plot_simulatematch.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_lesson5_plot_simulatematch.py: Simulating a match ================== Before we get started, we are going to simulate a football match using a very simple model. One in which goals occur completely at random during the match. .. GENERATED FROM PYTHON SOURCE LINES 10-16 .. code-block:: default import numpy as np import numpy.random as rnd import time import pylab as plt .. GENERATED FROM PYTHON SOURCE LINES 17-20 Setting parameters ---------------------------- Lets set up the basic scoring rates for football .. GENERATED FROM PYTHON SOURCE LINES 20-30 .. code-block:: default #Length of match match_minutes = 90 #Average goals per match goals_per_match = 2.79 #Probability of a goal per minute prob_per_minute = np.array(goals_per_match/match_minutes) print('The probability of a goal per minute is %5.5f. \n' % prob_per_minute ) .. rst-class:: sphx-glr-script-out .. code-block:: none The probability of a goal per minute is 0.03100. .. GENERATED FROM PYTHON SOURCE LINES 31-36 Simulating a single game ---------------------------- Now we simulate a single game. Goals go in with the same probability every minute. Run the simulation below 10 times. How many times were there 3 goals in a match? .. GENERATED FROM PYTHON SOURCE LINES 36-57 .. code-block:: default #Count of the number of goals goals=0 for minute in range(match_minutes): #Generate a random number between 0 and 1. r=rnd.rand(1,1) #Prints an X when there is a goal and a zero otherwise. if (r < prob_per_minute): #Goal - if the random number is less than the goal probability. print('X', end = ' ') goals=goals+1 time.sleep(1) #Longer pause else: print('o', end = ' ') time.sleep(0.1) #Short pause print('\n') print('Final whistle. \n \nThere were ' + str(goals) + ' goals.') .. rst-class:: sphx-glr-script-out .. code-block:: none o o o o o o o o o o o X o o o o o o o o o o o X o o o o o o o o o o o X o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o Final whistle. There were 3 goals. .. GENERATED FROM PYTHON SOURCE LINES 58-60 It is likely that about 2 or 3 of your simulations ended with 3 goals. Most matches have between 0 and 5 goals. It is likely you saw at most one match with 0 goals. .. GENERATED FROM PYTHON SOURCE LINES 63-65 Simulating goals over a season ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 65-116 .. code-block:: default # We now simulate 380 matches of a football season and look at how well it predicts the # distribution of the number of goals. This is done in the code below: we loop over 380 matches, # store the number of goals for each match in array and then we make a histogram of the number of goals. # def simulateMatch(n, p): # n - number of time units # p - probability per time unit of a goal # display_match == True then display simulation output for match. # Count the number of goals goals = 0 for minute in range(n): # Generate a random number between 0 and 1. r = rnd.rand(1, 1) # Prints an X when there is a goal and a zero otherwise. if (r < p): # Goal - if the random number is less than the goal probability. goals = goals + 1 return goals # Number of matches num_matches = 380 # Loop over all the matches and print the number of goals. goals = np.zeros(num_matches) for i in range(num_matches): goals[i] = simulateMatch(match_minutes, prob_per_minute) #print('In match ' + str(i+1) + ' there were ' + str(int(goals[i])) + ' goals.') # Create a histogram fig, ax = plt.subplots(num=1) histogram_range = np.arange(-0.5, 10.51, 1) histogram_goals = np.histogram(goals, histogram_range) ax.bar(histogram_goals[1][:-1] + 0.5, histogram_goals[0], color='white', edgecolor='black', linestyle='-', alpha=0.5) ax.set_ylim(0, 100) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_xticks(np.arange(0, 11, step=1)) ax.set_yticks(np.arange(0, 101, step=20)) ax.set_xlabel('Number of goals') ax.set_ylabel('Number of matches') plt.show() .. image-sg:: /gallery/lesson5/images/sphx_glr_plot_simulatematch_001.png :alt: plot simulatematch :srcset: /gallery/lesson5/images/sphx_glr_plot_simulatematch_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 117-118 Run the code a few times and look at the resulting histogram. Now we have seen this simulation, we can look at football... .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 11.797 seconds) .. _sphx_glr_download_gallery_lesson5_plot_simulatematch.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_simulatematch.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_simulatematch.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_