Day 5: Poisson Distribution I

https://www.hackerrank.com/challenges/s10-poisson-distribution-1

Objective

In this challenge, we learn about Poisson distributions. Check out the Tutorial tab for learning materials!

Task

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

Input Format

The first line contains 's mean. The second line contains the value we want the probability for:

2.5
5

If you do not wish to read this information from stdin, you can hard-code it into your program.

Output Format

Print a single line denoting the answer, rounded to a scale of decimal places (i.e., format).


In [19]:
import math
from matplotlib import pylab as plt
%matplotlib inline

In [20]:
def posion_dist(m, k):
    return (m**k * math.e**(-m)) / math.factorial(k)

Example

Acme Realty company sells an average of 2 homes per day.

What is the probability that exactly 3 homes will be sold tomorrow?


In [34]:
posion_dist(2, 3)


Out[34]:
0.1804470443154836

In [30]:
N = range(1, 20)
M = 7
plt.plot(N, [posion_dist(M, i) for i in N])


Out[30]:
[<matplotlib.lines.Line2D at 0x10a4db358>]