%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats

"""
Explanation: Probability distributions and statistical functions with scipy
TODO
* ...
Official documentation:
* https://docs.scipy.org/doc/scipy/reference/stats.html
End of explanation
"""


n = 20
p = 0.25

dist = scipy.stats.binom(n, p)

"""
Explanation: Discrete distributions
Binomial distribution
See https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binom.html#scipy.stats.binom
End of explanation
"""


xmin = 0
xmax = n

x = np.arange(xmin, xmax, 1)
y = dist.pmf(x)

plt.plot(x, y, 'k.');

"""
Explanation: Probability mass function
End of explanation
"""


xmin = 0
xmax = n + 1

x = np.arange(xmin, xmax, 1)
xstart = x[:-1]
xend = x[1:]

y = dist.cdf(xstart)

plt.hlines(y, xstart, xend, colors='k')
plt.plot(xstart, y, 'k.');

"""
Explanation: Cumulative distribution function
End of explanation
"""


k = 3

dist = scipy.stats.poisson(k)

"""
Explanation: Poisson distribution
See https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.poisson.html#scipy.stats.poisson
End of explanation
"""


xmin = 0
xmax = 14 + 1  # TODO

x = np.arange(xmin, xmax, 1)
y = dist.pmf(x)

plt.plot(x, y, 'k.');

"""
Explanation: Probability mass function
End of explanation
"""


xmin = 0
xmax = 14 + 2 # TODO

x = np.arange(xmin, xmax, 1)
xstart = x[:-1]
xend = x[1:]

y = dist.cdf(xstart)

plt.hlines(y, xstart, xend, colors='k')
plt.plot(xstart, y, 'k.');

"""
Explanation: Cumulative distribution function
End of explanation
"""


mu = 0.
sigma = 1.

dist = scipy.stats.norm(mu, sigma)

"""
Explanation: Continuous distributions
Normal distribution
See https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html#scipy.stats.norm
End of explanation
"""


xmin = -5.
xmax = 5.

x = np.arange(xmin, xmax, 0.01)
y = dist.pdf(x)

plt.plot(x, y, 'k-');

"""
Explanation: Probability density function
End of explanation
"""


xmin = -5.
xmax = 5.

x = np.arange(xmin, xmax, 0.01)
y = dist.cdf(x)

plt.plot(x, y, 'k-');

"""
Explanation: Cumulative distribution function
End of explanation
"""


