Survival analysis marketing
Skill afelipeg/Anthropic-Skills-for-enterprise-marketing-os/skills/survival-analysis-marketing
Models time-to-event in marketing: next purchase, churn, subscription renewal, promotion redemption. Handles censored observations correctly. Use when asked to: predict when a customer will next purchase, identify churn risk and timing, measure how discounts or email frequency accelerate or delay events, build replenishment campaign triggers, or estimate time-to-first-purchase for acquisition targeting. Also trigger when someone says "time to event", "survival curve", "kaplan meier", "cox model", "hazard ratio", "churn timing", "time to churn", "time to next purchase", "censored data", "censored records", "replenishment timing", "when will they buy", "how long until", or pastes data with event times and censoring indicators. Always renders inline HTML dashboard as primary output — never just text tables.From its SKILL.md
npx -y skills add afelipeg/Anthropic-Skills-for-enterprise-marketing-os --skill survival-analysis-marketingAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 1 stars1 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
8.4 KB, ~2.1k tokens by cl100k_base, as published. Nobody here has run it
Survival Analysis for Marketing
Time-to-event modeling with proper censored data handling. Three-tier approach: Kaplan–Meier (nonparametric) → Cox PH (semiparametric) → Time-to-Event prediction.
Core Equations (Katsov §2.6.2)
Survival Function (eq. 2.107–2.109)
S(t) = Pr(T > t) = 1 − F(t) # probability of surviving past t
# Kaplan-Meier estimator (eq. 2.109):
Ŝ(t) = ∏_{i ≤ t} (1 − d_i / n_i) # product over observed event times
Hazard Function (eq. 2.112–2.116)
h(t) = lim_{dt→0} Pr(t < T ≤ t+dt | T > t) / dt
= f(t) / S(t) = −d/dt [log S(t)]
H(t) = ∫₀ᵗ h(τ)dτ [cumulative hazard]
S(t) = exp(−H(t))
Cox Proportional Hazards (eq. 2.119–2.133)
h(t | x) = h₀(t) · exp(wᵀx) # baseline × risk ratio
# Partial likelihood (eq. 2.127–2.128):
L_i(w) = exp(wᵀx_i) / Σ_{j ∈ R(t_i)} exp(wᵀx_j)
# Personalized survival function (eq. 2.133):
S(t | x) = S₀(t)^{exp(wᵀx)}
# Breslow baseline cumulative hazard (eq. 2.131):
Ĥ₀(t) = Σ_{t_i ≤ t} d̂_i / Σ_{j ∈ R(t_i)} exp(wᵀx_j)
Key marketing interpretations:
exp(w_k)= hazard ratio for feature k → how much it accelerates/decelerates the eventHR > 1→ accelerates event (e.g., discount → faster purchase)HR < 1→ decelerates event (e.g., retention offer → slower churn)
Input Data Format (eq. 2.104, 2.118)
Each row: (customer_id, t_i, δ_i, x_i...)
| Column | Description |
|---|---|
t | Survival/censoring time (days, weeks, months from treatment) |
delta | 1 = event observed; 0 = censored (customer still active at analysis date) |
x_1..N | Feature vector: recency, frequency, discount_depth, n_emails, etc. |
Censoring rule: Customer is censored if event not observed by analysis date. Never drop censored records — they carry information (§2.6.2, p. 58–59).
Workflow
Step 1 — Prepare Data
python scripts/censored_data_prep.py \
--transactions data/txns.csv \
--analysis-date 2024-12-31 \
--event purchase \
--output data/survival_ready.csv
Step 2 — Kaplan-Meier (nonparametric baseline)
python scripts/kaplan_meier.py \
--data data/survival_ready.csv \
--group-col treatment \
--output results/km_output.json
Output: survival curve per group + log-rank test p-value.
Step 3 — Cox PH Model (covariate effects)
python scripts/cox_model.py \
--data data/survival_ready.csv \
--features recency frequency discount_depth n_emails \
--output results/cox_output.json
Output: hazard ratios, 95% CI, p-values, concordance index.
Step 4 — Predict Time-to-Event per Customer
python scripts/time_to_event_pred.py \
--data data/survival_ready.csv \
--cox-model results/cox_output.json \
--output results/tte_predictions.csv
Output: median survival time, p25/p75 per customer → use as campaign trigger.
Step 5 — Visualize Hazard Ratios
python scripts/hazard_ratio_viz.py \
--cox-results results/cox_output.json \
--output results/hr_forest_data.json
Step 6 — Export Dashboard JSON + Render
python scripts/export_survival_dashboard_json.py \
--km results/km_output.json \
--cox results/cox_output.json \
--tte results/tte_predictions.csv \
--output dashboard_data.json
Then call show_widget with references/survival_dashboard_template.html.
Output sequence:
1. [bash_tool] censored_data_prep.py
2. [bash_tool] kaplan_meier.py + cox_model.py + time_to_event_pred.py
3. [bash_tool] export_survival_dashboard_json.py → JSON
4. [show_widget] Render dashboard
5. [text] Exec recommendation: HR interpretation + campaign trigger timing
6. [text] PH assumption caveat if relevant
Scripts Reference
| Script | Key Inputs | Key Outputs |
|---|---|---|
censored_data_prep.py | raw txns CSV, analysis date, event type | survival-ready CSV with (t, delta, features) |
kaplan_meier.py | survival CSV, optional group col | KM curve JSON + log-rank p-value |
cox_model.py | survival CSV + feature cols | HR table, concordance, baseline cumhaz |
time_to_event_pred.py | survival CSV + cox JSON | per-customer median TTE + p25/p75 |
hazard_ratio_viz.py | cox JSON | forest plot data JSON |
export_survival_dashboard_json.py | km + cox + tte JSONs | unified dashboard JSON |
Output Format — Visualization First
Primary output: inline HTML dashboard. Always render before text.
Dashboard panels (see references/survival_dashboard_template.html):
- KPI bar — median survival time, concordance index, n events, n censored, % censored
- Survival curve(s) — KM estimator, one line per group, with 95% CI band
- Hazard ratio forest plot — one row per feature, dot + CI bar, HR=1 reference line
- Cumulative hazard H(t) — log scale, shows proportional hazard structure
- TTE distribution — histogram of predicted median time-to-event across customers
- Log-rank test summary — p-value and interpretation per group comparison
Fallback: Katsov Example 2.1 format — t | n_at_risk | events | S(t) | CI_lower | CI_upper
Marketing Applications (§3.5.6)
| Application | Event | Censoring rule | Campaign action |
|---|---|---|---|
| Replenishment | Next purchase | Analysis date | Send notification N days before predicted purchase |
| Churn detection | Subscription cancel | Still active | Retention offer at t = median × 0.7 |
| Acquisition | First purchase | Analysis date | Retarget users nearing median TTE |
| Promotion redemption | Coupon use | Expiry date | Adjust offer timing to HR of discount_depth |
| Reactivation | Return after lapse | End of observation | Winback trigger at predicted re-engagement |
Key Caveats
- Proportional hazards assumption. Cox model requires
h_i(t)/h_j(t) = constantacross time. Validate with Schoenfeld residuals or log-log plot. Violation → use time-varying coefficients or stratified Cox. - Censoring must be non-informative. Censored customers must not differ systematically from uncensored ones (e.g., don't censor high-value customers selectively).
- Event definition determines the model. Purchase ≠ redemption ≠ churn. Build separate models per event type.
- Repeated events. KM/Cox assume single event per customer. For repeat purchases, model each purchase interval separately (gap time model).
- Marketing interpretation of HR. HR = exp(w_k): a 1-unit increase in feature k multiplies the event rate by HR. For binary features (treatment=1/0), HR is the treatment effect directly.
Integration with Agency Growth OS
| Skill | Handoff |
|---|---|
customer-lifetime-value | S_u(t) feeds LTV survival model (eq. 3.31) |
response-uplift-modeling | Survival curves compare treated vs control groups |
crm-journey-architect | Median TTE → message timing in replenishment journeys |
audience-segmentation-brief | TTE segments → urgency tiers for targeting |
measurement-incrementality | KM group comparison = A/B test measurement |
Reference Files
references/katsov_survival_excerpts.md— Equations 2.104–2.133, Example 2.1, §3.5.6references/model_selection_survival.md— KM vs Cox vs parametric decision guidereferences/survival_dashboard_template.html— Reusable HTML dashboard; injectSKILL_DATA_JSON
What ships with it: 9 files
41.5 KB alongside SKILL.md, 6 of them executable
references/
scripts/
- censored_data_prep.pyruns3.8 KB
- cox_model.pyruns6.1 KB
- export_survival_dashboard_json.pyruns2.9 KB
- hazard_ratio_viz.pyruns2.3 KB
- kaplan_meier.pyruns5.5 KB
- time_to_event_pred.pyruns4.2 KB