Solution to: [Day 5: Poisson Distribution I](https://www.hackerrank.com/challenges/s10-poisson-distribution-1/problem)

<h1 id="tocheading">Table of Contents</h1>
<div id="toc"></div>

- Table of Contents
- Notes
    - Poisson Experiment
    - Poisson Distribution
- Examples
    - Example 1
    - Example 2
    - Special Example
- Math Solution
- Solution
    - Imports
    - Constants
    - Input
    - Poisson Distribution
    - Main


```javascript
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')
```


    <IPython.core.display.Javascript object>


# Notes

## Poisson Experiment
A Poisson experiment is a statistical experiment that has the following properties:
- binary outcome of experiment: Success (s) or failure (f)
- average number of successes in specified region is known
- Probability that success occurs is proportional to size of region
- Probability that success occurs in extremely small region is virtually 0

## Poisson Distribution
A Poisson random variable is the number of successes that result from a Poisson experiment. The probability distribution of a Poisson random variable is called a Poisson distribution:

\begin{equation}
\large
P(k, \lambda) = 
\frac
{\lambda ^{k} * e ^{-\lambda}}
{k!}
\end{equation}


Where:
- P(k, λ) is the Poisson probability to get exactly k successes when average success is λ.
- λ = average number of successes in region
- k = actual number of successes in region
- e = 2.71828


# Examples


## Example 1
Acme Realty company sells an average of 2 homes per day. What is the probability that exactly 3 homes will be sold tomorrow?

- $\lambda$ = 2
- k = 3

\begin{equation}
\large
P(3, 2) = 
\frac
{2^{3} * e^{-2}}
{3!}
\end{equation}

\begin{equation}
\large
= 0.180
\end{equation}

## Example 2
Suppose the average number of lions seen by tourists on a one-day safari is 5. What is the probability that tourists will see fewer than 4 lions on the next one-day safari?

This requires a summation of Poisson distribution Mass Probability Function, from 1 to 3.

\begin{equation}
\large
= \sum_{k=1}^{3}\frac
{5^{k} * e^{-5}}
{k!}
\end{equation}


## Special Example
Consider some Poisson random variable, X. Let **E[X]** be the expectation of X. Find the value of E[X**2].

Let **Var(X)** be the variance of X. Recall that if a random variable has a Poisson distribution, then:

\begin{equation}
\large
E[X] = \lambda
\end{equation}

\begin{equation}
\large
Var(X) = \lambda
\end{equation}

The following is the property of expectation and variation for any random variable, X:

\begin{equation}
\large
Var(x) = E[X^{2}] - E[X]^{2}
\end{equation}

\begin{equation}
\large
E[X^{2}] = Var(X) + E[X]^{2}
\end{equation}

Thus,

\begin{equation}
\large
E[X^{2}] = \lambda + \lambda^{2}
\end{equation}

# Math Solution

A random variable, X, follows Poisson distribution with mean of 2.5. Find the probability with which the random variable X is equal to 5.

- labmda = 2.5
- k = 5

\begin{equation}
\large
P(5, 2.5) = \frac
{2.5^{5} * e ^{-2.5}}
{5!}
\end{equation}

\begin{equation}
\large
0.067
\end{equation}

# Solution

## Imports


```python
from typing import Tuple
```

## Constants


```python
E = 2.71828
```

## Input


```python
def get_input() -> Tuple[float, int]:
	"""Returns input for Day 5: Poisson Distribution I.

	Returns:
		Tuple[float, int]: X's mean and desired random variable value, respectively
	"""
	var_mean = float(input())
	desired_value = int(input())
	return (var_mean, desired_value)
```

## Poisson Distribution


```python
def factorial(num: int) -> int:
	"""Returns factorial of num

	Args:
		num (int): Number to factorial

	Returns:
		int: Factorial result
	"""
	output = 1
	for i in range(num, 0, -1):
		output *= i
	return output
```


```python
def calc_poisson_prob_mass(desired_value: int, val_mean: float) -> float:
	"""Returns mass probability for desired value in Poisson Distribution with val_mean. 

	Args:
		desired_value (int): Desired value of random variable, X
		val_mean (float): mean value of X

	Returns:
		float: Returns probability of desired value in Poisson Dist.
	"""
	return val_mean ** desired_value * E ** -val_mean / factorial(desired_value)
```


```python
def format_scale(num: float) -> float:
	"""Returns number formatted to scale.

	Args:
		num (float): number to

	Returns:
		float: formatted number
	"""
	return f"{num:.3f}"
```

## Main


```python
def main():
	var_mean, desired_value = get_input()

	prob = calc_poisson_prob_mass(desired_value, var_mean)
	print(format_scale(prob))
```


```python
if __name__ == "__main__":
	main()
```

    2.5
    5
    0.067


ef2eb9cd355fea4941c3fb184b4303c74628ab64
ipynb
Jupyter Notebook
statistics/10_days/16_day5poissondistributioni.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
statistics/10_days/16_day5poissondistributioni.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
statistics/10_days/16_day5poissondistributioni.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
2021-09-22T11:06:58.000Z
2022-01-25T09:29:24.000Z
Qwen/Qwen-72B
1. YES
2. YES