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.

Hide code cell source

### Solution
# False
# A bootstrap resample is drawn WITH replacement. Without replacement, a resample of the same size would just be the original sample in a different order.

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.

Hide code cell source

### Solution
# False
# The interval estimates the average order value. Individual orders vary much more than their average does.

3. What “95% Confidence” Means

For a reliable approximate procedure under suitable sampling conditions, if we repeated the whole process many times (draw a new random sample, bootstrap it, build a 95% interval), about 95% of those intervals would contain the true parameter.

Hide code cell source

### Solution
# True
# The confidence level describes the process, not one particular interval.

4. Lower Confidence

From the same bootstrap distribution, the 80% confidence interval is no wider than the 95% confidence interval.

Hide code cell source

### Solution
# True
# The 80% interval runs from the 10th to the 90th percentile, which is inside the 2.5th to 97.5th percentile range.
# The widths can be equal when percentile endpoints tie.

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.

Hide code cell source

### Solution
# False
# The bootstrap works poorly for extremes. A resample can never contain a value larger than the sample's maximum, and the sample probably missed the highest salary.
### 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
# Median: 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.

Hide code cell source

### Solution
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])

print("25th percentile:", np.percentile(orders, 25, method='nearest'))
print("Median:", np.percentile(orders, 50, method='nearest'))
print("75th percentile:", np.percentile(orders, 75, method='nearest'))
25th percentile: 45
Median: 49
75th percentile: 52
### 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.

Hide code cell source

### Solution
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])
resample = orders.sample(len(orders), replace=True, random_state=7)

print("Resample mean:", round(resample.mean(), 2))
print("Distinct values:", resample.nunique(), "of", len(resample))
# Drawing with replacement repeats some orders and leaves others out.
Resample mean: 50.67
Distinct values: 9 of 15
### 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.

Hide code cell source

### Solution
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)
bootstrap_means = []
for i in range(40):
    resample = rng.choice(orders, size=len(orders), replace=True)
    bootstrap_means.append(np.mean(resample))

left = np.percentile(bootstrap_means, 2.5, method='nearest')
right = np.percentile(bootstrap_means, 97.5, method='nearest')
print("95% CI:", round(left, 2), "to", round(right, 2))
95% CI: 45.67 to 52.6
### 9. Sample Size and Interval Width
# Two surveys estimated the share of customers who use the mobile app.
# Survey A asked 100 customers; survey B asked 400.
#   1. Compute the width of each 95% interval, rounded to 3 decimals.
#   2. Compute how many times wider interval A is than interval B, 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

interval_a = [0.31, 0.49]     # n = 100
interval_b = [0.355, 0.445]   # n = 400

### Your code starts here.



### Your code ends here.

Hide code cell source

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

interval_a = [0.31, 0.49]     # n = 100
interval_b = [0.355, 0.445]   # n = 400

width_a = interval_a[1] - interval_a[0]
width_b = interval_b[1] - interval_b[0]

print("Width A:", round(width_a, 3))
print("Width B:", round(width_b, 3))
print("Ratio:", round(width_a / width_b, 1))
# With comparable designs and variability, four times n gives roughly
# half the width. This is a planning approximation, not a guarantee.
Width A: 0.18
Width B: 0.09
Ratio: 2.0
### 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.

Hide code cell source

### Solution
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])

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("Claim inside interval:", left <= claimed <= right)
# This practice interval does not rule out 5%; it does not prove the claim
# or show that the supplier meets the buyer's requirements.
95% CI: 0.033 to 0.083
Claim inside interval: True