agentsclimarketplace

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

Install
npx -y skills add afelipeg/Anthropic-Skills-for-enterprise-marketing-os --skill survival-analysis-marketing

Assembled 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 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...)

ColumnDescription
tSurvival/censoring time (days, weeks, months from treatment)
delta1 = event observed; 0 = censored (customer still active at analysis date)
x_1..NFeature 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

ScriptKey InputsKey Outputs
censored_data_prep.pyraw txns CSV, analysis date, event typesurvival-ready CSV with (t, delta, features)
kaplan_meier.pysurvival CSV, optional group colKM curve JSON + log-rank p-value
cox_model.pysurvival CSV + feature colsHR table, concordance, baseline cumhaz
time_to_event_pred.pysurvival CSV + cox JSONper-customer median TTE + p25/p75
hazard_ratio_viz.pycox JSONforest plot data JSON
export_survival_dashboard_json.pykm + cox + tte JSONsunified 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)

ApplicationEventCensoring ruleCampaign action
ReplenishmentNext purchaseAnalysis dateSend notification N days before predicted purchase
Churn detectionSubscription cancelStill activeRetention offer at t = median × 0.7
AcquisitionFirst purchaseAnalysis dateRetarget users nearing median TTE
Promotion redemptionCoupon useExpiry dateAdjust offer timing to HR of discount_depth
ReactivationReturn after lapseEnd of observationWinback 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

SkillHandoff
customer-lifetime-valueS_u(t) feeds LTV survival model (eq. 3.31)
response-uplift-modelingSurvival curves compare treated vs control groups
crm-journey-architectMedian TTE → message timing in replenishment journeys
audience-segmentation-briefTTE segments → urgency tiers for targeting
measurement-incrementalityKM 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

What ships with it: 9 files

41.5 KB alongside SKILL.md, 6 of them executable

Keep looking

Skills are one crate of 326,851. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.