Customer Lifetime Value¶
Bayesian CLV models (BG/NBD + Gamma-Gamma) powering LTV-aware experiment economics.
LTV / CLV modeling: RFM preprocessing + BG/NBD + Gamma-Gamma likelihoods.
preprocess is pandas-only and imported eagerly; the pytensor likelihood
expressions live in mmm_framework.ltv.likelihood (import it explicitly —
kept out of this namespace so import mmm_framework.ltv stays light, the
same lazy-import convention as mmm_extensions).
- mmm_framework.ltv.transactions_to_rfm(df, *, customer_col='customer_id', date_col='date', value_col=None, observation_end=None, freq='W', segment_col=None)[source]¶
Collapse a transaction log to one row per customer (RFM summaries).
Multiple transactions by the same customer on the same date are merged into one purchase (values summed) — sub-period repeats are indistinguishable at the model’s time resolution and would otherwise inflate
frequency.- Parameters:
df (
DataFrame) – long transaction log (one row per transaction).value_col (
str|None) – transaction value column; omit for frequency-only (nomonetaryoutput column).observation_end (
Timestamp|str|None) – end of the calibration window (transactions after it are dropped). Defaults to the last transaction date.freq (
str) – time unit forrecency/T— ‘D’, ‘W’ (default) or ‘M’.segment_col (
str|None) – per-customer attribute (e.g. acquisition channel) carried through as asegmentcolumn — the FIRST transaction’s value per customer (the acquisition event’s attribution).
- Return type:
- Returns:
DataFrame indexed by customer with columns
frequency,recency,T,n_txn(raw purchase count) and, whenvalue_colis given,monetary(mean REPEAT-transaction value; NaN for one-time buyers), plussegmentwhensegment_colis given.
- mmm_framework.ltv.rfm_arrays(rfm)[source]¶
(frequency, recency, T, monetary-or-None)float arrays for the model.
- mmm_framework.ltv.new_customer_clv_series(transactions, clv_per_customer, *, customer_col='customer_id', date_col='date', freq='W-MON')[source]¶
Weekly (or
freq-period) cohort CLV: for each period, the number of NEWLY ACQUIRED customers and the sum of their (posterior-mean) CLV.clv_per_customeris indexed by customer id (e.g. the posterior mean of the fitted model’s per-customerclvdeterministic). Customers in the log but missing from the CLV index contribute count but zero value (and are counted inn_unvaluedfor honesty).Returns a DataFrame indexed by period start with columns
new_customers,cohort_clv,mean_clv,n_unvalued.- Return type:
- mmm_framework.ltv.clv_to_cac(segment_clv, cac)[source]¶
Blended acquisition economics per segment/channel.
- Parameters:
- Return type:
- Returns:
DataFrame with
clv,cac,clv_minus_cacandclv_to_cacper segment, sorted byclv_minus_cacdescending. Segments missing a CAC get NaN economics (still listed, flagged).