Homework

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.

Hide code cell source

### Solution
# False
# The null hypothesis is the chance model: the new page makes no difference to the purchase rate. "The page increases the rate" is the alternative.

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.

Hide code cell source

### Solution
# False
# The p-value is the chance, computed assuming the null hypothesis is true, of a test statistic at least as extreme as the one observed. It is not the probability 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.

Hide code cell source

### Solution
# True
# The TVD measures how far apart the two distributions are. Random selection tends to give small TVDs, so large values favor the alternative.

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.

Hide code cell source

### Solution
# False
# A large p-value means the data are consistent with the null hypothesis, not that the null is proven. The effect may be small, or the sample may be too small to detect it.

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.

Hide code cell source

### Solution
# True
# With a 5% cutoff, a true null hypothesis is rejected about 5% of the time, and 5% of 20 tests is 1. That significant result is a Type I error (a false alarm).
### 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.

Hide code cell source

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

defective = 42
shipment = 600
claimed_percent = 5

observed_percent = round(100 * defective / shipment, 2)
print("Observed percent:", observed_percent)
print("Test statistic:", round(abs(observed_percent - claimed_percent), 2))
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.

Hide code cell source

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

tvd = round((mix['this_month'] - mix['last_year']).abs().sum() / 2, 2)
print("TVD:", tvd)
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.

Hide code cell source

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

p_value = (simulated >= observed_statistic).mean()
print("P-value:", p_value)
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.

Hide code cell source

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

p_value = 0.08
cutoffs = [0.05, 0.1]

for cutoff in cutoffs:
    if p_value < cutoff:
        decision = "reject"
    else:
        decision = "do not reject"
    print("At " + str(cutoff) + ":", decision)
# Report the p-value along with the decision, so readers can judge for themselves.

# At 5%, the evidence is insufficient to reject the model. That does not
# establish that the claimed rate is correct or that the shipment is acceptable.
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.

Hide code cell source

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

cutoff_value = np.percentile(simulated, 95)
print("95th percentile:", round(cutoff_value, 3))
print("Observed is above it:", observed_tvd > cutoff_value)
95th percentile: 0.068
Observed is above it: True