Get startedGet started for free

Sampling out of the Binomial distribution

Compute the probability mass function for the number of defaults we would expect for 100 loans as in the last section, but instead of simulating all of the Bernoulli trials, perform the sampling using rng.binomial(). This is identical to the calculation you did in the last set of exercises using your custom-written perform_bernoulli_trials() function, but far more computationally efficient. Given this extra efficiency, we will take 10,000 samples instead of 1000. After taking the samples, plot the CDF as last time. This CDF that you are plotting is that of the Binomial distribution.

Note: For this exercise and all going forward, the random number generator is instantiated and seeded for you (with rng = np.random.default_rng(42)) to save you typing that each time.

This exercise is part of the course

Statistical Thinking in Python (Part 1)

View Course

Exercise instructions

  • Draw samples out of the Binomial distribution using rng.binomial(). You should use parameters n = 100 and p = 0.05, and set the size keyword argument to 10000.
  • Compute the CDF using your previously-written ecdf() function.
  • Plot the CDF with axis labels. The x-axis here is the number of defaults out of 100 loans, while the y-axis is the CDF.
  • Show the plot.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Take 10,000 samples out of the binomial distribution: n_defaults


# Compute CDF: x, y


# Plot the CDF with axis labels




# Show the plot

Edit and Run Code