...

/

Calculating the Probabilities Related to the Ticket Class

Calculating the Probabilities Related to the Ticket Class

Learn how we can calculate probabilities related to the ticket class.

Press + to interact
pop_first = train[train.Pclass.eq(1)]
surv_first = round(len(pop_first[pop_first.Survived.eq(1)])/len(pop_first), 2)
p_first = round(len(pop_first)/len(train), 2)
pop_second = train[train.Pclass.eq(2)]
surv_second = round(len(pop_second[pop_second.Survived.eq(1)])/len(pop_second), 2)
p_second = round(len(pop_second)/len(train), 2)
pop_third = train[train.Pclass.eq(3)]
surv_third = round(len(pop_third[pop_third.Survived.eq(1)])/len(pop_third), 2)
p_third = round(len(pop_third)/len(train), 2)
print("First class: {} of the passengers, survived: {}".format(p_first,surv_first))
print("Second class: {} of the passengers, survived: {}".format(p_second,surv_second))
print("Third class: {} of the passengers, survived: {}".format(p_third,surv_third))

Let’s turn to the marginal probability of having a ticket of a certain class (Pclass) and the respective chances to survive.

The calculation of the probabilities is straightforward. The marginal probability of owning a ticket is given by the number of tickets of the respective class, divided by the total number of passengers in lines 3, 7, and 11. The conditional probability of surviving given a certain ticket class is the quotient of the number of survivors and the total number of passengers with a ticket of that class, in lines 2, 6, and 10.

Representing the ticket‐class

Press + to interact
# positions of the qubits
QPOS_FIRST = 3
QPOS_SECOND = 4
QPOS_THIRD = 5
def apply_class(qc):
# set the marginal probability of Pclass=1st
qc.ry(prob_to_angle(p_first), QPOS_FIRST)
qc.x(QPOS_FIRST)
# set the marginal probability of Pclass=2nd
qc.cry(prob_to_angle(p_second/(1-p_first)), QPOS_FIRST, QPOS_SECOND)
# set the marginal probability of Pclass=3rd
qc.x(QPOS_SECOND)
ccry(qc, prob_to_angle(p_third/(1-p_first-p_second)), QPOS_FIRST, QPOS_SECOND, QPOS_THIRD)
qc.x(QPOS_SECOND)
qc.x(QPOS_FIRST)

So far, we’ve only dealt with boolean variables. These are variables that only have two possible values. The Pclass is different because there are three different ticket classes, that is, the 1st, 2nd, and 3rd.

Technically, we could represent three values by using two qubits, but we’re going to use three. We’ll represent the probability of having a ticket of a certain class by a single one qubit being in state 1|1\rangle ...