Bayes Theorem for Conditional Probability in C/C++

Conditional probability denoted by P(A|B) is the probability of occurrence of an event ‘A’ given that event ‘B’ has already occurred.

Formula for conditional probability −

P(A|B) = P( A⋂B ) / P(B)

Bayes’s Theorem

It is the formula that shows the relation between probabilities of occurrences of mutually dependent events i.e. it given the relation between their conditional probabilities.

Given an event A and another event B, according to bayes’ theorem,

P(A/B) = {P(B/A) * P(A)} / P(B)

Lets derive the formula for Bayes’ theorem,

For this we will use the formula for conditional probability,

P(A|B) = P( A?B ) / P(B) —— 1
P(B|A) = P( B?A ) / P(A) —— 2

We know that A⋂B and B⋂A are the same, hence we can substitute the value of B⋂A with A⋂B equation 2.

P(B/A) = P(A⋂B) / P(A)
P(B/A) * P(A) = P(A⋂B) —- 3

Now, using this value for A?B in equation 1, we will get the bayes’ theorem formula.

P(A/B) = {P(B/A) * P(A)} / P(B)

Some derivations for the Bayes’ Theorem,

Product Rule

depicted in equation 3, it says that the probability of both the events to occur in the same trials is equal to the product of conditional probability of the event and the probability of occurrence of evidence event.

P(A?B) = P(A/B) * P(B)

From this rule we can derive two important formulas −

If A⊆B i.e. A is the subset of B which means all the elements of set A are in set B, then

P(A⋂B) = P(A), then P(A/B) = P(A) / P(B)

If B⊆A i.e. B is the subset of A which means all the elements of set B are in set A, then

P(A⋂B) = P(B), then P(A/B) = 1

Bayes’ Theorem form more than three events −

If we have more that three mutually dependent events, their conditional probability will have the following relation,

P(X1/Y) = (P(X1)*P(Y/X1) / [P(X1 * P(Y/X1)) + P(X2 * P(Y/X2)) + P(X3 * P(Y/X3)) + …]