How to build a PMF sample?

Is there any function or library that would help me to plot the sample probabilistic mass function in the same way as for plotting the sample probability density function?

For example, using pandas, building a PDF is as easy as calling:

sample.plot(kind="density")

      

If there is no easy way, how can I calculate the PMF so that I can plot using matplotlib?

+3


source to share


2 answers


If ts

is a batch, you can get the PMF of the sample:

>>> pmf = ts.value_counts().sort_index() / len(ts)

      

and write it down:

>>> pmf.plot(kind='bar')

      




The numpy solution only can be accomplished with np.unique

:

>>> xs = np.random.randint(0, 10, 100)
>>> xs
array([5, 2, 2, 1, 2, 8, 6, 7, 5, 3, 2, 6, 4, 9, 7, 6, 4, 7, 6, 8, 7, 0, 6,
       2, 9, 8, 7, 7, 2, 6, 2, 8, 0, 2, 5, 1, 3, 6, 7, 7, 2, 2, 0, 3, 8, 7,
       4, 0, 5, 7, 5, 4, 4, 9, 5, 1, 6, 6, 0, 9, 4, 2, 0, 8, 7, 5, 1, 1, 2,
       8, 3, 8, 9, 0, 0, 6, 8, 7, 2, 6, 7, 9, 7, 8, 8, 3, 3, 7, 8, 2, 2, 4,
       4, 5, 3, 4, 1, 5, 5, 1])

>>> val, cnt = np.unique(xs, return_counts=True)
>>> pmf = cnt / len(xs)

>>> # values along with probability mass function
>>> np.column_stack((val, pmf))
array([[ 0.  ,  0.08],
       [ 1.  ,  0.07],
       [ 2.  ,  0.15],
       [ 3.  ,  0.07],
       [ 4.  ,  0.09],
       [ 5.  ,  0.1 ],
       [ 6.  ,  0.11],
       [ 7.  ,  0.15],
       [ 8.  ,  0.12],
       [ 9.  ,  0.06]])

      

+8


source


You can use np.histogram

to calculate the PMF using density=true

assuming the unit width cells are used (otherwise you will get the value of the bin probability density function, which most likely does not suit you).



>>> xs = np.array(
          [5, 2, 2, 1, 2, 8, 6, 7, 5, 3, 2, 6, 4, 9, 7, 6, 4, 7, 6, 8, 7, 0, 6,
           2, 9, 8, 7, 7, 2, 6, 2, 8, 0, 2, 5, 1, 3, 6, 7, 7, 2, 2, 0, 3, 8, 7,
           4, 0, 5, 7, 5, 4, 4, 9, 5, 1, 6, 6, 0, 9, 4, 2, 0, 8, 7, 5, 1, 1, 2,
           8, 3, 8, 9, 0, 0, 6, 8, 7, 2, 6, 7, 9, 7, 8, 8, 3, 3, 7, 8, 2, 2, 4,
           4, 5, 3, 4, 1, 5, 5, 1])

>>> pmf, bins = np.histogram(xs, bins=range(0,11), density=True)
>>> np.column_stack((bins[:-1], pmf))
array([[ 0.  ,  0.08],
       [ 1.  ,  0.07],
       [ 2.  ,  0.15],
       [ 3.  ,  0.07],
       [ 4.  ,  0.09],
       [ 5.  ,  0.1 ],
       [ 6.  ,  0.11],
       [ 7.  ,  0.15],
       [ 8.  ,  0.12],
       [ 9.  ,  0.06]])

      

0


source







All Articles