Lab

11.4.2. Lab#

  • Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.

  • Sign in from the account menu before you start working on the assignment.

  • Write and run your code directly in the coding cells.

  • Multiple submissions are allowed. The highest score is kept.

### Question 1: Delivery Percentiles
# An online store wants to promise a delivery time that most orders meet.
#   1. Compute the 75th and 90th percentiles of days with
#      np.percentile(..., method='nearest'), as in this chapter.
# These describe this sample, not a guaranteed future delivery promise.
# Print exactly:
# 75th percentile: 6
# 90th percentile: 8
import numpy as np
import pandas as pd

days = pd.Series([2, 3, 3, 4, 4, 5, 5, 6, 8, 12])
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

days = pd.Series([2, 3, 3, 4, 4, 5, 5, 6, 8, 12])

print("75th percentile:", np.percentile(days, 75, method='nearest'))
print("90th percentile:", np.percentile(days, 90, method='nearest'))
# Nine of these ten observed orders arrived within 8 days.
# A small historical sample does not guarantee future delivery performance.
75th percentile: 6
90th percentile: 8
### Question 2: An Estimate and One Resample
# HR has a random sample of 12 salaries (in $1,000s) and wants to estimate the
# median salary of all employees.
#   1. Compute the sample median: the estimate of the population median.
#   2. Draw len(salaries) values WITH replacement using salaries.sample(
#      len(salaries), replace=True, random_state=3). Compute its median.
# Print exactly:
# Sample median: 71.0
# Resample median: 84.5
import numpy as np
import pandas as pd

salaries = pd.Series([62, 58, 75, 81, 66, 93, 70, 59, 88, 64, 120, 72])
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

salaries = pd.Series([62, 58, 75, 81, 66, 93, 70, 59, 88, 64, 120, 72])
resample = salaries.sample(len(salaries), replace=True, random_state=3)

print("Sample median:", salaries.median())
print("Resample median:", resample.median())
# Some salaries appear more than once in the resample and some
# not at all, which is what drawing with replacement does.
Sample median: 71.0
Resample median: 84.5
### Question 3: A 95% Bootstrap Confidence Interval
# Generate a bootstrap distribution for the population median salary.
#   1. Repeat 5,000 times: draw len(salaries) values with rng.choice(...,
#      size=len(salaries), replace=True), and append np.median(resample)
#      to bootstrap_medians. Initialize the list before your for loop.
#   2. Use its 2.5th and 97.5th percentiles (method='nearest') as endpoints.
# The seed is set once; do not reset it in the loop. The original sample
# still contains only 12 employees, so interpret this interval cautiously.
# Print exactly:
# 95% CI: 63.0 to 84.5
import numpy as np
import pandas as pd

salaries = pd.Series([62, 58, 75, 81, 66, 93, 70, 59, 88, 64, 120, 72])
rng = np.random.default_rng(42)

### Your code starts here.


### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

salaries = pd.Series([62, 58, 75, 81, 66, 93, 70, 59, 88, 64, 120, 72])
rng = np.random.default_rng(42)
bootstrap_medians = []
for i in range(5000):
    resample = rng.choice(salaries, size=len(salaries), replace=True)
    bootstrap_medians.append(np.median(resample))

left = np.percentile(bootstrap_medians, 2.5, method='nearest')
right = np.percentile(bootstrap_medians, 97.5, method='nearest')
print("95% CI:", left, "to", right)
95% CI: 63.0 to 84.5
### Question 4: 95% vs. 80% Confidence
# Using the same bootstrap medians:
#   1. Compute the width of the 95% interval (2.5th to 97.5th percentile).
#   2. Compute the width of the 80% interval (10th to 90th percentile).
#   Use method='nearest' for all percentiles.
# Print exactly:
# 95% width: 21.5
# 80% width: 13.0
import numpy as np
import pandas as pd

salaries = pd.Series([62, 58, 75, 81, 66, 93, 70, 59, 88, 64, 120, 72])
rng = np.random.default_rng(42)
bootstrap_medians = []
for i in range(5000):
    resample = rng.choice(salaries, size=len(salaries), replace=True)
    bootstrap_medians.append(np.median(resample))

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

salaries = pd.Series([62, 58, 75, 81, 66, 93, 70, 59, 88, 64, 120, 72])
rng = np.random.default_rng(42)
bootstrap_medians = []
for i in range(5000):
    resample = rng.choice(salaries, size=len(salaries), replace=True)
    bootstrap_medians.append(np.median(resample))

def width(low, high):
    return np.percentile(bootstrap_medians, high, method='nearest') - np.percentile(bootstrap_medians, low, method='nearest')

print("95% width:", width(2.5, 97.5))
print("80% width:", width(10, 90))
# Here the 80% interval is narrower; ties can produce equal widths.
# More resamples alone would not add employees to the original sample.
95% width: 21.5
80% width: 13.0
### Question 5: Is It a Majority?
# In a random sample of 200 customers, 116 (0.58) would recommend the store.
# The 40 supplied bootstrap proportions are a short arithmetic exercise.
# Use thousands of resamples and check the sampling design before acting.
#   1. Compute the 95% confidence interval (method='nearest').
#   2. Check whether the whole interval is above 0.5, which supports the claim
#      that a majority of all customers would recommend the store.
# Print exactly:
# 95% CI: 0.52 to 0.625
# Whole interval above 0.5: True
import numpy as np
import pandas as pd

bootstrap_props = pd.Series([0.585, 0.56, 0.585, 0.57, 0.595, 0.62, 0.56, 0.555, 0.53, 0.545,
                             0.56, 0.625, 0.525, 0.59, 0.54, 0.585, 0.575, 0.565, 0.61, 0.585,
                             0.59, 0.52, 0.575, 0.56, 0.615, 0.585, 0.52, 0.54, 0.58, 0.59,
                             0.645, 0.58, 0.585, 0.575, 0.61, 0.565, 0.6, 0.58, 0.515, 0.565])
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

bootstrap_props = pd.Series([0.585, 0.56, 0.585, 0.57, 0.595, 0.62, 0.56, 0.555, 0.53, 0.545,
                             0.56, 0.625, 0.525, 0.59, 0.54, 0.585, 0.575, 0.565, 0.61, 0.585,
                             0.59, 0.52, 0.575, 0.56, 0.615, 0.585, 0.52, 0.54, 0.58, 0.59,
                             0.645, 0.58, 0.585, 0.575, 0.61, 0.565, 0.6, 0.58, 0.515, 0.565])

left = np.percentile(bootstrap_props, 2.5, method='nearest')
right = np.percentile(bootstrap_props, 97.5, method='nearest')

print("95% CI:", left, "to", right)
print("Whole interval above 0.5:", left > 0.5)
# This coarse practice interval supports, but does not prove, a majority.
95% CI: 0.52 to 0.625
Whole interval above 0.5: True