See Scipy rv_discrete for reference manual.
from scipy import stats
import numpy as np
xk = np.arange(7)
pk = (0.1, 0.2, 0.3, 0.1, 0.1, 0.0, 0.2)
custm = stats.rv_discrete(name='custm', values=(xk, pk))
%matplotlib inline
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)
ax.plot(xk, custm.pmf(xk), 'ro', ms=12, mec='r')
ax.vlines(xk, 0, custm.pmf(xk), colors='r', lw=4)
plt.show()
custm.stats()
custm.median()
custm.mean()
custm.var()
custm.std()
custm.interval(0.05)
custm.expect(lambda x: x)
custm.expect(lambda x: x * 2)
custm.expect(lambda x: x * 2 + 3)
custm.expect(lambda x: x / 2)
custm.pmf(3)
custm.pmf(8)
The method rvs() samples one value according to the distribution:
print([custm.rvs() for _ in range(20)])
Alternatively, we can generate many samples at once:
custm.rvs(size=20)
If we sample many times, and then count the frequency of each outcome, we get back the same distribution as used in the constructor of the discrete distribution:
from collections import Counter
samples = custm.rvs(size=1000)
estimated = Counter(samples)
estimated
print("k \t Est \t Prob")
print("--\t-----\t-----")
for k in estimated:
print("%d \t %.1f \t %2.1f" % (k, estimated[k]/1000, custm.pmf(k)))