Get startedGet started for free

Visualizing bootstrap samples

In this exercise, you will generate bootstrap samples from the set of annual rainfall data measured at the Sheffield Weather Station in the UK from 1883 to 2015. The data are stored in the Numpy array rainfall in units of millimeters (mm). By graphically displaying the bootstrap samples with an ECDF, you can get a feel for how bootstrap sampling allows probabilistic descriptions of data.

This exercise is part of the course

Statistical Thinking in Python (Part 2)

View Course

Exercise instructions

  • Write a for loop to acquire 50 bootstrap samples of the rainfall data and plot their ECDF.
    • Use np.random.choice() to generate a bootstrap sample. Be sure that the length of the resampled array is len(data).
    • Use the function ecdf() that you wrote in the prequel to this course to generate the x and y values for the ECDF of the bootstrap sample, and plot them. Use the color='gray' (to make gray dots) and alpha=0.1 (to make them semi-transparent, since we are overlaying so many).
  • Use ecdf() to generate x and y values for the ECDF of the original rainfall data and plot them as you normally plot ECDFs.
  • Label your axes and specify a 2% margin.
  • Show your plot.

Hands-on interactive exercise

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

for _ in ____:
    # Generate bootstrap sample: bs_sample
    bs_sample = ____(____, size=____)

    # Compute and plot ECDF from bootstrap sample
    x, y = ____
    _ = plt.plot(____, ____, ____='.', ____='none',
                 ____='gray', ____=0.1)

# Compute and plot ECDF from original data
x, y = ____
_ = plt.plot(____, ____, '.')

# Make margins and label axes
plt.margins(0.02)
_ = plt.xlabel('yearly rainfall (mm)')
_ = plt.xlabel('ECDF')

# Show the plot
plt.show()
Edit and Run Code