In this article, several distributions are implented with scipy.stats.

In [1]:
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

Binomial

In [2]:
n = 10
p = 0.3

rv = stats.binom(n=n, p=p)

fig, ax = plt.subplots()
# x = np.arange(11)
x = np.arange(stats.binom.ppf(0, n, p),
              stats.binom.ppf(1, n, p))
ax.plot(x, rv.pmf(x), color='r', zorder=1)
ax.scatter(x, rv.pmf(x), zorder=2)
ax.set(title="Binomial pmf")
# ax.set_ylim(bottom=0)
ax.axhline(y=0, linestyle='--', color='black')
Out[2]:
<matplotlib.lines.Line2D at 0x9436ef0>

Poisson

In [3]:
mu = 0.9
rv = stats.poisson(mu)

fig, ax = plt.subplots()
# x = np.arange(11)
x = np.arange(stats.poisson.ppf(0.01, mu),
              stats.poisson.ppf(0.99, mu))
ax.plot(x, rv.pmf(x), color='r', zorder=1)
ax.scatter(x, rv.pmf(x), zorder=2)
ax.set(title="Poisson pmf")
# ax.set_ylim(bottom=0)
ax.axhline(y=0, linestyle='--', color='black')
Out[3]:
<matplotlib.lines.Line2D at 0x951da90>

Gaussian

In [4]:
rv = stats.norm()

fig, ax = plt.subplots()
# x = np.arange(11)
x = np.linspace(stats.norm.ppf(0.01),
              stats.norm.ppf(0.99), 100)
ax.plot(x, rv.pdf(x), color='r', zorder=1)
ax.scatter(x, rv.pdf(x), zorder=2)
ax.set(title="Gaussian(0, 1) pmf")
# ax.set_ylim(bottom=0)
ax.axhline(y=0, linestyle='--', color='black')
Out[4]:
<matplotlib.lines.Line2D at 0x9c407f0>

Lognormal

In [5]:
fig, ax = plt.subplots()

for s in np.arange(0.1, 1, 0.1):
    x = np.linspace(stats.lognorm.ppf(0.01, s),
                    stats.lognorm.ppf(0.99, s), 100)
    ax.plot(x, stats.lognorm.pdf(x, s), zorder=1, label='s={}'.format(s))

ax.set(title="Lognormal pdf")
ax.legend()
ax.axhline(y=0, linestyle='--', color='black')
Out[5]:
<matplotlib.lines.Line2D at 0x9c40860>

Student's T

mean of random samples from gaussian random variable with different variances

In [6]:
fig, ax = plt.subplots()

for df in np.arange(2, 5, 1):
    x = np.linspace(stats.t.ppf(0.01, df),
                    stats.t.ppf(0.99, df), 100)
    ax.plot(x, stats.t.pdf(x, df), zorder=1, label='df={}'.format(df))

ax.set(title="Student's T pdf")
ax.legend()
ax.axhline(y=0, linestyle='--', color='black')
Out[6]:
<matplotlib.lines.Line2D at 0xa3214e0>

${\chi}^2$

In [7]:
fig, ax = plt.subplots()

for df in np.arange(1, 10, 1):
    x = np.linspace(stats.chi2.ppf(0.01, df),
                    stats.chi2.ppf(0.99, df), 100)
    ax.plot(x, stats.t.pdf(x, df), zorder=1, label='df={}'.format(df))

ax.set(title=r"${\chi}^2$ pdf", xlim=(0,5))
ax.legend()
ax.axhline(y=0, linestyle='--', color='black')
Out[7]:
<matplotlib.lines.Line2D at 0xa5b2358>

Fisher's F

In [8]:
fig, ax = plt.subplots()

# for dfn, dfd in zip(np.arange(1, 10, 1), np.arange()):
dfn, dfd = 29*2, 18*2
x = np.linspace(stats.f.ppf(0.01, dfn, dfd),
                stats.f.ppf(0.99, dfn, dfd), 100)
ax.plot(x, stats.f.pdf(x, dfn, dfd), zorder=1, label='dfn={},dfd={}'.format(dfn, dfd))

ax.set(title=r"Fisher's F pdf", xlim=(0,5))
ax.legend()
ax.axhline(y=0, linestyle='--', color='black')
Out[8]:
<matplotlib.lines.Line2D at 0xa8c7ac8>


Comments

comments powered by Disqus
share -