Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Section 2.3 — Inventory of discrete distributions

This notebook contains all the code examples from Section 2.3 Inventory of discrete distributions of the No Bullshit Guide to Statistics.

Notebook setup


[notice] A new release of pip is available: 26.0.1 -> 26.2.1
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.
<Figure size 640x480 with 0 Axes>

Discrete uniform distribution

Note the +1 we added to the second argument.

2.5
1.25

The limits of the sample space of the random variable rvR can be obtained by calling its .support() method.

Probability mass function

<Figure size 600x200 with 1 Axes>
(1, 4)

Bernoulli distribution

(0, 1)
(0.4, 0.24)
array([0, 1, 1, 0, 0, 0, 0, 1, 1, 1])
<Figure size 600x200 with 1 Axes>

Mathematical interlude: counting combinations

Factorials

TODO explain + math formula

24.0

The factorial of 0 is defined as 1: 0!=10!=1.

1.0

The factorial function grows grows very quickly:

[120.0, 720.0, 5040.0, 40320.0, 362880.0, 3628800.0, 39916800.0, 479001600.0, 6227020800.0]

Combinations

TODO explain + math formula

10.0
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]

Exercises

924.0
2598960.0

Binomial distribution

We’ll use the name rvX because rvB was already used for the Bernoulli random variable above.

(0, 20)
(2.8000000000000003, 2.4080000000000004)
<Figure size 600x200 with 1 Axes>

Poisson distribution

0.11259903214902009
0.3328196787507191
<Figure size 600x200 with 1 Axes>

Geometric distribution

(1, inf)
(5.0, 20.0)
<Figure size 600x200 with 1 Axes>

Negative binomial distribution

(5, inf)
(10.0, 10.0)
<Figure size 600x200 with 1 Axes>

Computer models for discrete distributions

TODO table models

TODO table methods

Building computer models for random variables

Note the +1 we added to the second argument.

2.5
2.5
1.25
1.25
1.118033988749895

The limits of the sample space of the random variable rvR can be obtained by calling its .support() method.

(1, 4)

Probability calculations

0.75
[0.25, 0.25, 0.25]
0.75
0.75

Plotting distributions

Probability mass function

To create a stem-plot of the probability mass function fRf_R, we can use the following three-step procedure:

  1. Create a range of inputs rs for the plot.

  2. Compute the value of fR=f_R = rvR for each of the inputs and store the results as list of values fRs.

  3. Plot the values fRs by calling the function plt.stem(rs, fRs).

<Figure size 600x200 with 1 Axes>

Note, we used the np.where statement to replace zero entries in the list fRs to the special Not a Number (np.nan) values, which excludes them from the plot. This is arguably a cleaner representation of the PMF plot: the function is only defined for the sample space R={1,2,3,4}\mathcal{R} = \{1,2,3,4\}.

An alternative ,,,

<Figure size 600x200 with 1 Axes>
Cumulative distribution function
<Figure size 600x200 with 1 Axes>
<Figure size 600x200 with 1 Axes>

Generating random observations

Let’s generate 10 random observations from random variable rvR:

array([1, 1, 4, 2, 2, 3, 1, 4, 2, 4])

Explanations

Binomial coefficient formula explained

[1, 6, 15, 20, 15, 6, 1]

We can draw Pascal’s triangle by by repeating the above calculation for several values of n.

                             1                              
                           1   1                            
                         1   2   1                          
                       1   3   3   1                        
                     1   4   6   4   1                      
                  1   5   10   10   5   1                   
                1   6   15   20   15   6   1                
             1   7   21   35   35   21   7   1              
           1   8   28   56   70   56   28   8   1           
       1   9   36   84   126   126   84   36   9   1        

Geometric sums

1.4285714285714286
1.4285714285714286

Discussion

Relations between distributions

Exercises

Exercise XX

0.2503540762867841
0.6867142435340193
28.0