...

/

Calculating the Parameters

Calculating the Parameters

Learn how we can calculate the parameters of norm and survival and run the QBN.

We'll cover the following...

Calculating the parameters of the norm

Javascript (babel-node)
def calculate_norm_params(passengers):
# the different populations in our data
pop_children = passengers[passengers.IsChild.eq(1)]
pop_adults = passengers[passengers.IsChild.eq(0)]
# combinations of being a child and gender
pop_am = pop_adults[pop_adults.Sex.eq('male')]
pop_af = pop_adults[pop_adults.Sex.eq('female')]
pop_cm = pop_children[pop_children.Sex.eq('male')]
pop_cf = pop_children[pop_children.Sex.eq('female')]
norm_params = {
'p_norm_am': pop_am.Norm.sum() / len(pop_am),
'p_norm_af': pop_af.Norm.sum() / len(pop_af),
'p_norm_cm': pop_cm.Norm.sum() / len(pop_cm),
'p_norm_cf': pop_cf.Norm.sum() / len(pop_cf),
}
return norm_params

The function calculate_norm_params takes the Pandas dataframe of the passengers and returns the norm_params dictionary. From lines 2 to 10, we’ll specify different populations (groups) of passengers. Then, from lines 12 to 17, we’ll calculate the probabilities of a passenger being favored by a norm (Norm), given that the passenger belongs to a group.

In lines 3 and 4, we’ll separate the children from the adults in the data by evaluating whether the value of the column IsChild is 1 (children) or 0 (adults). In lines 7 to 10, we’ll further split these two groups into four based on whether the sex (Sex) is female or male.

Let’s pay some attention to how we calculate the probabilities of a passenger being favored by a norm in lines 13 to 16. We’ll sum the Norm of all passengers of a group and divide it by the number of passengers in the group. The Norm is the hidden variable. Similar to the example of a missing value, we’ll fill this column with a number between 0 and 1 that represents the probability of the respective passenger to be favored by a norm.

For example, if we have ten passengers and five have a value of 00, and five have a value of 11, we’ll get a resulting probability of P(Norm)=(51+50)/10=0.5P(Norm)=(5\cdot 1+5\cdot 0)/10=0.5 ...