Hello all, I’m back after a longer hiatus than I intended. After Easter, some things happened that took a good deal of my time and I was unable to carry on with my posts.

But I’m back today to my series on HackerRank’s “10 Days of Statistics”. The first purpose of these posts is to explain the solutions to these exercises, although I have also an undercurrent of some other objectives: to find topics for more interesting, in-depth posts; to give me some practice in Python; to get me into the mode of thinking about data science; and to create a writing routine.

Days 2 and 3 provide for some of those, but not for Python practice: unlike the other challenges of this set, the questions on these days are strictly pen and paper, on the topic of Probabilities. These are simple problems, but their success rate is, as a group, the lowest of all days. Probabilities tend to have a bad reputation among students, of being hard to understand. Because of this, I think it’s useful to spend some time to go to the basics and try to provide some lights on how to attack these problems.

**Task**

In a single toss of **2 **fair (evenly-weighted) six-sided dice, find the probability that their sum will be *at most* **9.**

First, to get our feet on the ground, we must understand what probabilities are about. They are about *Events. *An event is a binary variable: either something happens or it doesn’t. And we don’t know for sure whether it will or won’t. There is uncertainty, and that is crucial to probabilities.

A probability measures how likely it is that a particular event will happen.

An event should be something concrete, observable, that you can definitely say whether it happens or not. It usually happens as the result of an experiment, that will end up in one of several different states.

Examples of events are

- it will rain tomorrow
- Real Madrid will win the Champions League this year
- the cat will be alive when I open this box
- I will get 5 tails in 5 consecutive tosses of a coin

This post is only about probabilities of uniform discrete distributions. It just means we can count the number of states the experiment can reach, and that each state is equally likely.

The basic notion of discrete probability that should serve as the guiding light in any problem is this:

The Probability of an event is the number of final states of the experiment in which the event happens divided by the total number of possible final states.

Or, for short: “number of favourable cases over total number of cases”. Remember that.

For this task, we identify the following:

- Experiment: throw two fair six-sided dice
- Random variable: sum of the sides shown by each die
- Event: the value of the random variable is between 2 and 9

Let’s apply the principle above and start by counting the total number of cases. Each die has six sides, and the side showing on one does not influence the side showing on the other. So, there are 6 x 6 = 36 cases.

How many of these are favourable cases?

The problem does not say these dice are out of the ordinary, so we assume they are numbered as standard, from 1 to 6. Then, we can divide our favourable cases by each of the possible states of the first die. For example, if the first die shows a 1, all states of the second die (1 to 6) lead to a sum at most equal to 9.

- 6 (max = 7)
- 6 (max = 8)
- 6 (max = 9)
- 5
- 4
- 3

Total: 30

The result is then `30/36 = 5/6.`

**Task**

In a single toss of **2 **fair (evenly-weighted) six-sided dice, find the probability that the values rolled by each die will be *different* and the two dice have a sum of **6.**

There is a new complication in this problem. There are two conditions: the dice will show different numbers, and their sum is 6.

Following the previous challenge, we could compute the probability of the event E = “the sum is 6”.

We can easily see we have 5 favourable cases: for each value on the first die, except for 6, there is a single value on the second one that makes the sum equal to 6. But we have to consider now the first condition, that creates a sub-event of E: of those 6 cases, we cannot accept those where both dice are equal. There can only be one such case, when both dice show 3. Therefore, we have only 4 favourable cases.

Applying the basic principle, the probability is `4/36 = 1/9.`

**Task**

There are **3 **urns labeled **X, Y** and **Z.**

- 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.

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?

This is a more interesting problem than the first 2, and belongs to a very popular class of problems: one of counting favourable permutations / combinations / arrangements of some objects.

- Experiment: Take one ball from each of the urns
- Random Variable: number of red balls in the experiment’s final state
- Event: the variable’s value is exactly 2.

Let’s apply the basic principle again: we want to count the number of favourable cases and the total number of cases. I always prefer to start by the latter, because in counting all the cases I am forced to see the problem as a whole: to understand what its universe is, and where its variables are. I may even see what techniques to apply to count the favourable cases.

Total cases: there are 7 balls in one of the urns, 8 in another and 9 in another. We can draw each of these for each urn, with equal likelihood. So, we have `7 x 8 x 9 = 504`

cases.

The interesting part is to count favourable cases. We know a success case is one where we have exactly 2 Red balls. Since we have 3 urns and 2 colours inside each urn, the number of different sequences we can get is 2^3 = 8:

0 Reds: BBB,

1 Red: RBB, BRB, BBR

2 Reds: RRB, RBR, BRR

3 Reds: RRR

So we only need to count the cases that correspond to 3 different sequences.

RRB:

We can have any of the *red* balls in urn 1 (4),

and any of the *red* balls in urn 2 (5),

and any of the *black *balls in urn 3 (4).

Since these balls are all indistinguishable, we can have 4 x 5 x 4 variants = 80.

RBR:

Following the same strategy, the number of favourable cases for this sequence is 4 x 4 x 4 = 64.

BRR:

Finally, for the third sequence we have this number of favourable cases: 3 x 5 x 4 = 60.

Total number of favourable cases: `80 + 64 + 60 = 204.`

Final probability = `204 / 504 = 17 / 42.`