# Probability for Discrete Random Variable

## Binomial Distribution

### Concept

The binomial distribution is used to get the probability of the number of successful events.

* Binomial probability: the probability of success (p) or failure (1-p)
* A **binomial random variable** is the number of successes **x** in **n** repeated trials.

The required information are the success probability _**p**_ and number of trial _**n**_

### Example 1:

[**example from medium@AerinKim**](https://towardsdatascience.com/poisson-distribution-intuition-and-derivation-1059aeab90d)

Using the binomial PMF, what is the probability that 20 ppl will clap next week?

We need the success probability _**p**_ and number of trial _**n**_

From the past statistics,

* number of trial n= 1134 visits/ week
* success # = 17 claps per week
  * rate or expected value of x \*\*\*\* is 17 claps/week.
* success p= 17 claps/1134visits /week = 1.5% for 1 week

Then, what is the probability of x=20 claps in next week? Using binomial PMF, Binomial P(X=x) is 0.0692

![](https://lh5.googleusercontent.com/oKqmhDfkjSqfCUMK_TBD_XWeM2khgr3x5d9SbtwkTLEpLxo2FIq0i4GS8qBFpg-sYHwJiPUnkAcFtM38C0c9y7716HvJ3Ijg9fHaeq-OdtkRYaCDiAm1Ttg3S4VsKlmdOnA4rkHn)

## Poisson Distribution

### Definition:

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-f034e1e69186aa3d1a3bf16a7cf06bed9e33637f%2Fimage.png?alt=media)

Predicts the probability of a given number of events occuring in a fixed interval of time with the avg. number of events in that interval is lambda (rate parameter)

Read this article for the full text:

{% embed url="https://towardsdatascience.com/poisson-distribution-intuition-and-derivation-1059aeab90d" %}

### Intuition

What is Possison for? What can Possion do which Binomial can't?

#### Binomial Shortcoming

Binomial random variable is "Binary" 0 or 1 and it cannot have multiple events in the unit of time.

From the previous example, 17ppl/week means 17/(7\*24)=**0.1 ppl per hour.**

Then, in one week, there can be _multiple_ events(S, F) for different hours.

This multiple event issue can be solved by dividing the time unit into more smaller units, from week to hour, to have one event at a time.

* For each minute, 0.1/60 \[ppl/min] is the rate.

Then, it allows multiple events in one hour for the time unit is now in minutes.

We can continue to make the time unit more smaller, from hour to min to sec and so on.

#### Concept:

_We can make the Binomial random variable handle multiple events by dividing a unit time into smaller units._ or making _n_--> large number

If we make the time unit to be infinitesimal, we no longer have to worry about more than one event within the same unit time.

If the expected rate is a fixed value, (i.e. _n\*p_ = constant) , then when we increase n--> inf, the probability p--> 0

### Math

_As n_-->∞ for k is given, and the expected rate (\$$\lambda\$$) is a fixed value, (i.e. _n\*p_ = constant)

![](https://lh6.googleusercontent.com/1vxpIPZ_4eW77G2v3vEgi1oLkeZnC6apjxgbJRwfma_Q036VE2naM3MtjwdqvV8hiC-4RA15dV_-WmzC2GuHbzO1gEIobX-hK4JWvXUmwtdMn3_0B_vNlygKg0ah9AVRMH-2D0av)

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-6c7f135041fc3fcc13158f45d5ce83d8a1f5d1c7%2Fimage.png?alt=media)

### Example 1:

From the previous example, the lamda was 17 ppl/week

Unlike the Binomial, Possion distribution does _not_ require to use the value of _**n**_ and _**p**_\
\_\*\*\*\*\_Poisson is usually used for rare events (n is a large number), but not always.

As lambda becomes bigger, the graph looks more like a normal distribution.\
It assumed the lambda is a constant value but in real application, it may not be.

Also, it assumed the events are independent, but it may not be in real application.

Poisson distribution is discrete.

* For continuous distribution, use Exponential distribution

### ML example:

(Under construction)

## Exponential Distribution

### Definition

![Probability density funtion (pdf) of exponential distribution](https://wikimedia.org/api/rest_v1/media/math/render/svg/026c6516959b9899e341cd54890e0f2a442d9ec3)

Exponential distribution is the probability distribution of the time _between_ the events in a Poisson process. λ \* e^(−λt).

> It is continuous distribution, unlike Possion that is discrete.

### Intuition

It predicts the amount of waiting time until the next event occurs.

* Example: the time until the OS fails again.

What does **X\~Exp(0.25)** means? 0.25 events?

* 0.25 is not time duration. it is an event rate
*   X\~Exp(lamda), lamda = Possion parameter rate

    Example: lamda=17claps/week, is a rate of the unit time of 1 week.

In terms of the unit of time of the event, time = 1/lamda

* this is the decay paramter or rate.
* 17claps/week --> (1/17) week per clap.

rate=0.25 means 0.25 events in the time unit(e.g. hours) 4 time unit(e.g. hours) until the event occurs

#### Understanding λ \* e^(−λt)

We want to find the time between the events in Poisson process.\
The waiting period until the next event occurs means there is NO single event has happened

* Possion (X=0)

![medium @AerinKim](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-2b8144847de1f4dd5ab37c3791bef41ee5b4e27e%2Fimage.png?alt=media)

If you want to model the probability distribution of “nothing happens during the _**time duration**_\*\* **\_**&#x74;\*\*\_,” _not just during **one unit time**_

```python
P(Nothing happens during t time units)
= P(X=0 in the first time unit) 
  * P(X=0 in the second time unit) 
  * … * P (X=0 in the t-th time unit) 
= e^−λ * e^−λ * … * e^−λ = e^(-λt)
```

The Poisson distribution assumes that events occur independent of one another.

Therefore, we can calculate the probability of zero success during **t** units of time by multiplying P(**X**=0 in a single unit of time) **t** times.

```python
P(T > t) = P(X=0 in t time units) = e^−λt
* T : the random variable of our interest!
      the random variable for the waiting time until the first event
* X : the # of events in the future which follows the Poisson dist.
* P(T > t) : The probability that the waiting time until the first event is greater than t time units
* P(X = 0 in t time units) : The probability of zero successes in t time units
```

A PDF is the derivative of the CDF.\
Since we already have the CDF, **1 - P(T > t),** of exponential, we can get its PDF by differentiating it.

![medium @AerinKim](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-809c879cd7c9dc3f3d7f738b039ab4c7bfd925b0%2Fimage.png?alt=media)

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-0f9f4fe54431b43075856cfdb6fe8271404df7d2%2Fimage.png?alt=media)

### ML example:

(Under construction)

## Sum of Exponential Random Variables <a href="#id-59b2" id="id-59b2"></a>

**R**ead this article for simple and clear explanations

{% embed url="https://towardsdatascience.com/sum-of-exponential-random-variables-b023b61f0c0f" %}

**X1** and **X2** are **independent** exponential random variables with the rate **λ**.

**X1\~EXP(λ)**\
**X2\~EXP(λ)**

Let **Y=X1+X2**.

Question : What is the PDF of **Y**?\
Where do we use **the distribution of Y**?

In the [Poisson Process](https://towardsdatascience.com/poisson-distribution-intuition-and-derivation-1059aeab90d) with rate **λ**, **X1+X2** would represent the time at which the 2nd event happens (addition of time x1 and x2).

An Erlang distribution is then used to answer the question:

> “How long do I have to wait before I see **n** success events occurs?”

The answer is **a sum of independent exponentially distributed random variables**, which is an **Erlang(n, λ)** distribution. The Erlang distribution is a special case of the Gamma distribution. The difference between Erlang and Gamma is that in a Gamma distribution, **n** can be a non-integer

* Erlang (2, λ) distribution

![medium @aerinKim](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-50515e6dda025a2c571dd86b3ff50f8d75000973%2Fimage.png?alt=media)

### Example

\[Queuing Theory] You went to Chipotle and joined a line with two people ahead of you. One is being served and the other is waiting. Their service times **S1** and **S2** are independent, exponential random variables with **mean of 2 minutes**. (Thus the mean service rate is **.5/minute**.

Your conditional time in the queue is **T = S1 + S2,** given the system state **N = 2**.\
**T** is Erlang distributed.

**What is the probability that you wait more than 5 minutes in the queue?**

Let’s plug **λ = 0.5** into the CDF that we have already derived.

![medium @aerinKim](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-d2c761a43b26dad6ce0612d37fa2c42eca1088cb%2Fimage.png?alt=media)

A less-than-30% chance that I’ll wait for more than 5 minutes at Chipotle sounds good to me
