Lab

10.4.2. Lab#

  • 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.

Follow an online store’s checkout experiment from group summaries to a simulated test, then see why the same kind of comparison needs more care when customers choose their own groups. Choose the direction of the test and the 5% cutoff before looking at results. Each question includes its own setup.

The seeded simulations make your results reproducible. Use unrounded values for comparisons and decisions. A finite simulation cannot show that a probability is exactly zero, so Question 4 reports the count of extreme shuffles out of the repetitions.

### Question 1: Revenue per Visitor
# A store randomly showed checkout page A or B to 5,000 visitors each (summary of the
# chapter's checkout test). mean_order is the average order value among buyers, in dollars.
# Visitors who did not buy count as $0, so revenue per visitor =
# buyers * mean_order / visitors.
#   1. Compute revenue per visitor for each version.
#   2. Print both rounded to 2 decimals, and the difference B minus A.
# Print exactly:
# A: 5.07
# B: 6.54
# B minus A: 1.47
import numpy as np
import pandas as pd
checkout = pd.DataFrame({
    "version": ["A", "B"],
    "visitors": [5000, 5000],
    "buyers": [400, 500],
    "mean_order": [63.34, 65.42],
})
### Your code starts here.



### Your code ends here.
### Question 2: Conversion Rate, Percentage Points, and Lift
# Use the same summary table. The conversion rate is buyers divided by visitors.
#   1. Compute the conversion rate for each version.
#   2. Print both rates, the difference B minus A in percentage points (rate
#      difference x 100), and the relative lift in percent, both rounded to 1 decimal.
# Print exactly:
# Rate A: 0.08
# Rate B: 0.1
# Difference: 2.0 percentage points
# Relative lift: 25.0 percent
import numpy as np
import pandas as pd
checkout = pd.DataFrame({
    "version": ["A", "B"],
    "visitors": [5000, 5000],
    "buyers": [400, 500],
    "mean_order": [63.34, 65.42],
})
### Your code starts here.



### Your code ends here.
### Question 3: One Shuffle of the Labels
# The same test as one row per visitor (1 = bought). Under the null model the
# version labels are interchangeable while the outcomes stay fixed.
#   1. Shuffle the version labels once with rng.permutation.
#   2. Compute the conversion gap with the shuffled labels, B minus A, in
#      percentage points rounded to 2 decimals.
# Print exactly:
# Shuffled gap: 0.4 percentage points
import numpy as np
import pandas as pd
version = np.array(['A'] * 5000 + ['B'] * 5000)
purchased = np.array([1] * 400 + [0] * 4600 + [1] * 500 + [0] * 4500)
rng = np.random.default_rng(10)
### Your code starts here.



### Your code ends here.
### Question 4: Permutation Test and Reporting
# Test whether B converts more than A. The alternative says B is higher; the
# cutoff is 5%, chosen before looking at the result.
#   1. Repeat repetitions times: shuffle version with rng.permutation (one shuffle
#      per repetition) and store the conversion gap, B minus A.
#   2. Count the simulated gaps at least as large as observed_gap.
#   3. Print the count out of the repetitions, not a rounded p-value, then the
#      decision using the unrounded proportion.
# Print exactly:
# Extreme shuffles: 0 of 1000
# Decision: reject the null
import numpy as np
import pandas as pd
version = np.array(['A'] * 5000 + ['B'] * 5000)
purchased = np.array([1] * 400 + [0] * 4600 + [1] * 500 + [0] * 4500)
rng = np.random.default_rng(42)
observed_gap = purchased[version == 'B'].mean() - purchased[version == 'A'].mean()
repetitions = 1000
### Your code starts here.



### Your code ends here.
### Question 5: Does the Weekend Gap Survive Visitor Type?
# Real sessions from an online store (UCI Online Shoppers, the data in Section 10.2),
# counted by visitor type and weekend (1) or weekday (0). Nobody assigned visits to
# days. Only new and returning visitors are included.
#   1. Add a rate column: purchases divided by sessions.
#   2. Compute the weekend minus weekday gap in percentage points within New and
#      within Returning sessions, and overall (add up sessions and purchases first).
#   3. Print the three gaps rounded to 1 decimal. Then print whether the New and
#      Returning gaps have opposite signs.
# Print exactly:
# Overall gap: 2.6
# New visitors: -4.2
# Returning visitors: 3.3
# Opposite signs: True
import numpy as np
import pandas as pd
cells = pd.DataFrame({
    "visitor_type": ["New", "New", "Returning", "Returning"],
    "weekend": [0, 1, 0, 1],
    "sessions": [1215, 479, 8169, 2382],
    "purchases": [317, 105, 1077, 393],
})
### Your code starts here.



### Your code ends here.