Skip to content

Commit a4c6eff

Browse files
authored
add subgroup analysis to hillstrom (#101)
* add subgroup analysis * up date discussion * update definition of mens/womens * apply men/women to male/female
1 parent f11045e commit a4c6eff

6 files changed

Lines changed: 1104 additions & 0 deletions

File tree

122 KB
Loading
70.4 KB
Loading
135 KB
Loading
60.8 KB
Loading

docs/source/tutorials/hillstrom.rst

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,327 @@ The side-by-side bar charts show probability treatment effects across different
332332

333333
**Conclusion**: Using the real Hillstrom dataset with 64,000 customers, the distributional analysis reveals nuanced patterns in how email campaigns affect customer spending. The analysis goes beyond simple average comparisons to show how treatment effects vary across the entire spending distribution, providing insights into which customer segments respond best to different campaign types. This demonstrates the power of distribution treatment effect analysis for understanding heterogeneous responses in digital marketing experiments.
334334

335+
Subgroup Analysis by Purchase History
336+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
337+
338+
Beyond comparing email campaigns overall, we can examine how campaign effectiveness varies by customer purchase history. This analysis segments customers based on their past purchasing behavior:
339+
340+
- **Men's merchandise purchasers** (``mens=1``): Customers who previously purchased men's merchandise (35,266 customers, 55.1%)
341+
- **Women's merchandise purchasers** (``womens=1``): Customers who previously purchased women's merchandise (35,182 customers, 55.0%)
342+
343+
Note that these segments overlap (6,448 customers purchased both categories), so a customer can appear in both analyses.
344+
345+
**Research Question**: Does the effectiveness of men's vs women's email campaigns vary by the type of merchandise customers have historically purchased?
346+
347+
Defining Subgroups
348+
^^^^^^^^^^^^^^^^^^^
349+
350+
.. code-block:: python
351+
352+
# Define subgroup masks based on purchase history
353+
mens_purchasers = (df['mens'] == 1)
354+
womens_purchasers = (df['womens'] == 1)
355+
356+
print(f"Men's merchandise purchaser segment: {mens_purchasers.sum():,} customers")
357+
print(f"Women's merchandise purchaser segment: {womens_purchasers.sum():,} customers")
358+
print(f"Overlap: {(mens_purchasers & womens_purchasers).sum():,} customers")
359+
360+
Average Treatment Effects by Subgroup
361+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
362+
363+
Let's first compute the average treatment effects (ATEs) to quantify the overall impact:
364+
365+
.. code-block:: python
366+
367+
# Compute ATEs for each campaign-subgroup combination
368+
# Women's Email Campaign
369+
ate_women_male = (revenue[(D==2) & mens_purchasers].mean() -
370+
revenue[(D==0) & mens_purchasers].mean())
371+
ate_women_female = (revenue[(D==2) & womens_purchasers].mean() -
372+
revenue[(D==0) & womens_purchasers].mean())
373+
374+
# Men's Email Campaign
375+
ate_men_male = (revenue[(D==1) & mens_purchasers].mean() -
376+
revenue[(D==0) & mens_purchasers].mean())
377+
ate_men_female = (revenue[(D==1) & womens_purchasers].mean() -
378+
revenue[(D==0) & womens_purchasers].mean())
379+
380+
print("Average Treatment Effects by Subgroup:")
381+
print("\nWomen's Email Campaign:")
382+
print(f" Men's Merch. Purchasers: ATE = ${ate_women_male:.4f}")
383+
print(f" Women's Merch. Purchasers: ATE = ${ate_women_female:.4f}")
384+
print("\nMen's Email Campaign:")
385+
print(f" Men's Merch. Purchasers: ATE = ${ate_men_male:.4f}")
386+
print(f" Women's Merch. Purchasers: ATE = ${ate_men_female:.4f}")
387+
388+
Expected output::
389+
390+
Average Treatment Effects by Subgroup:
391+
392+
Women's Email Campaign:
393+
Men's Merch. Purchasers: ATE = $0.2564
394+
Women's Merch. Purchasers: ATE = $0.5442
395+
396+
Men's Email Campaign:
397+
Men's Merch. Purchasers: ATE = $0.8966
398+
Women's Merch. Purchasers: ATE = $0.8412
399+
400+
These results reveal important patterns:
401+
402+
- **Women's Email Campaign**: Shows 2× stronger effect for women's merchandise purchasers ($0.54) vs men's merchandise purchasers ($0.26)
403+
- **Men's Email Campaign**: Demonstrates consistent strong effects across both segments ($0.84-$0.89)
404+
405+
While these averages provide a useful summary, they don't tell us *how* customer spending distributions change. The distributional and probability treatment effect analyses that follow reveal the complete picture of campaign effectiveness.
406+
407+
Distribution Treatment Effects: Women's Email Campaign
408+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
409+
410+
Beyond the average effects, let's examine how the Women's Email campaign shifts the entire spending distribution for each subgroup:
411+
412+
.. code-block:: python
413+
414+
# Analyze men's merchandise purchaser segment
415+
estimator_male = dte_adj.SimpleDistributionEstimator()
416+
estimator_male.fit(X[mens_purchasers], D[mens_purchasers], revenue[mens_purchasers])
417+
418+
# Analyze women's merchandise purchaser segment
419+
estimator_female = dte_adj.SimpleDistributionEstimator()
420+
estimator_female.fit(X[womens_purchasers], D[womens_purchasers], revenue[womens_purchasers])
421+
422+
# Define evaluation points
423+
locations = np.linspace(0, 500, 51)
424+
425+
# Compute DTE for Women's Email vs Control in each subgroup
426+
dte_women_male, lower_women_male, upper_women_male = estimator_male.predict_dte(
427+
target_treatment_arm=2, # Women's Email
428+
control_treatment_arm=0, # No Email
429+
locations=locations,
430+
variance_type="moment"
431+
)
432+
433+
dte_women_female, lower_women_female, upper_women_female = estimator_female.predict_dte(
434+
target_treatment_arm=2, # Women's Email
435+
control_treatment_arm=0, # No Email
436+
locations=locations,
437+
variance_type="moment"
438+
)
439+
440+
# Visualize side-by-side
441+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
442+
443+
plot(locations, dte_women_male, lower_women_male, upper_women_male,
444+
title="Women's Email vs Control\nMen's Merch. Purchasers",
445+
xlabel="Spending ($)", ylabel="Distribution Treatment Effect",
446+
color="purple", ax=ax1)
447+
ax1.axhline(y=0, color='black', linestyle='--', linewidth=0.8, alpha=0.5)
448+
449+
plot(locations, dte_women_female, lower_women_female, upper_women_female,
450+
title="Women's Email vs Control\nWomen's Merch. Purchasers",
451+
xlabel="Spending ($)", ylabel="Distribution Treatment Effect",
452+
color="green", ax=ax2)
453+
ax2.axhline(y=0, color='black', linestyle='--', linewidth=0.8, alpha=0.5)
454+
455+
plt.tight_layout()
456+
plt.show()
457+
458+
.. image:: ../_static/hillstorm_subgroup_women_dte.png
459+
:alt: Women's Email Campaign Subgroup Analysis
460+
:width: 800px
461+
:align: center
462+
463+
**Key Finding for Women's Email Campaign**: The distributional treatment effects reveal that women's email campaigns are significantly more effective for the women's merchandise purchaser segment (right panel) compared to the men's merchandise purchaser segment (left panel). The DTE curves show that women's emails reduce the probability of low spending levels (negative DTE at lower thresholds) for women's merchandise purchasers, indicating a shift toward higher spending. In contrast, the men's merchandise purchaser segment shows minimal or non-significant effects across most of the spending distribution, with confidence intervals overlapping zero.
464+
465+
Distribution Treatment Effects: Men's Email Campaign
466+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
467+
468+
Now let's examine how the Men's Email campaign affects spending distributions:
469+
470+
.. code-block:: python
471+
472+
# Compute DTE for Men's Email vs Control in each subgroup
473+
dte_men_male, lower_men_male, upper_men_male = estimator_male.predict_dte(
474+
target_treatment_arm=1, # Men's Email
475+
control_treatment_arm=0, # No Email
476+
locations=locations,
477+
variance_type="moment"
478+
)
479+
480+
dte_men_female, lower_men_female, upper_men_female = estimator_female.predict_dte(
481+
target_treatment_arm=1, # Men's Email
482+
control_treatment_arm=0, # No Email
483+
locations=locations,
484+
variance_type="moment"
485+
)
486+
487+
# Visualize side-by-side
488+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
489+
490+
plot(locations, dte_men_male, lower_men_male, upper_men_male,
491+
title="Men's Email vs Control\nMen's Merch. Purchasers",
492+
xlabel="Spending ($)", ylabel="Distribution Treatment Effect",
493+
color="purple", ax=ax1)
494+
ax1.axhline(y=0, color='black', linestyle='--', linewidth=0.8, alpha=0.5)
495+
496+
plot(locations, dte_men_female, lower_men_female, upper_men_female,
497+
title="Men's Email vs Control\nWomen's Merch. Purchasers",
498+
xlabel="Spending ($)", ylabel="Distribution Treatment Effect",
499+
color="green", ax=ax2)
500+
ax2.axhline(y=0, color='black', linestyle='--', linewidth=0.8, alpha=0.5)
501+
502+
plt.tight_layout()
503+
plt.show()
504+
505+
.. image:: ../_static/hillstorm_subgroup_men_dte.png
506+
:alt: Men's Email Campaign Subgroup Analysis
507+
:width: 800px
508+
:align: center
509+
510+
**Key Finding for Men's Email Campaign**: In contrast to women's email campaigns, men's email campaigns show consistent effectiveness across both purchase history segments. The DTE curves in both panels show similar patterns, with negative values at lower spending levels indicating reduced probability of low spending for both male and women's merchandise purchasers. This suggests that men's emails have broad appeal regardless of whether customers historically purchased men's or women's merchandise.
511+
512+
Probability Treatment Effects: Women's Email Campaign
513+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
514+
515+
While DTE shows how cumulative distributions shift, Probability Treatment Effects (PTE) reveal which specific spending intervals are most affected by the campaign. PTE measures the change in probability mass within each spending category:
516+
517+
.. code-block:: python
518+
519+
# Compute PTE for Women's Email vs Control in each subgroup
520+
pte_locations = np.insert(locations, 0, -1) # Add -1 at beginning for intervals
521+
522+
pte_women_male, pte_lower_women_male, pte_upper_women_male = estimator_male.predict_pte(
523+
target_treatment_arm=2, # Women's Email
524+
control_treatment_arm=0, # No Email
525+
locations=pte_locations,
526+
variance_type="moment"
527+
)
528+
529+
pte_women_female, pte_lower_women_female, pte_upper_women_female = estimator_female.predict_pte(
530+
target_treatment_arm=2, # Women's Email
531+
control_treatment_arm=0, # No Email
532+
locations=pte_locations,
533+
variance_type="moment"
534+
)
535+
536+
# Visualize side-by-side with bar charts
537+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
538+
539+
plot(locations, pte_women_male, pte_lower_women_male, pte_upper_women_male,
540+
chart_type="bar",
541+
title="Women's Email vs Control\nMen's Merch. Purchasers",
542+
xlabel="Spending Category ($)", ylabel="Probability Treatment Effect",
543+
color="purple", ax=ax1)
544+
ax1.axhline(y=0, color='black', linestyle='--', linewidth=0.8, alpha=0.5)
545+
546+
plot(locations, pte_women_female, pte_lower_women_female, pte_upper_women_female,
547+
chart_type="bar",
548+
title="Women's Email vs Control\nWomen's Merch. Purchasers",
549+
xlabel="Spending Category ($)", ylabel="Probability Treatment Effect",
550+
color="green", ax=ax2)
551+
ax2.axhline(y=0, color='black', linestyle='--', linewidth=0.8, alpha=0.5)
552+
553+
plt.tight_layout()
554+
plt.show()
555+
556+
.. image:: ../_static/hillstorm_subgroup_women_pte.png
557+
:alt: Women's Email Campaign PTE Subgroup Analysis
558+
:width: 800px
559+
:align: center
560+
561+
**Interval-Specific Insights**: The PTE bar charts reveal the mechanism behind the average treatment effect. For women's merchandise purchasers (right panel), women's emails significantly reduce the probability of zero spending (non-purchasers converting to purchasers), which is the primary driver of the positive ATE. However, no significant increase in high spending categories is observed. For men's merchandise purchasers (left panel), the effects are much smaller and less consistent, confirming the limited impact suggested by the ATE and DTE analyses.
562+
563+
Probability Treatment Effects: Men's Email Campaign
564+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
565+
566+
Let's examine which spending categories are most affected by men's email campaigns:
567+
568+
.. code-block:: python
569+
570+
# Compute PTE for Men's Email vs Control in each subgroup
571+
pte_men_male, pte_lower_men_male, pte_upper_men_male = estimator_male.predict_pte(
572+
target_treatment_arm=1, # Men's Email
573+
control_treatment_arm=0, # No Email
574+
locations=pte_locations,
575+
variance_type="moment"
576+
)
577+
578+
pte_men_female, pte_lower_men_female, pte_upper_men_female = estimator_female.predict_pte(
579+
target_treatment_arm=1, # Men's Email
580+
control_treatment_arm=0, # No Email
581+
locations=pte_locations,
582+
variance_type="moment"
583+
)
584+
585+
# Visualize side-by-side with bar charts
586+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
587+
588+
plot(locations, pte_men_male, pte_lower_men_male, pte_upper_men_male,
589+
chart_type="bar",
590+
title="Men's Email vs Control\nMen's Merch. Purchasers",
591+
xlabel="Spending Category ($)", ylabel="Probability Treatment Effect",
592+
color="purple", ax=ax1)
593+
ax1.axhline(y=0, color='black', linestyle='--', linewidth=0.8, alpha=0.5)
594+
595+
plot(locations, pte_men_female, pte_lower_men_female, pte_upper_men_female,
596+
chart_type="bar",
597+
title="Men's Email vs Control\nWomen's Merch. Purchasers",
598+
xlabel="Spending Category ($)", ylabel="Probability Treatment Effect",
599+
color="green", ax=ax2)
600+
ax2.axhline(y=0, color='black', linestyle='--', linewidth=0.8, alpha=0.5)
601+
602+
plt.tight_layout()
603+
plt.show()
604+
605+
.. image:: ../_static/hillstorm_subgroup_men_pte.png
606+
:alt: Men's Email Campaign PTE Subgroup Analysis
607+
:width: 800px
608+
:align: center
609+
610+
**Interval-Specific Insights**: Men's email campaigns show similar PTE patterns across both segments (left and right panels). The key mechanism is twofold: (1) significant reduction in zero spending probability (converting non-purchasers to purchasers), and (2) increased probability in the $40-100 spending range. This dual effect—both purchase conversion and mid-range spending increases—occurs consistently across both male and women's merchandise purchaser segments, confirming the broad effectiveness of men's campaigns.
611+
612+
Key Insights from Subgroup Analysis
613+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
614+
615+
Combining Average Treatment Effects (ATE), Distribution Treatment Effects (DTE), and Probability Treatment Effects (PTE) provides a comprehensive understanding of campaign effectiveness:
616+
617+
**1. Campaign Targeting Effectiveness (from ATE)**
618+
619+
- Women's email campaigns show 2× stronger average effects for women's merchandise purchasers ($0.54) vs men's merchandise purchasers ($0.26)
620+
- Men's email campaigns demonstrate consistent strong effects across both segments ($0.89-$0.84)
621+
- This suggests women's campaigns benefit from precise targeting, while men's campaigns have broader appeal
622+
623+
**2. Distributional Shifts Beyond Averages (from DTE)**
624+
625+
- For women's emails, the women's merchandise purchaser segment shows negative DTE at lower spending thresholds, indicating a systematic shift away from low-spending behavior
626+
- Men's merchandise purchasers show minimal distributional changes from women's emails, with confidence intervals overlapping zero at most thresholds
627+
- Men's emails produce similar distributional patterns across both segments, confirming broad effectiveness
628+
629+
**3. Spending Category Changes (from PTE)**
630+
631+
- PTE analysis reveals *which specific spending intervals* change in response to campaigns, particularly identifying the mechanisms behind average effects
632+
- **Women's emails**: For women's merchandise purchasers, the primary effect is converting non-purchasers to purchasers (significant reduction in zero spending probability). No significant increase in high spending categories was observed.
633+
- **Men's emails**: Show a dual mechanism across both segments: (1) converting non-purchasers to purchasers (zero spending reduction), and (2) increasing purchases in the $40-100 range
634+
- PTE enables identification of behavioral change mechanisms that are invisible in average treatment effects alone—specifically revealing that lift comes primarily from purchase conversion (0→1 effect) rather than spending increases among existing purchasers
635+
636+
**4. Strategic Implications**
637+
638+
Based on these findings, several practical implications emerge:
639+
640+
- **For Women's Campaigns**: Target customers with history of purchasing women's merchandise to maximize ROI. The PTE analysis reveals that effectiveness comes primarily from converting non-purchasers to purchasers among women's merchandise purchaser segments, rather than increasing spending among existing buyers.
641+
- **For Men's Campaigns**: Deploy broadly as they produce consistent positive effects across diverse customer segments. Both male and women's merchandise purchasers show both purchase conversion and mid-range spending increases, suggesting broader appeal.
642+
- **Resource Allocation**: One practical implication is to prioritize precise targeting for gender-specific content (women's emails) but invest confidently in broad deployment for broadly appealing content (men's emails).
643+
644+
**5. Methodological Value**
645+
646+
This three-tier analysis demonstrates why distributional methods matter:
647+
648+
- **ATE alone** would show that both campaigns have positive effects, but with varying magnitudes across subgroups
649+
- **Adding DTE** reveals *how* spending distributions shift, not just average changes
650+
- **Adding PTE** pinpoints *which spending categories* are most affected, enabling precise business decisions
651+
652+
By examining effects at average, distributional, and interval-specific levels, we gain actionable insights that would be invisible to traditional mean-comparison approaches. This demonstrates the power of distribution treatment effect methods for understanding heterogeneous responses in digital marketing experiments.
653+
654+
For the complete reproducible code including helper functions and visualizations, see `example/hillstrom.ipynb <https://github.com/CyberAgentAILab/python-dte-adjustment/blob/main/example/hillstrom.ipynb>`_.
655+
335656
Next Steps
336657
~~~~~~~~~~
337658

0 commit comments

Comments
 (0)