How many defaults might we expect?
Let's say a bank made 100 mortgage loans. It is possible that anywhere between 0 and 100 of the loans will be defaulted upon. You would like to know the probability of getting a given number of defaults, given that the probability of a default is p = 0.05
. To investigate this, you will do a simulation. You will perform 100 Bernoulli trials using the perform_bernoulli_trials()
function you wrote in the previous exercise and record how many defaults we get. Here, a success is a default. (Remember that the word "success" just means that the Bernoulli trial evaluates to True
, i.e., did the loan recipient default?) You will do this for another 100 Bernoulli trials. And again and again until we have tried it 1000 times. Then, you will plot a histogram describing the probability of the number of defaults.
This exercise is part of the course
Statistical Thinking in Python (Part 1)
Exercise instructions
- Seed the random number generator to 42.
- Initialize
n_defaults
, an empty array, usingnp.empty()
. It should contain 1000 entries, since we are doing 1000 simulations. - Write a
for
loop with1000
iterations to compute the number of defaults per 100 loans using theperform_bernoulli_trials()
function. It accepts two arguments: the number of trialsn
- in this case 100 - and the probability of successp
- in this case the probability of a default, which is0.05
. On each iteration of the loop store the result in an entry ofn_defaults
. - Plot a histogram of
n_defaults
. Include thedensity=True
keyword argument so that the height of the bars of the histogram indicate the probability. - Show your plot.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Instantiate and seed random number generator
# Initialize the number of defaults: n_defaults
# Compute the number of defaults
for i in ____:
n_defaults[i] = ____
# Plot the histogram with default number of bins; label your axes
_ = plt.hist(____, ____)
_ = plt.xlabel('number of defaults out of 100 loans')
_ = plt.ylabel('probability')
# Show the plot