In [1]:
import scipy.stats as sps
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import numpy as np
import seaborn as sns
%matplotlib inline
Rigorous definitions of discrete probability laws and discrete random variables are provided in part 00. By reading this part or from your own education, you should know by now what are the probability mass function and the cumulative distribution function for a discrete probability law.
Let $N \in \mathbb{N}$ and $\mathcal{A} = \{a_1, \dots, a_N\}$ a set of atoms, the uniform discrete law is written as:
\begin{equation} p(\{a_i\}) = \frac{1}{N} \end{equation}For example, with $N=6$, let's take the example of the set of integers $\{1,2,3,4,5,6\}$:
In [2]:
N = 6
xk = np.arange(1,N+1)
fig, ax = plt.subplots(1, 1)
ax.plot(xk, sps.randint.pmf(xk, xk[0], 1+xk[-1]), 'ro', ms=12, mec='r')
ax.vlines(xk, 0, sps.randint.pmf(xk, xk[0], 1+xk[-1]), colors='r', lw=4)
plt.show()
The cumulative distribution function of a uniform discrete distribution is defined $\forall k \in [a,b]$ as :
$$F(k;a,b) = \frac{\lfloor k \rfloor-a+1}{b-a+1}$$In our example it looks like this:
In [3]:
xk = np.arange(0,N+1)
fig, ax = plt.subplots(1, 1)
for i in np.arange(0,N+1):
y=sps.randint.cdf(i, xk[1], 1+xk[-1])
l = mlines.Line2D([i,i+1], [y,y], color='r')
ax.add_line(l)
ax.plot(xk[1:], sps.randint.cdf(xk[1:], xk[1], 1+xk[-1]), 'ro', ms=12, mec='r')
ax.plot(xk[:-1]+1, sps.randint.cdf(xk[:-1], xk[1], 1+xk[-1]), 'ro', ms=13, mec='r', mfc='r') # Ugly hack to get white circles with red edges
ax.plot(xk[:-1]+1, sps.randint.cdf(xk[:-1], xk[1], 1+xk[-1]), 'ro', ms=12, mec='r', mfc='w') #
ax.set()
plt.show()
In [ ]: