> For the complete documentation index, see [llms.txt](https://stephanosterburg.gitbook.io/scrapbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stephanosterburg.gitbook.io/scrapbook/math/hackerrank/basic-probability.md).

# Basic Probability

### Event, Sample Space, and Probability

In probability theory, an experiment is any procedure that can be infinitely repeated and has a well-defined set of possible outcomes, known as the *sample space*, $$S$$. We define an *event* to be a set of outcomes of an experiment (also known as a subset of $$S$$) to which a probability (numerical value) is assigned.&#x20;

The probability of the occurrence of an event, $$A$$, is:

$$\huge P(A) =\frac{\text{Number of favorable outcomes}}{\text{Total number of outcomes}}$$

Here are the first two fundamental rules of probability:&#x20;

1. Any probability, $$P(A)$$, is a number between $$0$$ and $$1$$ (i.e., $$0\leq P(A) \leq1$$).
2. The probability of the sample space, $$S$$, is $$1$$ (i.e., $$P(S) = 1$$).&#x20;

So how do we bridge the gap between the value of $$P(A)$$ and the sample space? Quite simply, since we know that $$P(A)$$ is the probability that event $$A$$ will occur, then we define $$P(A')$$ (also written as $$P(A^c)$$) to be the probability that event  will *not occur* (the *complement* of $$P(A)$$). If our sample space is composed of the probabilities of 's occurrence and non-occurrence, we can then say $$P(A) + P(A')=1$$, or the sum of all possible outcomes of $$A$$ in the sample space is equal to $$1$$. This is the third fundamental rule of probability: $$\newline P(A^c)=1 - P(A)$$.

**Question 1** \
*Find the probability of getting an odd number when rolling a* $$6$$*-sided fair die.*&#x20;

Given the above question, we can extract the following:

* Experiment: rolling a $$6$$-sided die.
* Sample space ($$S$$): $$S={1, 2, 3, 4, 5, 6}$$.
* Event ($$A$$): that the number rolled is *odd* (i.e., $$A={1, 3, 5}$$).&#x20;

If we refer back to the basic formula for the probability of the occurrence of an event, we can say:\
\
$$\large P(A) =\frac{\text{Number of favorable outcomes}}{\text{Total number of outcomes}} = \frac{\vert{A}\vert}{\vert{S}\vert} =\frac{3}{6}=\frac{1}{2}$$

### Compound Events, Mutually Exclusive Events, and Collectively Exhaustive Events

Let's consider $$2$$ events: $$A$$ and $$B$$. A *compound event* is a combination of $$2$$ or more simple events. If $$A$$ and $$B$$ are simple events, then $$A\cup B$$ denotes the occurrence of either $$A$$ or $$B$$. Similarly, $$A\cap B$$ denotes the occurrence of $$A$$ and $$B$$ together.&#x20;

$$A$$ and $$B$$ are said to be *mutually exclusive* or *disjoint* if they have no events in common (i.e., $$A \cap B = \emptyset$$ and $$P(A \cap B) = 0$$). The probability of *any* of $$2$$ or more events occurring is the *union* ($$\cup$$) of events. Because disjoint probabilities have no common events, the probability of the union of disjoint events is the sum of the events' individual probabilities. $$A$$ and $$B$$ are said to be *collectively exhaustive* if their union covers all events in the sample space (i.e., $$A \cup B = S$$ and $$P(A \cup B) = 1$$). This brings us to our next fundamental rule of probability: if $$2$$ events, $$A$$ and $$B$$, are disjoint, then the probability of either event is the sum of the probabilities of the $$2$$ events (i.e., $$P(A \ or B) = P(A) + P(B)$$).

If the outcome of the first event ($$A$$) has no impact on the second event ($$B$$), then they are considered to be *independent* (e.g., tossing a fair coin). This brings us to the next fundamental rule of probability: the multiplication rule. It states that if two events, $$A$$ and $$B$$, are independent, then the probability of both events is the product of the probabilities for each event (i.e., $$P(A \ and\ B) = P(A) \* P(B)$$). The chance of *all* events occurring in a sequence of events is called the *intersection* ($$\cap$$) of those events.&#x20;

**Question 2** \
*Find the probability of getting* $$1$$*head and* $$1$$ *tail when* $$2$$ *fair coins are tossed.*

Given the above question, we can extract the following:

* Experiment: tossing $$2$$ coins.
* Sample space ($$S$$): The possible outcomes for the toss of $$1$$ coin are $${H, T}$$, where $$H = heads$$ and $$T = tails$$. As our experiment tosses $$2$$ coins, we have to consider all possible toss outcomes by finding the Cartesian Product of the possible outcomes for each coin: $$\newline S = {{ H, T} \*{H, T} = {(H,H),(H,T),(T,H),(T,T)}$$.
* Event ($$A \cap B$$): that the outcome of $$1$$ toss will be $$H$$, and the outcome of the other toss will be  $$T$$(i.e., $$A={(H, T), (T, H)}$$.

Connecting this information back to our basic formula for $$P(A)$$, we can say:

$$\large P(A) =\frac{\text{Number of favorable outcomes}}{\text{Total number of outcomes}} = \frac{\vert{A}\vert}{\vert{S}\vert} =\frac{2}{4}=\frac{1}{2}$$

**Question 3** \
*Let* $$A$$ *and* $$B$$ *be two events such that* $$P(A)=\frac{2}{5}$$ *and* $$P(B)=\frac{4}{5}$$*. If the probability of the occurrence of either* $$A$$ *or* $$B$$ *is* $$\frac{3}{5}$$*, find the probability of the occurrence of both* $$A$$ *and* $$B$$ *together (i.e.,* $$A \cap B$$*).*

We can use our fundamental rules of probability to solve this problem:

$$\vert A \cup B\vert = \vert A \vert + \vert B \vert - \vert A \cap B\vert \newline \Rightarrow P(A \cup B) = P(A)+P(B)-P(A \cap B)\newline \Rightarrow P(A \cap B) = P(A)+P(B)-P(A \cup B)\newline \Rightarrow P(A \cup B) = \frac{2}{5}+\frac{4}{5} -\frac{3}{5}=\frac{3}{5}$$

### **Task**&#x20;

There are  urns labeled $$X$$, $$Y$$, and $$Z$$.&#x20;

* Urn $$X$$ contains $$4$$ red balls and $$3$$ black balls.
* Urn $$Y$$ contains $$5$$ red balls and $$4$$ black balls.
* Urn $$Z$$ contains $$4$$ red balls and $$4$$ black balls.&#x20;

One ball is drawn from each of the $$3$$ urns. What is the probability that, of the $$3$$ balls drawn, $$2$$ are red and $$1$$ is black?

#### Solution

Urn X has a 4/7 probability of giving a red ball\
Urn Y has a 5/9 probability of giving a red ball\
Urn Z has a 1/2 probability of giving a red ball

Urn X has a 3/7 probability of giving a black ball\
Urn Y has a 4/9 probability of giving a black ball\
Urn Z has a 1/2 probability of giving a black ball

**P(2 red, 1 black)** \
\= P(Red Red Black) + P(Red Black Red) + P(Black Red Red)\
\= (4/7)(5/9)(1/2) + (4/7)(4/9)(1/2) + (3/7)(5/9)(1/2)\
\= 20/126 + 16/126 + 15/126 \
\= 51/126 \
\= 17/42
