Homework

11.4.3. Homework#

  • 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.

1. How Resampling Works

A bootstrap resample is drawn without replacement from the original sample and has the same size as the original sample.

2. Interval for a Mean

A 95% confidence interval for the mean order value is $42 to $48. This means that 95% of individual orders are between $42 and $48.

3. What “95% Confidence” Means

An analyst draws a random sample of 400 orders, bootstraps it, and reports a 95% interval for the mean order value. The analyst tells the manager: “If we repeated this whole process with fresh random samples many times, about 95% of the intervals would contain the true mean order value.”

4. Lower Confidence

A pricing team builds 5,000 bootstrap means of weekly sales and reports both a 95% and an 80% interval from them. A colleague says the 80% interval cannot be wider than the 95% interval.

5. Estimating a Maximum

A bootstrap confidence interval works well for estimating the highest salary paid by a large company from a random sample of its employees.

### 6. The Middle Half of Orders
# Describe individual order values (in dollars), not uncertainty in a mean.
# Use the chapter's nearest convention; this odd-size sample has the same
# ordinary median and nearest 50th percentile.
#   1. Compute the 25th, 50th, and 75th percentiles with method='nearest'.
# Print exactly:
# 25th percentile: 45
# 50th percentile: 49
# 75th percentile: 52
import numpy as np
import pandas as pd

orders = pd.Series([38, 52, 45, 61, 47, 55, 40, 49, 58, 44, 51, 46, 63, 42, 50])

### Your code starts here.



### Your code ends here.
### 7. One Bootstrap Resample
# Draw one bootstrap resample of orders with .sample(): same size,
# replacement, and random_state=7. Then summarize that resample.
#   1. Compute the mean of the resample, rounded to 2 decimals.
#   2. Count the distinct values in the resample with nunique().
# Print exactly:
# Resample mean: 50.67
# Distinct values: 9 of 15
import numpy as np
import pandas as pd

orders = pd.Series([38, 52, 45, 61, 47, 55, 40, 49, 58, 44, 51, 46, 63, 42, 50])

### Your code starts here.



### Your code ends here.
### 8. Interval for the Mean Order Value
# Generate 40 bootstrap means from orders: draw len(orders) values WITH
# replacement using rng.choice, and collect each mean in bootstrap_means.
# Set up the list before the loop. Keep the seed outside the loop.
# Then compute a central 95% interval with method='nearest'.
# Round only the displayed endpoints to 2 decimals.
# Forty repetitions make this a coarse workflow exercise; use thousands
# and check endpoint stability for a real management decision.
# Print exactly:
# 95% CI: 45.67 to 52.6
import numpy as np
import pandas as pd

orders = pd.Series([38, 52, 45, 61, 47, 55, 40, 49, 58, 44, 51, 46, 63, 42, 50])
rng = np.random.default_rng(434)

### Your code starts here.


### Your code ends here.
### 9. Sample Size and Interval Width
# Two surveys asked customers whether they use the mobile app (1 = yes).
# Survey A asked 100 customers (40 yes); survey B asked 400 (160 yes).
# Bootstrap each survey with the same generator, survey A first and then survey B:
# for each, draw 1000 resamples of the survey's own size WITH replacement using
# rng.choice, and keep the mean of each resample.
#   1. Find each survey's central 95% interval with method='nearest'.
#   2. Compute each width, then how many times wider interval A is than interval B,
#      using the unrounded widths.
# Print the widths rounded to 3 decimals and the ratio rounded to 1 decimal.
# Print exactly:
# Width A: 0.18
# Width B: 0.09
# Ratio: 2.0
import numpy as np
import pandas as pd

survey_a = np.array([1] * 40 + [0] * 60)       # n = 100
survey_b = np.array([1] * 160 + [0] * 240)     # n = 400
rng = np.random.default_rng(461)

### Your code starts here.



### Your code ends here.
### 10. Checking a Supplier's Claim
# A supplier claims a 5% defect rate. In a random sample of 300 parts, 18 (0.06)
# were defective. The 40 rounded bootstrap proportions below are supplied
# for arithmetic practice, not a complete supplier-approval analysis.
#   1. Compute the 95% confidence interval (method='nearest').
#   2. Check whether the claimed rate of 0.05 is inside the interval.
# Print exactly:
# 95% CI: 0.033 to 0.083
# Claim inside interval: True
import numpy as np
import pandas as pd

claimed = 0.05
bootstrap_props = pd.Series([0.043, 0.07, 0.033, 0.05, 0.093, 0.057, 0.063, 0.07, 0.067, 0.06,
                             0.033, 0.067, 0.043, 0.05, 0.047, 0.083, 0.04, 0.067, 0.057, 0.083,
                             0.057, 0.077, 0.083, 0.05, 0.057, 0.033, 0.06, 0.04, 0.06, 0.057,
                             0.067, 0.07, 0.063, 0.05, 0.067, 0.073, 0.05, 0.057, 0.053, 0.073])

### Your code starts here.



### Your code ends here.