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 coin simulator in Python programming language:
Create a new notebook using the Google Colab environment as described in section 1.2: Designing the solution.
To build a coin simulator is necessary to define a list of possible events after throwing a coin ['Head','Tail'], a list with the corresponding probabilities of happening for each event [ph, pt], and the number n of times the experiment will be repeated. All these commands can be summarized as:
import numpy as np
n = 1000
ph = 0.5
pt = 0.5
coin_seq = np.random.choice(['Head','Tail'], p=[ph, pt], size=n)
coin_seq
The following values will be stored in variable coin_seq:
array(['Head', 'Tail', 'Head', 'Head', 'Head', 'Tail', 'Tail', 'Head', 'Head', 'Head', 'Head', 'Head', 'Head', 'Tail', 'Head', 'Tail', 'Head', 'Head', 'Head', 'Tail', 'Head', 'Tail', 'Head', 'Tail', 'Tail', 'Tail', 'Tail', 'Tail', 'Head', 'Head', 'Head', 'Head', 'Tail', 'Tail', 'Tail', 'Tail', 'Head', 'Tail', 'Head', 'Head', 'Tail', 'Head', 'Head', 'Head', 'Tail', 'Head', 'Tail', 'Tail', 'Tail', 'Head', 'Tail', 'Head', 'Head', 'Head', 'Tail', 'Tail', 'Head', 'Tail', 'Head', 'Tail', 'Head', 'Head', 'Head', 'Head', 'Tail', 'Tail', 'Head', 'Tail', 'Tail', 'Tail', 'Tail', 'Tail', 'Head', 'Tail', 'Head', 'Tail', 'Head', 'Head', 'Tail', 'Head', 'Tail', 'Tail', 'Tail', 'Head', 'Head', 'Tail', 'Tail', 'Tail', 'Tail', 'Head', 'Head', 'Tail', 'Head', 'Tail', 'Tail', 'Head', 'Head', 'Tail', 'Tail', 'Tail', 'Head', 'Tail', 'Tail', 'Tail', 'Head', 'Head', 'Head', 'Tail', 'Head', 'Tail', 'Tail', 'Head', 'Tail', 'Head', 'Head', 'Tail', 'Head', 'Head', 'Tail', 'Tail', 'Tail', 'Tail', 'Head', 'Tail', 'Tail', 'Head', 'Tail', 'Tail', 'Head', 'Tail', 'Head', 'Tail', 'Head', 'Head', 'Head', 'Head',
After the previous command, the variable coin_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: 'Head' and 'Tail'. 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
coin_counter = collections.Counter(coin_seq)
print(coin_counter)
Counter({'Head': 503, 'Tail': 497})
To extract the keys from the variable coin_seq the command keys() could be used:
coin_counter.keys()
dict_keys(['Head', 'Tail'])
To extract the keys from the variable coin_seq the command values() could be used:
coin_counter.values()
dict_values([503, 497])
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(coin_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(coin_counter.values()).
import matplotlib.pyplot as plt
face = list(coin_counter.keys())
freq = list(coin_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 coin "+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/1Qy7otOirP6urREMSjRktpHhpz28AOzPi?usp=sharing