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 compute probabilities using data and commands in Python programming language:
Imagine a simple situation in a port where importation containers could be divided in terms of their ProductCode and from its origin. To be didactic, suppose the container origin could be from two countries, and ProductCode could be Vegetable and Food Product leading to the following table:
import pandas as pd
col = ['Vegetable', 'FoodProduct']
row = ['C1', 'C2']
dat = [[1400, 600], [700, 2300]]
df_imp = pd.DataFrame(data=dat, index=row, columns=col)
df_imp
The following values will be stored in variable df_imp:
The next sequences of commands are useful to obtain the sum of rows:
df_imp.loc[:,'SumC'] = df_imp.sum(axis=1)
df_imp
The following table with values will appear:
The next sequences of commands are useful to obtain the sum of columns for df_imp variable:
df_imp.loc['SumP',:] = df_imp.sum(axis=0)
df_imp
The following table with values will appear:
To compute the simple or marginal probabilities of selecting a container of product from a country (C1 or C2) or of a certain type (Vegetable or FoodProduct) just apply the following commands:
PC1 = df_imp.loc['C1','SumC']/df_imp.loc['SumP','SumC']
PC2 = df_imp.loc['C2','SumC']/df_imp.loc['SumP','SumC']
PV = df_imp.loc['SumP','Vegetable']/df_imp.loc['SumP','SumC']
PF = df_imp.loc['SumP','FoodProduct']/df_imp.loc['SumP','SumC']
print("PC1 = ",PC1)
print("PC2 = ",PC2)
print("PV = ",PV)
print("PF = ",PF)
The following probability values will appear:
PC1 = 0.4
PC2 = 0.6
PV = 0.42
PF = 0.58
The Python code with all the steps is summarized in this Google Colab (click on the link):
https://colab.research.google.com/drive/1CKHXfvPksQHKAoeryYwsAOlaKTz9JIm_?usp=sharing