Last month I migrated an e-commerce retention pipeline off the archived CamDavidsonPilon/lifetimes package onto PyMC-Marketing v1.5.0. The script ran end to end without a single warning, exception, or NaN. Then I spot-checked the customer table before exporting it to CRM: our best weekly buyer had a P(Alive) score of 0.00%, while a customer who placed six orders in their first week and vanished for nearly ten months sat at 88.19% alive.
I stared at the output for twenty minutes before realizing the model was fine. My input table was upside down, and nothing in the library could ever catch it.
Why inverted recency never raises an error
If you already maintain a descriptive RFM table for marketing dashboards, you almost certainly define recency as T_end - last_order (days since the customer's most recent purchase). In that convention, smaller means fresher.
Fader, Hardie, and Lee (2005) define recency (t_x) in the exact opposite direction for BG/NBD, and both lifetimes and PyMC-Marketing follow their notation. In Buy Till You Die models, recency is last_order - first_order: the customer's active span up to their last repeat purchase, bounded by 0 <= t_x <= T, where T = T_end - first_order is the customer's age at the calibration cutoff. Larger t_x means more recently active, and one-time buyers (frequency == 0) have t_x = 0.0.
Here is why the bug stays completely silent. If t_x is valid (0 <= t_x <= T), then the dashboard quantity T - t_x also satisfies 0 <= T - t_x <= T for every single repeat buyer. No boundary check trips. No library raises ValueError("recency > T"). The optimizer converges cleanly and hands you an inverted ranking.
To measure how badly that silent swap scrambles a real customer base, I ran a controlled simulation with np.random.default_rng(20261203) over 1,200 customers and a 365.0-day window (1,003 repeat buyers). Fitting standard BG/NBD MLE gave r = 0.8569, alpha = 27.1529, a = 1.0988, and b = 4.1172. Passing T - t_x instead of t_x into P(Alive) flipped the rankings across all 1,003 repeat buyers to a Spearman correlation of -0.7885.
Look at two customers who both joined 300.0 days ago (T = 300.0) and placed 6 repeat orders (frequency = 6):
- A loyal buyer who ordered 3 days ago (
t_x = 297.0) has a trueP(Alive)of0.8862(88.62%). Under the inverted-recency bug, the model readsrecency = 3.0and scores them at0.0000(0.00%). - A churned buyer who placed all 6 repeat orders in week 1 (
t_x = 5.0) and went silent for 295 days has a trueP(Alive)of0.0000(0.00%). Under the bug, the model readsrecency = 295.0and scores them at0.8819(88.19%).
You can verify both numbers and the rank flip directly with numpy and scipy:
import numpy as np from scipy.stats import spearmanr rng = np.random.default_rng(20261203) # Fitted BG/NBD MLE parameters from the 1,200-customer, 365.0-day simulation r, alpha, a, b = 0.8569, 27.1529, 1.0988, 4.1172 def bgnbd_p_alive(x, t_x, T, r=r, alpha=alpha, a=a, b=b): x = np.asarray(x, dtype=float) t_x = np.asarray(t_x, dtype=float) T = np.asarray(T, dtype=float) p = np.ones_like(x, dtype=float) mask = x > 0 ratio = (alpha + T[mask]) / (alpha + t_x[mask]) p[mask] = 1.0 / (1.0 + (a / (b + x[mask] - 1.0)) * (ratio ** (r + x[mask]))) return p # 1. Concrete pair at T = 300.0 days, frequency = 6 for label, t_x in [("Loyal (3d ago)", 297.0), ("Churned (wk 1)", 5.0)]: true_p = bgnbd_p_alive([6], [t_x], [300.0])[0] bug_p = bgnbd_p_alive([6], [300.0 - t_x], [300.0])[0] print(f"{label}: true={true_p:.4f}, inverted={bug_p:.4f}") # 2. Zero-repeat buyer silent for almost a year (frequency = 0, T = 360.0) zero_p = bgnbd_p_alive([0], [0.0], [360.0])[0] print(f"Zero-repeat (T=360.0): P(Alive)={zero_p:.4f}") # 3. Quick rank-inversion check across synthetic repeat buyers T_rep = rng.uniform(60.0, 365.0, size=1003) x_rep = rng.integers(1, 10, size=1003) t_x_rep = T_rep * rng.beta(2.0, 2.0, size=1003) rho = spearmanr( bgnbd_p_alive(x_rep, t_x_rep, T_rep), bgnbd_p_alive(x_rep, T_rep - t_x_rep, T_rep), ).statistic print(f"Synthetic repeat-buyer rank correlation: {rho:.4f}") On the full 1,200-customer simulated transaction log (seed=20261203), that Spearman correlation lands at -0.7885. Even on the quick synthetic draw at the bottom of the snippet, you immediately see the sign flip negative without a single exception.
Why a silent one-time buyer still scores P(Alive) = 1.0
Right after I fixed the recency column, a stakeholder opened the table and asked why a one-time buyer from almost a year ago (frequency = 0, recency = 0.0, T = 360.0) still showed P(Alive) = 1.0.
That one is not a code bug. It is an analytical property of standard BG/NBD. In Fader, Hardie, and Lee (2005), a customer only flips the geometric dropout coin immediately after a repeat purchase (x >= 1). If someone has never placed a repeat order, the model has never executed a dropout flip for them, so P(Alive) evaluates to 1.0 identically no matter how large T gets.
Their expected future purchases still decay smoothly toward zero as T grows because the posterior estimate of their Poisson purchase rate updates through alpha + T. Dollar CLV rankings stay sensible. Still, if downstream teams inspect raw P(Alive) on one-time buyers and treat 1.0 as "definitely coming back," switch to ModifiedBetaGeoModel (MBG/NBD) or ParetoNBDModel in PyMC-Marketing v1.5.0, both of which allow dropout right after the initial acquisition order.
Uncollapsed SKU rows and the Gamma-Gamma check
Two more data-prep bugs bit me during the same migration.
First, raw order exports from Shopify, Stripe, or an ERP table usually store one row per SKU line item (1 to 4 SKUs per order), not one row per order. If you pass raw invoice rows straight into a BTYD summary helper without collapsing (customer_id, order_date), a single four-item shopping cart turns into three instant repeat purchases. On my 1,200-customer simulated cohort, skipping that aggregation inflated the cohort repeat rate from 83.58% to 97.17% and nearly tripled mean repeat frequency from 3.3617 to 9.9417.
Second, GammaGammaModel assumes that a customer's average repeat order value is independent of their repeat purchase frequency, and it must only be fit on repeat buyers (frequency > 0 and monetary_value > 0). Before fitting it, compute the Pearson correlation r_fm on that filtered subset and assert abs(r_fm) < 0.30. In the clean simulated cohort, r_fm came out to 0.0296. If abs(r_fm) crosses 0.30 in production, you are usually pooling wholesale B2B accounts or heavy promo hunters with ordinary retail buyers, and you need to split those segments before fitting spend.
Also watch the PyMC-Marketing v1.5.0 API change if you are porting old 0.x snippets: data no longer goes into __init__. You instantiate the model first and pass data into .fit(data=...):
from pymc_marketing import clv # 1. Collapse SKU line items to one positive-revenue row per (customer_id, order_date) tx = ( raw_df.query("net_revenue > 0") .groupby(["customer_id", "order_date"], as_index=False)["net_revenue"] .sum() ) # 2. Build BTYD inputs with a temporal holdout split (never reuse dashboard RFM columns) rfm_split = clv.utils.rfm_train_test_split( transactions=tx, customer_id_col="customer_id", datetime_col="order_date", monetary_value_col="net_revenue", train_period_end="2026-06-30", time_unit="W", ) # 3. Fit BG/NBD (method="map" for fast CPU iteration, method="mcmc" for credible intervals) bgm = clv.BetaGeoModel() bgm.fit(data=rfm_split, method="map") # 4. Mandatory Gamma-Gamma pre-checks on repeat buyers only repeat_df = rfm_split.query("frequency > 0 and monetary_value > 0").copy() r_fm = repeat_df[["frequency", "monetary_value"]].corr().iloc[0, 1] assert abs(r_fm) < 0.30, f"Gamma-Gamma independence violated: r_fm={r_fm:.4f}" gg = clv.GammaGammaModel() gg.fit(data=repeat_df, method="map") Three guards I keep in every CLV pipeline now
Keep descriptive dashboard features in their own table with unambiguous names (days_since_last_order, total_orders, total_spend), and let clv.utils.rfm_train_test_split() generate frequency, recency, T, and monetary_value after collapsing (customer_id, order_date). Spot-check one recent repeat buyer and one churned week-1 buyer right after .fit(), and keep assert abs(r_fm) < 0.30 in code so a bad upstream export fails loudly instead of shipping an inverted retention list.
