Homework

10.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. Opt-In Comparisons

A store shows a new product page only to customers who click “Try the new design.” Those customers buy more often than everyone else. This proves that the new page causes more purchases.

2. A Rare Result

An analyst shuffles the group labels 5,000 times and none of the shuffled gaps reaches the observed gap. The report says: “The p-value is exactly 0, so chance is impossible.”

3. Either Direction

A manager wants to know whether a new onboarding email changes customers’ renewal rates, whether up or down. The absolute difference in renewal rates is a sensible test statistic for this question.

4. A Randomized Trial

Employees were randomly assigned to a new training program or to the usual one. Follow-up and sales measurement were comparable, and participants did not affect one another’s outcomes. The new-program group had higher average sales, and a permutation test gives a p-value of 0.002. This is evidence that the new program caused an improvement for these participants.

5. A Quiet Result

In a randomized trial with 30 customers per group, a retention call gives a p-value of 0.30. The report concludes: “The call has no effect on renewal.”

### 6. Month-Adjusted Weekend Gap
# Real online-store sessions (UCI Online Shoppers), counted by month and by weekend or
# weekday. Customers chose when to shop, and conversion differs a lot by month.
#   1. Compute the overall weekend minus weekday conversion gap in percentage points
#      (add up sessions and purchases across months first).
#   2. Compute the gap within each month, then average the monthly gaps weighted by
#      each month's number of weekend sessions.
#   3. Print both gaps rounded to 2 decimals.
# Print exactly:
# Overall gap: 2.51
# Gap within months: 1.84
import numpy as np
import pandas as pd
months = pd.DataFrame({
    "month": ["Feb", "Mar", "May", "June", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
    "weekday_sessions": [156, 1426, 2649, 241, 328, 337, 352, 405, 2207, 1361],
    "weekday_purchases": [2, 127, 266, 24, 50, 64, 64, 85, 550, 177],
    "weekend_sessions": [28, 481, 715, 47, 104, 96, 96, 144, 791, 366],
    "weekend_purchases": [1, 65, 99, 5, 16, 12, 22, 30, 210, 39],
})
### Your code starts here.



### Your code ends here.
### 7. A Two-Sided Test on the Retention Trial
# In a randomized trial, 28 of 40 called customers renewed and 17 of 40 uncalled
# customers renewed. Either an increase or a decrease would matter to the company.
#   1. Repeat repetitions times: shuffle labels with rng.permutation (one shuffle per
#      repetition) and store the absolute difference in renewal rates.
#   2. Compute the proportion of simulated distances at least as large as observed.
#   3. Print the p-value rounded to 2 decimals and the decision at the 5% cutoff,
#      using the unrounded proportion.
# Print exactly:
# P-value: 0.02
# Decision: reject the null
import numpy as np
import pandas as pd
labels = np.array(['call'] * 40 + ['no call'] * 40)
outcomes = np.array([1] * 28 + [0] * 12 + [1] * 17 + [0] * 23)
observed = abs(outcomes[labels == 'call'].mean() - outcomes[labels == 'no call'].mean())
rng = np.random.default_rng(7)
repetitions = 5000
### Your code starts here.



### Your code ends here.
### 8. Is the Program Worth It?
# A retention program called 50 randomly chosen customers at $60 per call. Of the 50
# called customers 31 renewed; of 50 uncalled customers 18 renewed. One extra
# renewal is worth $250 in profit.
#   1. Compute the total cost, the extra renewals (called renewals minus uncalled
#      renewals, since both groups have 50), and the cost per extra renewal.
#   2. Print them, with the cost per extra renewal rounded to 2 decimals, then
#      yes or no: is the cost per extra renewal below the $250 profit?
# Print exactly:
# Total cost: 3000
# Extra renewals: 13
# Cost per extra renewal: 230.77
# Worth it: yes
import numpy as np
import pandas as pd
n_called = 50
cost_per_call = 60
renewed_called = 31
renewed_uncalled = 18
profit_per_renewal = 250
### Your code starts here.



### Your code ends here.
### 9. Percentage Points or Relative Lift
# Two randomized tests changed a baseline conversion rate:
#   Test 1: 4% to 5%      Test 2: 20% to 22%
#   1. For each test compute the difference in percentage points and the relative
#      lift in percent, rounded to 1 decimal.
#   2. Print the name of the test with the larger relative lift.
# Print exactly:
# Test 1: 1.0 pp, 25.0 percent
# Test 2: 2.0 pp, 10.0 percent
# Larger relative lift: Test 1
import numpy as np
import pandas as pd
before_1 = 0.04
after_1 = 0.05
before_2 = 0.20
after_2 = 0.22
### Your code starts here.



### Your code ends here.
### 10. What Can Each Study Claim?
# Four studies have consistent outcome measurement and comparable follow-up. Assume no
# interference, and that the randomized studies were conducted as planned.
# Classify each study:
#   - p-value of 0.05 or more             -> "no evidence of a difference"
#     (insufficient evidence at this cutoff, not proof of no effect)
#   - p-value below 0.05 and randomized   -> "evidence of a causal effect"
#   - p-value below 0.05, not randomized  -> "association only"
# Print one line per study, in table order, as "Study: claim".
# Print exactly:
# Coupon email: evidence of a causal effect
# Loyalty members: association only
# New layout: no evidence of a difference
# Free shipping: no evidence of a difference
import numpy as np
import pandas as pd
studies = pd.DataFrame({
    "study": ["Coupon email", "Loyalty members", "New layout", "Free shipping"],
    "randomized": [True, False, True, False],
    "p_value": [0.01, 0.002, 0.34, 0.20],
})
### Your code starts here.



### Your code ends here.