9.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. Stating the Null Hypothesis
A store tests a new checkout page. The null hypothesis is that the new page increases the purchase rate.
2. Reading a p-Value
A test of a new pricing policy gives a p-value of 0.03. This means there is a 3% chance that the null hypothesis is true.
3. Choosing a Test Statistic
An analyst checks whether a jury panel’s mix of five ethnic groups matches the eligible population. Large values of the total variation distance between the panel and the population are evidence against the model of random selection.
4. Not Rejecting
A test of a new warehouse process gives a p-value of 0.30. This proves that the new process has no effect.
5. Many Tests
A marketing team runs 20 independent tests in which the null hypothesis is actually true, using a 5% cutoff. They should expect about one of them to come out statistically significant.
### 6. Defect Rate Statistic
# A supplier claims that 5% of its parts are defective. In a shipment of 600 parts,
# 42 were defective.
# 1. Compute the observed percent defective, rounded to 2 decimals.
# 2. Compute the test statistic | observed percent - 5 |, rounded to 2 decimals.
# Print exactly:
# Observed percent: 7.0
# Test statistic: 2.0
import numpy as np
import pandas as pd
defective = 42
shipment = 600
claimed_percent = 5
### Your code starts here.
### Your code ends here.
Observed percent: 7.0
Test statistic: 2.0
### 7. Channel Mix TVD
# Compare this month's sales channel mix with last year's mix.
# 1. Compute the total variation distance between the two distributions,
# rounded to 2 decimals.
# Print exactly:
# TVD: 0.08
import numpy as np
import pandas as pd
mix = pd.DataFrame({
"channel": ["Web", "App", "Store", "Phone"],
"last_year": [0.40, 0.25, 0.30, 0.05],
"this_month": [0.42, 0.31, 0.22, 0.05],
})
### Your code starts here.
### Your code ends here.
TVD: 0.08
### 8. P-value for the Defect Rate
# Under the supplier's 5% claim, an analyst simulated 25 shipments of 600 parts and
# recorded the defective counts below. The setup converts those counts to
# | simulated percent - 5 | without rounding before the comparison.
# These 25 repetitions are for practice; use many more for a stable estimate.
# 1. Compute the p-value: the proportion of simulated statistics at least as
# large as the observed statistic of 2.0.
# Print exactly:
# P-value: 0.08
import numpy as np
import pandas as pd
observed_statistic = 2.0
# Counts generated by np.random.default_rng(6).binomial(600, 0.05, size=25).
simulated_defects = pd.Series([30, 28, 28, 28, 43, 32, 32, 28, 32, 24, 22, 36, 18,
41, 35, 34, 21, 26, 36, 29, 32, 24, 25, 30, 34])
simulated = (100 * simulated_defects / 600 - 5).abs()
### Your code starts here.
### Your code ends here.
P-value: 0.08
### 9. The Cutoff Matters
# The defect-rate test gave a p-value of 0.08.
# 1. Decide at a 5% cutoff: print "reject" if the p-value is below the cutoff,
# otherwise "do not reject".
# 2. Decide again at a 10% cutoff.
# These are two policies chosen in advance for comparison. Do not switch to
# 10% after seeing p = 0.08 merely to obtain a rejection. In a comment, explain
# why failing to reject at 5% does not prove that the supplier meets its claim.
# Print exactly:
# At 0.05: do not reject
# At 0.1: reject
import numpy as np
import pandas as pd
p_value = 0.08
cutoffs = [0.05, 0.1]
### Your code starts here.
### Your code ends here.
At 0.05: do not reject
At 0.1: reject
### 10. The Cutoff as a Statistic
# As in the chapter's percentile example, estimate an upper-tail boundary
# with the 95th percentile. For a small or discrete simulation, interpolation
# and ties mean it may not match a p-value decision exactly.
# 1. Compute the 95th percentile with np.percentile(simulated, 95).
# Keep the unrounded value for comparisons; round to 3 decimals for display.
# 2. Decide whether the observed TVD of 0.071 is above the unrounded value.
# Print exactly:
# 95th percentile: 0.068
# Observed is above it: True
import numpy as np
import pandas as pd
observed_tvd = 0.071
simulated = pd.Series([0.037, 0.028, 0.044, 0.051, 0.065, 0.050, 0.062, 0.010, 0.031, 0.025,
0.054, 0.061, 0.013, 0.046, 0.068, 0.073, 0.022, 0.064, 0.058, 0.039])
### Your code starts here.
### Your code ends here.
95th percentile: 0.068
Observed is above it: True