Survival Analysis Marketing
ASecurityModels 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 mod...
- 2 stars
- 0 votes
- 0 copies
- 0 views
- Added December 5, 2026
Security analysis
100/100Pro scans all 10 files and shows the line behind each finding
npx -y skills add afelipeg/Anthropic-Skills-for-enterprise-marketing-os --skill survival-analysis-marketing --agent claude-codeAre you the author of Survival Analysis Marketing?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/afelipeg-survival-analysis-marketing)---
name: survival-analysis-marketing
description: >
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.
metadata:
author: Axiom Nexar / Polanyi
version: 1.0.0
reference: >
Katsov, Introduction to Algorithmic Marketing, Ch. 2 §2.6.2 (pp. 58–68),
Ch. 3 §3.5.6; Kaplan & Meier (1958); Cox (1972)
---
# 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 event
- `HR > 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
```bash
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)
```bash
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)
```bash
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
```bash
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
```bash
python scripts/hazard_ratio_viz.py \
--cox-results results/cox_output.json \
--output results/hr_forest_data.json
```
### Step 6 — Export Dashboard JSON + Render
```bash
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`):
1. **KPI bar** — median survival time, concordance index, n events, n censored, % censored
2. **Survival curve(s)** — KM estimator, one line per group, with 95% CI band
3. **Hazard ratio forest plot** — one row per feature, dot + CI bar, HR=1 reference line
4. **Cumulative hazard H(t)** — log scale, shows proportional hazard structure
5. **TTE distribution** — histogram of predicted median time-to-event across customers
6. **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) = constant` across 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.6
- `references/model_selection_survival.md` — KM vs Cox vs parametric decision guide
- `references/survival_dashboard_template.html` — Reusable HTML dashboard; inject `SKILL_DATA_JSON`
Files in this skill
- SKILL.md
- references/katsov_survival_excerpts.md
- references/model_selection_survival.md
- references/survival_dashboard_template.html
- scripts/censored_data_prep.py
- scripts/cox_model.py
- scripts/export_survival_dashboard_json.py
- scripts/hazard_ratio_viz.py
- scripts/kaplan_meier.py
- scripts/time_to_event_pred.py
Attribution
Related skills
View all in ai-agents →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.