Scipy Discrete Distributions

See Scipy rv_discrete for reference manual.

In [1]:
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))
In [2]:
%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()
In [3]:
custm.stats()
Out[3]:
(array(2.7), array(3.8099999999999987))
In [4]:
custm.median()
Out[4]:
2.0
In [5]:
custm.mean()
Out[5]:
2.7000000000000002
In [6]:
custm.var()
Out[6]:
3.8099999999999987
In [7]:
custm.std()
Out[7]:
1.9519221295943132
In [8]:
custm.interval(0.05)
Out[8]:
(2.0, 2.0)

Expected Value E[f(x)]

In [9]:
custm.expect(lambda x: x)
Out[9]:
2.7000000000000002
In [10]:
custm.expect(lambda x: x * 2)
Out[10]:
5.4000000000000004
In [11]:
custm.expect(lambda x: x * 2 + 3)
Out[11]:
8.4000000000000004
In [12]:
custm.expect(lambda x: x / 2)
Out[12]:
1.3500000000000001

Probability Function

In [13]:
custm.pmf(3)
Out[13]:
0.10000000000000001
In [14]:
custm.pmf(8)
Out[14]:
0.0

Sample

The method rvs() samples one value according to the distribution:

In [15]:
print([custm.rvs() for _ in range(20)])
[6, 2, 4, 2, 1, 1, 1, 6, 2, 4, 4, 1, 0, 6, 1, 3, 2, 4, 0, 3]

Alternatively, we can generate many samples at once:

In [16]:
custm.rvs(size=20)
Out[16]:
array([6, 2, 6, 2, 1, 6, 6, 1, 2, 3, 0, 1, 2, 3, 3, 6, 2, 2, 2, 3])

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:

In [17]:
from collections import Counter
samples = custm.rvs(size=1000)
estimated = Counter(samples)
estimated
Out[17]:
Counter({0: 111, 1: 181, 2: 287, 3: 84, 4: 98, 6: 239})
In [18]:
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)))
k 	 Est 	 Prob
--	-----	-----
0 	 0.1 	 0.1
1 	 0.2 	 0.2
2 	 0.3 	 0.3
3 	 0.1 	 0.1
4 	 0.1 	 0.1
6 	 0.2 	 0.2