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 the age mix of a survey’s respondents across five age groups matches the age mix of the customer base. Large values of the total variation distance between the respondents and the customer base are evidence against the model that the respondents are a random sample of customers.
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 promises that at most 5% of its parts are defective. In a shipment
# of 600 parts, 42 were defective. Fewer defects than promised is good news, so
# only too many defects count against the claim. The test is one-sided, and the
# statistic keeps its sign: observed percent - 5, with no absolute value.
# 1. Compute the observed percent defective, rounded to 2 decimals.
# 2. Compute the one-sided 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.
### 7. Channel Mix From Order Records
# The order log has one row per order this month. Last year's channel mix is
# given as shares. Compare the two distributions.
# 1. Turn the order records into this month's channel shares with
# value_counts(normalize=True).
# 2. Put the shares in the same channel order as last_year with
# reindex(channels), so each channel is compared with itself.
# 3. Compute the total variation distance, rounded to 2 decimals.
# Print exactly:
# TVD: 0.08
import numpy as np
import pandas as pd
channels = ["Web", "App", "Store", "Phone"]
last_year = pd.Series([0.40, 0.25, 0.30, 0.05], index=channels)
# A simulated log of 250 orders, generated with a fixed seed so everyone
# works with the same records.
orders = pd.DataFrame({
"channel": np.random.default_rng(174).choice(channels, size=250, p=[0.42, 0.31, 0.22, 0.05]),
})
### Your code starts here.
### Your code ends here.
### 8. One-Sided p-Value by Simulation
# The same supplier sends a smaller shipment: 15 of 200 parts are defective
# (7.5%). Under the claim, each part is defective with probability 0.05.
# Only too many defects count against the claim.
# 1. Simulate 10,000 shipments of 200 parts under the claim with
# rng.binomial(200, 0.05, size=10000).
# 2. Compute the one-sided p-value: the share of simulated shipments with
# at least as many defective parts as observed.
# 3. Print the p-value rounded to 2 decimals.
# Print exactly:
# P-value: 0.08
import numpy as np
import pandas as pd
rng = np.random.default_rng(4)
observed_defects = 15
### Your code starts here.
### Your code ends here.
### 9. Cutoffs on the Defect Count
# A cutoff can also be stated on the statistic itself, as in the chapter's
# percentile example. For a one-sided test where large counts count against
# the claim, a 5% rule rejects when the observed count is above the 95th
# percentile of the simulated counts, and a 10% rule uses the 90th percentile.
# Two buyers chose their rules before the shipment arrived: Buyer A uses 5%.
# Buyer B uses 10%, because keeping a supplier that ships too many defects
# would be costly for it.
# 1. Simulate the same 10,000 shipments as in Question 8.
# 2. For each cutoff, compute the matching percentile of the simulated
# counts. Print "reject" if the observed count is above it, otherwise
# "do not reject".
# 3. Print which error (Type I or Type II) Buyer B accepts more of by using
# 10%.
# Print exactly:
# At 0.05: do not reject
# At 0.1: reject
# Buyer B accepts more: Type I
import numpy as np
import pandas as pd
rng = np.random.default_rng(4)
observed_defects = 15
cutoffs = [0.05, 0.1]
### Your code starts here.
### Your code ends here.
### 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.