1. Concepts & Definitions
1.1. Experiment, observation, and sample space
1.2. Sample space: Venn and Tree diagram
1.3. Simple and composite events
1.4. Three definitions of probability
1.5. Law of large numbers and its consequences
1.6. Frequency and empirical probability
2. Problem & Solution
2.4. Frequency of categories from tables
2.5. Simple and marginal probabilities
2.6. Conditional probabilities
The following steps will serve as a guideline to create an honest six-face dice simulator in Python programming language:
Load the notebook with commands developed in step 2.1. (click on the link):
https://colab.research.google.com/drive/1Qy7otOirP6urREMSjRktpHhpz28AOzPi?usp=sharing
To build a six-face dice simulator is necessary to define a list of possible events after throwing a dice ['1','2','3','4','5','6'], a list with the corresponding probabilities of happening each face equally [pf,pf,pf,pf,pf,pf], and the number n of times the experiment will be repeated. All these commands can be summarized as:
import numpy as np
n = 1000
pf = 1/6
dice_seq = np.random.choice(['1','2','3','4','5','6'], p=[pf,pf,pf,pf,pf,pf], size=n)
dice_seq
The following values will be stored in variable coin_seq:
array(['1', '5', '5', '4', '1', '4', '3', '2', '6', '6', '1', '3', '4', '6', '5', '6', '3', '3', '5', '2', '3', '5', '6', '3', '1', '5', '2', '3', '4', '2', '6', '3', '4', '5', '2', '6', '1', '4', '6', '5', '4', '5', '3', '3', '2', '4', '3', '4', '1', '1', '6', '6', '5', '3', '2', '6', '2', '4', '6', '2', '2', '4', '5', '5', '6', '2', '1', '4', '1', '4', '4', '4', '6', '2', '2', '3', '4', '1', '3', '1', '1', '3', '6', '5', '5', '3', '3', '6', '3', '6', '6', '2', '5', '6', '1', '2', '5', '1', '1', '2', '4', '4', '2', '4', '4', '4', '5', '4', '2', '2', '5', '4', '2', '6', '6', '4', '1', '4', '3', '5', '2', '1', '4', '3', '1', '4', '3', '5', '4', '2', '2', '2', '1', '5', '6', '3', '4', '4', '5', '3', '3', '5', '1', '5', '1', '3', '4', '4', '4', '2', '2', '2', '2', '2', '2', '1', '3', '3', '3', '1', '4', '6', '4', '4', '5', '4', '3', '5', '2', '1', '4', '2', '5', '6', '3', '2', '5', '2', '3', '6', '5', '2', '3', '3', '4', '4', '3', '2', '1', '6', '5', '6', '5', '1', '3', '2', '6', '6', '4', '1', '5', '6', '6', '4', '4', '5', '5',
After the previous command, the variable dice_seq contains the results of the n repetitions of the experiment. Now, these results could be counted in terms of the number of occurrences of each event: '1', '2', '3', '4', '5', and '6'. For this task, the command Counter() from the collections package is useful since it returns a dictionary with the name of the event and its frequency. A dictionary is a data structure where each element has two parts: the key which is the name of the element, and the value is the part that stores the value related to the name stored in the key part:
import collections
dice_counter = collections.Counter(dice_seq)
print(dice_counter)
Counter({'2': 189, '6': 180, '5': 172, '4': 166, '3': 156, '1': 137})
The command plt.bar from package matplotlib.pyplot as plt helps to create a related bar plot. The number of times each event occurs is the height of each bar, and the command face = list(dice_counter.keys()) stores in the variable face the frequency of each event. The name of each event is described in the x-axis label using the command freq = list(dice_counter.values()).
import matplotlib.pyplot as plt
face = list(dice_counter.keys())
freq = list(dice_counter.values())
print(freq)
plt.bar(face, freq, color ='maroon', width = 0.4)
plt.xlabel("Face")
plt.ylabel("Frequency")
plt.title("Number of occurrences of each face after throw a dice "+str(n)+" times")
plt.show()
The following values and graph will appear:
The Python code with all the steps is summarized in this Google Colab (click on the link):
https://colab.research.google.com/drive/1mUX_ItyQzRGUC3qqjjCijTFzOW4TCIdL?usp=sharing