# Random Variables and Distributions

A random variable (also called stochastic variable) is a variable that can take a set of possible different values, each with its own probability. For example, the experiment could be picking a person at random, and a random variable would be the person’s height.

A random variable can be discrete or continuous. Discrete random variables can only take a finite or infinitely countable set of values (e.g., integers are infinitely countable). Continuous random variables can take values that vary continuously and might not be countable (for example the real numbers in a given interval).

Associated with a random variable is a probability distribution, which can be used to calculate the probability of any given random variable value, or the probability that the value with fall within some interval or range.

## Binomial Distribution

The Binomial Distribution is a special type of probability distribution, used to find the probability of getting r successes in n independent experiments (notice that binomial distribution experiments can output only two values: yes or no, or success and non-success).

Example: Suppose you want to know the probability of getting exactly r heads when tossing a coin n times. You can find this using the binomial distribution.

Let’s call the probability of getting a head P (we know that P = 1/2, but let’s just call it P for now, to keep it general).

The probability of getting exactly r heads is: P^r (i.e., you multiply the individual probabilities together since these are independent events). If you want to get exactly r heads, you obviously need to get n-r tails, and that probability is (1-P)^(n-r). So we need to multiply these two probabilities together.

Finally, you can move the position of the heads around. That is, if you are tossing the coin 3 times and want 2 heads, the following sequences would satisfy it:

HHT
THH
HTH

So you would need to multiply the probabilities above by 3. In the general case you would need to multiply it by (n choose r). So the formula for finding binomial distributions, called probability mass function, is the following:

P = Probability of success event happening (in our case, getting a head)
r = Number of successes we want
n = Number of trials

f(r) = (n choose r) (P^r) (1-P)^(n-r)

## Geometric Distribution

Suppose you want to roll a die until a 6 shows up. Our experiment is therefore rolling a die many times, and our random variable is the number of rolls we’ll need until a 6 shows up.

Now what’s the probability that the value of the random variable will be 3? That’s, what’s the probability that after 3 rolls we’ll get a 6?

This problem can be solved using the geometric distribution, and here’s is its mass function:

P = probability of the target event (in our case, a 6 showing up)
Q = 1 – P (probability that limit event won’t happen)
r = number of trials until target event happens

f(r) = P * Q^(r-1)

In our example P = 1/6 and Q = 5/6, so:

f(r) = 1/6 * 5/6^2 = 1/6 * 25/36 = 25/216

So the probability of getting a 6 on the third roll of a die is 25/216.