- Monthly growth rate and two-month forecast
- A/B test of a landing page
- Seasonal indices and quarterly forecast
- Bass diffusion curve of a new app
- RFM segmentation of customers
- Customer lifetime value with retention
- Average basket after a product page redesign
- Saturating media response and the optimal spend
- Best send time of a newsletter
- Media mix model through the origin
- Baskets that bring the most margin
- Promotions and seasonal products
- Segment the customers with RFM
- Customer lifetime value from the transactions (BG/NBD and Gamma-Gamma)
- Multi-touch attribution: last click against Markov and Shapley
- Clean the addresses of a newsletter before sending
Monthly growth rate and two-month forecast
Revenue grew from 41.2 to 69.8 k EUR in 10 months. The script fits an exponential trend, turns its coefficient into a monthly growth rate and forecasts months 11 and 12.
#1. Monthly revenue in k EUR over 10 months; -> "[x]" "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"; -> "[y]" "[41.2, 43.5, 46.9, 48.1, 52.4, 55.0, 58.9, 61.3, 66.0, 69.8]"; #2. Exponential trend y = a*exp b*x; json load "rx_fit" (stat fit [x] [y] "exp"); -> "[b]" (json select "rx_fit" "/b"); #3. Monthly growth rate and forecast of months 11 to 12; -> "[f]" (json select "rx_fit" "/expression"); concat "R2 = " (math round (json select "rx_fit" "/r2") 4) ", growth " (math round (math calc "(exp([b])-1)*100") 2) "% a month, forecast M11 " (math round (math eval [f] "{\"x\": 11}") 1) ", M12 " (math round (math eval [f] "{\"x\": 12}") 1) " k EUR.";
R2 = 0.9973, growth 6.03% a month, forecast M11 73.9, M12 78.3 k EUR.
- Compare with a linear trend: same call with "linear" and compare the r2
- Market saturation: the "logistic" model of
stat fitgives the ceiling c
A/B test of a landing page
Version A converted 120 visitors out of 2400 and version B 156 out of 2350. The script tests the independence of version and conversion with a chi-square test and gives the rates, the uplift and the decision at the 5% level.
#1. A/B test of a landing page: conversions and non-conversions; -> "[table]" "[[120, 2280], [156, 2194]]"; #2. Chi-square test of independence; json load "rx_ab" (stat chi2_independence [table]); -> "[p]" (json select "rx_ab" "/p_value"); #3. Conversion rates and decision at 5%; -> "[ra]" (math calc "120/2400*100"); -> "[rb]" (math calc "156/2350*100"); concat "A: " [ra] "%, B: " (math round [rb] 2) "%, uplift " (math round (math calc "([rb]-[ra])/[ra]*100") 1) "%, chi2 = " (math round (json select "rx_ab" "/chi2") 3) ", p = " (math round [p] 4) " -> " (if (< [p] 0.05) {"significant"} {"not significant"});
A: 5%, B: 6.64%, uplift 32.8%, chi2 = 5.823, p = 0.0158 -> significant
- Small samples:
stat fisher_exacton the same 2x2 table - Sample size needed: solve the power equation with
num solveandstat dist_inv
Seasonal indices and quarterly forecast
Quarterly sales over three years show a strong Q3 peak. The script computes the seasonal index of a quarter (its mean over the mean of all quarters), fits a linear trend on the annual totals and forecasts year 4 quarter by quarter.
#1. Quarterly sales over 3 years; -> "[q1]" "[182, 195, 210]"; -> "[q2]" "[240, 258, 271]"; -> "[q3]" "[310, 322, 349]"; -> "[q4]" "[205, 214, 236]"; #2. Seasonal index: mean of the quarter over mean of all quarters; -> "[m]" (/ (+ (stat sum [q1]) (stat sum [q2]) (stat sum [q3]) (stat sum [q4])) 12); -> "[i1]" (math round (/ (stat mean [q1]) [m]) 3); -> "[i3]" (math round (/ (stat mean [q3]) [m]) 3); #3. Linear trend of the annual totals, forecast of year 4; json load "rx_tr" (stat fit "[1, 2, 3]" "[937, 989, 1066]" "linear"); -> "[y4]" (math eval (json select "rx_tr" "/expression") "{\"x\": 4}"); concat "Indices Q1 " [i1] ", Q3 " [i3] ". Year 4 forecast " (math round [y4] 0) ", Q1 " (math round (* (/ [y4] 4) [i1]) 0) ", Q3 " (math round (* (/ [y4] 4) [i3]) 0);
Indices Q1 0.785, Q3 1.311. Year 4 forecast 1126, Q1 221, Q3 369
- Deseasonalised series: divide each quarter by its index before fitting the trend
- Check the seasonality with
stat anovaon the four quarter series
Bass diffusion curve of a new app
Cumulative adopters of a new app follow the Bass model (innovation p, imitation q, market size m). The script fits the model, checks with the CAS that the curve saturates at m, and draws the measures with the fitted S curve.
#1. Cumulative adopters of a new app in thousands, month 1 to 12; -> "[t]" "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]"; -> "[N]" "[3.1, 7.6, 14.2, 23.5, 36.0, 51.2, 67.4, 82.5, 95.1, 104.6, 111.0, 115.3]"; #2. Bass diffusion model: m market size, p innovation, q imitation; json load "rx_bass" (stat fit_model "m*(1-exp(-(p+q)*t))/(1+q/p*exp(-(p+q)*t))" "t" [t] [N] "{\"m\": 120, \"p\": 0.01, \"q\": 0.5}"); #3. The CAS confirms the saturation level: with s = p+q t and r = q/p, the limit is m; -> "[lim]" (cas limit "m*(1-exp(-s))/(1+r*exp(-s))" "s" "Infinity"); #4. Measures and fitted S curve; plot fit [t] [N] (json select "rx_bass" /expression) "t"
The chart is shown in the Plot tab of the editor.
- Read the parameters with
json select "rx_bass" /parameters(about m = 123, p = 0.018, q = 0.49). - The peak of the adoptions is at t = ln(q/p)/(p+q).
RFM segmentation of customers
The CRM team scores each customer on Recency, Frequency and Monetary value over 12 months. The script computes the days since the last order, the quartiles of each measure, a 1 to 4 score per measure (recency reversed) and a segment: champion, at risk, promising or lost.
#1. Customers of an online shop: last order, number of orders and amount spent over 12 months; json load "rx_cu" "[{\"id\": \"C01\", \"last\": \"2026-10-28\", \"f\": 14, \"m\": 1820}, {\"id\": \"C02\", \"last\": \"2026-04-02\", \"f\": 9, \"m\": 1150}, {\"id\": \"C03\", \"last\": \"2026-10-12\", \"f\": 2, \"m\": 95}, {\"id\": \"C04\", \"last\": \"2025-12-20\", \"f\": 1, \"m\": 40}, {\"id\": \"C05\", \"last\": \"2026-09-30\", \"f\": 6, \"m\": 610}, {\"id\": \"C06\", \"last\": \"2026-10-31\", \"f\": 11, \"m\": 2390}, {\"id\": \"C07\", \"last\": \"2026-06-15\", \"f\": 4, \"m\": 380}, {\"id\": \"C08\", \"last\": \"2026-08-03\", \"f\": 3, \"m\": 210}]"; json load "rx_v" "{\"r\": [], \"f\": [], \"m\": []}"; #2. Recency in days on 2026-11-01, then the quartiles of each measure; for (-> "[i]" 0) (< [i] (json count "rx_cu" /)) (++ "[i]") { json iarray "rx_v" "/r" (date nb_day (json select "rx_cu" (concat "/[" [i] "]/last")) "2026-11-01") NUM; json iarray "rx_v" "/f" (json select "rx_cu" (concat "/[" [i] "]/f")) NUM; json iarray "rx_v" "/m" (json select "rx_cu" (concat "/[" [i] "]/m")) NUM; }; json load "rx_q" (concat "{\"r\": " (stat quartiles (json select "rx_v" "/r")) ", \"f\": " (stat quartiles (json select "rx_v" "/f")) ", \"m\": " (stat quartiles (json select "rx_v" "/m")) "}"); #3. Score 1 to 4 per measure, recency is reversed, then the segment; -> "[seg]" ""; for (-> "[i]" 0) (< [i] (json count "rx_cu" /)) (++ "[i]") { -> "[sc]" ""; for (-> "[k]" 1) (<= [k] 3) (++ "[k]") { -> "[x]" (atom get "r,f,m" [k] ","); -> "[v]" (json select "rx_v" (concat "/" [x] "/[" [i] "]")); -> "[s]" 1; for (-> "[q]" 0) (< [q] 3) (++ "[q]") {if (> [v] (json select "rx_q" (concat "/" [x] "/[" [q] "]"))) {-> "[s]" (+ [s] 1);};}; if (equal [x] "r") {-> "[s]" (- 5 [s]);}; -> "[sc]" (concat [sc] [s]); }; -> "[R]" (string left [sc] 1); -> "[F]" (string mid [sc] 1 2); if (and (>= [R] 3) (>= [F] 3)) {-> "[name]" "champion";} {if (>= [F] 3) {-> "[name]" "at risk";} {if (>= [R] 3) {-> "[name]" "promising";} {-> "[name]" "lost";};};}; -> "[seg]" (concat [seg] (json select "rx_cu" (concat "/[" [i] "]/id")) "=" [sc] " " [name] ", "); }; -> "[out]" (concat "quartiles " (string regexp_replace (json doc "rx_q") "\s" "") " | " [seg] "median spend " (stat median (json select "rx_v" "/m")) ", mean spend " (stat mean (json select "rx_v" "/m"))); json unload "rx_cu"; json unload "rx_v"; json unload "rx_q"; [out]
quartiles {"r":[16,61,157.5],"f":[2.75,5,9.5],"m":[181.25,495,1317.5]} | C01=444 champion, C02=133 at risk, C03=311 promising, C04=111 lost, C05=333 champion, C06=444 champion, C07=222 lost, C08=222 lost, median spend 495, mean spend 836.875- Send a win-back offer to the at risk customers: they ordered often but not lately
- With more customers use
stat percentilefor quintiles, the usual RFM grid of 125 cells
Customer lifetime value with retention
The growth team values the 2025 cohort of a subscription box: average yearly margin per customer, 78 pct retention, 9 pct discount rate, 95 EUR acquisition cost. The script computes the infinite horizon CLV m.r/(1+d-r), the 5-year CLV as a discounted sum, the CLV / CAC ratio and the expected lifetime.
#1. Yearly gross margin per customer of the 2025 cohort of a subscription box, retention 78 pct, discount rate 9 pct, acquisition cost 95 EUR; -> "[m]" (math avg "[64.0, 81.5, 72.0, 58.5, 90.0, 69.0, 77.5]"); -> "[r]" 0.78; -> "[d]" 0.09; -> "[cac]" 95; #2. Infinite horizon CLV = m . r / 1 + d - r, margins paid at the end of each year of retention; -> "[clv]" (/ (* [m] [r]) (- (+ 1 [d]) [r])); #3. Same on a 5-year horizon with the sum of m . r^t / 1 + d ^t, and the expected lifetime; -> "[clv5]" 0; for (-> "[t]" 1) (<= [t] 5) (++ "[t]") {-> "[clv5]" (+ [clv5] (/ (* [m] (math power [r] [t])) (math power (+ 1 [d]) [t])));}; concat "average margin " (math round [m] 2) " EUR, CLV " (math round [clv] 2) " EUR, 5-year CLV " (math round [clv5] 2) " EUR, CLV / CAC " (math round (/ [clv] [cac]) 2) ", expected lifetime " (math round (/ 1 (- 1 [r])) 2) " years"
average margin 73.21 EUR, CLV 184.22 EUR, 5-year CLV 149.65 EUR, CLV / CAC 1.94, expected lifetime 4.55 years
- A CLV / CAC under 3 is weak: raise the retention or lower the acquisition cost
- One point of retention adds about 8 EUR of CLV here: try r = 0.79
Average basket after a product page redesign
After a new product page, the e-commerce manager compares a sample of 16 orders with the historical mean basket of 42.50 EUR (standard deviation 9 EUR, known from years of orders). The script runs a z-test, gives the uplift and its 95 pct interval and a decision.
#1. Order baskets in EUR of a sample of 16 orders after the new product page, the historical mean is 42.50 EUR with sd 9 EUR; -> "[bk]" "[48.9, 39.5, 51.2, 44.0, 56.3, 41.8, 47.5, 38.9, 52.6, 45.1, 49.7, 43.3, 55.0, 40.6, 46.8, 50.4]"; -> "[mu]" 42.50; -> "[sigma]" 9; #2. z-test of the mean basket against the historical value; json load "rx_z" (stat ztest [bk] [mu] [sigma]); #3. Uplift, its 95 pct margin 1.96 sigma / sqrt n and the decision; json load "rx_b" [bk]; -> "[mean]" (stat mean [bk]); -> "[marg]" (/ (* 1.96 [sigma]) (math sqrt (json count "rx_b" "/"))); -> "[out]" (concat "mean basket " (math round [mean] 2) " EUR, uplift " (math round (* (/ (- [mean] [mu]) [mu]) 100) 1) " pct, 95 pct interval " (math round (- [mean] [marg]) 2) " to " (math round (+ [mean] [marg]) 2) " | z " (math round (json select "rx_z" "/z") 3) ", p " (math round (json select "rx_z" "/p_value") 4) (if (< (json select "rx_z" "/p_value") 0.05) {" -> keep the new page";} {" -> not proven";})); json unload "rx_z"; json unload "rx_b"; [out]
mean basket 46.98 EUR, uplift 10.5 pct, 95 pct interval 42.57 to 51.39 | z 1.989, p 0.0467 -> keep the new page
- p = 0.047 is just under 5 pct: collect a second sample before the general roll-out
- For a real A/B test with two samples use
stat ttest_2orstat mann_whitney
Saturating media response and the optimal spend
The media planner models TV sales with a saturating curve sales = a.tanh(x/k). The script computes the sales and the marginal return at the current spend (derivative a/k / cosh^2(x/k)), then the spend where one more euro brings back only one euro, with the inverse hyperbolic cosine.
#1. Saturating response of TV sales to the weekly spend x in kEUR: sales = a . tanh of x / k, a = 120 kEUR max, k = 40 kEUR; -> "[a]" 120; -> "[k]" 40; -> "[x]" 30; #2. Sales at the current 30 kEUR spend and the marginal return a/k . 1 / cosh2 of x/k; -> "[s]" (* [a] (math tanh (/ [x] [k]))); -> "[mroi]" (/ (/ [a] [k]) (math power (math cosh (/ [x] [k])) 2)); #3. Spend where the marginal return falls to 1 EUR per EUR: cosh2 = a/k, so x = k . acosh of sqrt a/k; -> "[xopt]" (* [k] (math acosh (math sqrt (/ [a] [k])))); concat "sales at 30 kEUR " (math round [s] 2) " kEUR, marginal return " (math round [mroi] 3) " EUR per EUR, optimal spend " (math round [xopt] 2) " kEUR giving " (math round (* [a] (math tanh (/ [xopt] [k]))) 2) " kEUR of sales"
sales at 30 kEUR 76.22 kEUR, marginal return 1.79 EUR per EUR, optimal spend 45.85 kEUR giving 97.98 kEUR of sales
- Draw the curve 120*tanh(x/40) and the break-even line y = x with
plot functionfrom 0 to 150 kEUR - Fit a and k on past weeks with
stat fit_modeland the model a*tanh(x/k)
Best send time of a newsletter
The CRM manager looks at the open timestamps of the last newsletter to plan the next one. The script extracts the hour and the weekday of each open, finds the peak hour with the mode, its share in the frequency table and the best weekday with its name.
#1. Open timestamps of the last newsletter, local time; json load "rx_op" "[\"2026-10-20 07:42:10\", \"2026-10-20 12:15:33\", \"2026-10-20 12:48:02\", \"2026-10-20 18:05:47\", \"2026-10-20 12:31:20\", \"2026-10-21 08:12:55\", \"2026-10-21 12:05:09\", \"2026-10-21 21:40:18\", \"2026-10-20 18:22:41\", \"2026-10-22 12:57:30\", \"2026-10-20 07:58:03\", \"2026-10-21 18:44:26\", \"2026-10-22 12:20:14\", \"2026-10-20 20:10:37\", \"2026-10-21 12:39:51\"]"; json load "rx_h" "[]"; json load "rx_d" "[]"; #2. Hour and weekday of each open; json parse_array "rx_op" "/" "[ts]" { json iarray "rx_h" "/" (date hour (date time [ts])) INT; json iarray "rx_d" "/" (date day_of_week (string left [ts] 10)) INT; }; #3. Peak hour with the mode, its share with the frequency table, best weekday; -> "[peak]" (string regexp_replace (stat mode (json doc "rx_h")) "[^0-9]" ""); -> "[bd]" (string regexp_replace (stat mode (json doc "rx_d")) "[^0-9]" ""); json load "rx_f" (stat frequency (json doc "rx_h")); -> "[share]" 0; -> "[dname]" ""; json parse_array "rx_op" "/" "[ts]" {if (== (date day_of_week (string left [ts] 10)) [bd]) {-> "[dname]" (date dayname (string left [ts] 10) "en");};}; json parse_array "rx_f" "/" "[row]" {json load "rx_r" [row]; if (== (json select "rx_r" "/value") [peak]) {-> "[share]" (json select "rx_r" "/frequency");};}; -> "[out]" (concat "opens by hour " (string regexp_replace (json doc "rx_h") "\s" "") " | peak hour " [peak] " with " (math round (* [share] 100) 1) " pct of the opens | best weekday " [bd] " = " [dname] " | schedule the next send at 11:45"); json unload "rx_op"; json unload "rx_h"; json unload "rx_d"; json unload "rx_f"; json unload "rx_r"; [out]
opens by hour [7,12,12,18,12,8,12,21,18,12,7,18,12,20,12] | peak hour 12 with 46.7 pct of the opens | best weekday 2 = Tuesday | schedule the next send at 11:45
- Send 15 minutes before the peak so the mail is on top of the inbox at lunch time
- Repeat over several campaigns: one newsletter is a small sample
Media mix model through the origin
The marketing analyst estimates the incremental sales brought by 1 kEUR on search, social and radio from 8 weeks of data. With no spend there is no incremental sale, so the multiple regression has no intercept. The script fits it, reads the return of each channel and the R2, and forecasts the next media plan.
#1. Weekly incremental sales in kEUR and the spends in kEUR on search, social and radio of 8 weeks; -> "[X]" "[[4.0, 2.0, 0.0], [5.5, 1.0, 3.0], [3.0, 3.5, 2.0], [6.0, 2.5, 0.0], [2.5, 0.5, 4.0], [4.5, 3.0, 1.5], [5.0, 1.5, 2.5], [3.5, 2.0, 3.5]]"; -> "[y]" "[21.3, 27.1, 22.8, 27.9, 17.6, 26.0, 26.4, 23.9]"; #2. No spend means no incremental sales: regression through the origin; pa rm load_from_json "rx_mmm" [X] [y]; pa rm set_no_intercept "rx_mmm" true; json load "rx_b" (pa rm estimate_regression_parameters "rx_mmm"); #3. Return of 1 kEUR on each channel, fit quality and the forecast of the next media plan; -> "[out]" (concat "ROI search " (math round (json select "rx_b" "/[0]") 3) ", social " (math round (json select "rx_b" "/[1]") 3) ", radio " (math round (json select "rx_b" "/[2]") 3) " | R2 " (math round (pa rm calculate_r_squared "rx_mmm") 4) " | plan 5 / 3 / 2 kEUR gives " (math round (pa rm predict "rx_mmm" "[5, 3, 2]") 2) " kEUR of sales"); pa rm close "rx_mmm"; json unload "rx_b"; [out]
ROI search 3.687, social 2.478, radio 1.657 | R2 0.9992 | plan 5 / 3 / 2 kEUR gives 29.18 kEUR of sales
- Without intercept the R2 is computed around 0, not around the mean: it is always high, compare models with the residuals
- Add the adstock (carry-over of past weeks) to the spends before the regression
Baskets that bring the most margin
The most frequent baskets are not always the most profitable: compare the frequent itemsets with the high utility itemsets computed on the margin of each line, to choose the bundles to promote.
#1. Receipts with the margin of each line in EUR; -> "[m]" "[[[\"coffee\", 0.8], [\"milk\", 0.3], [\"sugar\", 0.2]], [[\"coffee\", 0.8], [\"milk\", 0.3]], [[\"wine\", 6.5], [\"cheese\", 3.2]], [[\"coffee\", 0.8], [\"sugar\", 0.2]], [[\"wine\", 6.5], [\"cheese\", 3.2], [\"bread\", 0.4]], [[\"coffee\", 0.8], [\"milk\", 0.3], [\"bread\", 0.4]], [[\"wine\", 13.0]], [[\"coffee\", 0.8], [\"milk\", 0.3], [\"sugar\", 0.2]]]"; #2. The same receipts without the margins, for the frequent itemsets; json load "rx_m" [m]; json load "rx_t" "[]"; for (-> "[i]" 0) (< [i] (json count "rx_m" /)) (++ "[i]") { json load "rx_r" "[]"; for (-> "[j]" 0) (< [j] (json count "rx_m" (concat "/[" [i] "]"))) (++ "[j]") {json iarray "rx_r" / (json select "rx_m" (concat "/[" [i] "]/[" [j] "]/[0]")) STR;}; json iarray "rx_t" / (json doc "rx_r") ARRAY; }; json load "rx_f" (ml assoc itemsets (json doc "rx_t") 0.3 "frequent"); json load "rx_u" (ml assoc utility [m] 10); -> "[res]" (concat "most frequent pair: " (json select "rx_f" "/itemsets/[1]/items/[0]") " + " (json select "rx_f" "/itemsets/[1]/items/[1]") " in " (json select "rx_f" "/itemsets/[1]/support") " receipts | most profitable set: " (json select "rx_u" "/itemsets/[0]/items/[0]") (if (> (json count "rx_u" "/itemsets/[0]/items") 1) {concat " + " (json select "rx_u" "/itemsets/[0]/items/[1]")} {""}) " = " (json select "rx_u" "/itemsets/[0]/utility") " EUR | sets above 10 EUR: " (json select "rx_u" "/count")); json unload "rx_m"; json unload "rx_t"; json unload "rx_r"; json unload "rx_f"; json unload "rx_u"; [res]
most frequent pair: coffee + milk in 4 receipts | most profitable set: wine = 26 EUR | sets above 10 EUR: 3
- Use the quantity instead of the margin to find the sets that move the most stock.
- Check the profitable sets with
ml assoc rules: a high lift confirms a real association.
Promotions and seasonal products
A promotion is a negative margin: the high utility itemsets with negative utilities (FHN) show the baskets that stay profitable with the promotion; the on-shelf utility (FOSHU) does not penalize a product sold only in summer.
#1. Baskets with the margin of each line, the promotion costs 3 EUR; -> "[b]" "[[[\"bread\", 1.5], [\"wine\", 12.25], [\"promo\", -3]], [[\"bread\", 1.5], [\"cheese\", 4.8], [\"wine\", 12.25], [\"promo\", -3]], [[\"cheese\", 4.8], [\"wine\", 12.25]], [[\"bread\", 1.5], [\"cheese\", 4.8], [\"promo\", -3]]]"; #2. The same baskets with the season, mango only in summer; -> "[s]" "[[\"winter\", [[\"bread\", 1.5], [\"wine\", 12.25]]], [\"winter\", [[\"bread\", 1.5], [\"cheese\", 4.8]]], [\"summer\", [[\"cheese\", 4.8], [\"mango\", 6], [\"wine\", 12.25]]], [\"summer\", [[\"mango\", 6], [\"wine\", 12.25]]]]"; json load "rx_n" (ml assoc utility [b] 15); json load "rx_f" (ml assoc utility_periods [s] 0.8); -> "[res]" (concat "with the promotion, " (json select "rx_n" "/count") " sets above 15 EUR, the best: " (json select "rx_n" "/itemsets/[0]/items/[0]") " | relative utility >= 0.8: "); for (-> "[i]" 0) (< [i] (json select "rx_f" "/count")) (++ "[i]") { -> "[p]" (concat "/itemsets/[" [i] "]"); -> "[a]" ""; for (-> "[k]" 0) (< [k] (json count "rx_f" (concat [p] "/items"))) (++ "[k]") {-> "[a]" (concat [a] (if (> [k] 0) {" + "} {""}) (json select "rx_f" (concat [p] "/items/[" [k] "]")));}; -> "[res]" (concat [res] [a] " " (math round (json select "rx_f" (concat [p] "/relative_utility")) 2) "; "); }; json unload "rx_n"; json unload "rx_f"; [res]
with the promotion, 7 sets above 15 EUR, the best: wine | relative utility >= 0.8: wine + mango 0.88;
ml assoc topk_utilitygives the k most profitable sets without a threshold.- The periods can be months, seasons or shops.
Segment the customers with RFM
From 10 months of orders, compute for each customer the recency, the frequency and the monetary value, the RFM scores (quintiles) and the segments to target: champions, loyal, at risk...
#1. Orders: customer, date, amount; -> "[o]" "[{\"c\": \"C01\", \"d\": \"2026-09-12\", \"a\": 150.8}, {\"c\": \"C01\", \"d\": \"2026-09-07\", \"a\": 188.57}, {\"c\": \"C01\", \"d\": \"2026-05-16\", \"a\": 213.88}, {\"c\": \"C01\", \"d\": \"2026-04-11\", \"a\": 291.91}, {\"c\": \"C02\", \"d\": \"2026-05-07\", \"a\": 65.1}, {\"c\": \"C02\", \"d\": \"2026-08-25\", \"a\": 170.45}, {\"c\": \"C03\", \"d\": \"2026-08-26\", \"a\": 111.29}, {\"c\": \"C03\", \"d\": \"2026-07-14\", \"a\": 78.31}, {\"c\": \"C03\", \"d\": \"2026-07-15\", \"a\": 105.2}, {\"c\": \"C03\", \"d\": \"2026-02-21\", \"a\": 250.55}, {\"c\": \"C04\", \"d\": \"2026-05-13\", \"a\": 239.05}, {\"c\": \"C04\", \"d\": \"2026-04-14\", \"a\": 41.51}, {\"c\": \"C04\", \"d\": \"2026-07-01\", \"a\": 264.11}, {\"c\": \"C04\", \"d\": \"2026-04-18\", \"a\": 46.52}, {\"c\": \"C04\", \"d\": \"2026-01-12\", \"a\": 217.29}, {\"c\": \"C04\", \"d\": \"2026-01-09\", \"a\": 178.03}, {\"c\": \"C05\", \"d\": \"2026-01-14\", \"a\": 70.64}, {\"c\": \"C05\", \"d\": \"2026-02-25\", \"a\": 62.21}, {\"c\": \"C05\", \"d\": \"2026-06-21\", \"a\": 179.34}, {\"c\": \"C06\", \"d\": \"2026-08-28\", \"a\": 265.25}, {\"c\": \"C06\", \"d\": \"2026-05-28\", \"a\": 263.6}, {\"c\": \"C06\", \"d\": \"2026-09-22\", \"a\": 264.45}, {\"c\": \"C07\", \"d\": \"2026-02-02\", \"a\": 181.19}, {\"c\": \"C07\", \"d\": \"2026-09-23\", \"a\": 265.2}, {\"c\": \"C08\", \"d\": \"2026-10-25\", \"a\": 189.52}, {\"c\": \"C08\", \"d\": \"2026-08-26\", \"a\": 198.53}, {\"c\": \"C09\", \"d\": \"2026-04-24\", \"a\": 281.22}, {\"c\": \"C09\", \"d\": \"2026-09-15\", \"a\": 293.03}, {\"c\": \"C09\", \"d\": \"2026-03-03\", \"a\": 287.99}, {\"c\": \"C10\", \"d\": \"2026-06-13\", \"a\": 38.91}, {\"c\": \"C10\", \"d\": \"2026-09-18\", \"a\": 212.98}, {\"c\": \"C10\", \"d\": \"2026-06-15\", \"a\": 36.1}, {\"c\": \"C10\", \"d\": \"2026-05-22\", \"a\": 250.86}, {\"c\": \"C10\", \"d\": \"2026-02-09\", \"a\": 282.43}, {\"c\": \"C11\", \"d\": \"2026-08-07\", \"a\": 138.99}, {\"c\": \"C11\", \"d\": \"2026-09-26\", \"a\": 272.29}, {\"c\": \"C11\", \"d\": \"2026-06-08\", \"a\": 46.77}, {\"c\": \"C11\", \"d\": \"2026-03-01\", \"a\": 23.96}, {\"c\": \"C11\", \"d\": \"2026-07-28\", \"a\": 191.64}, {\"c\": \"C12\", \"d\": \"2026-06-08\", \"a\": 241.44}, {\"c\": \"C13\", \"d\": \"2026-04-18\", \"a\": 186.07}, {\"c\": \"C13\", \"d\": \"2026-06-11\", \"a\": 25.19}, {\"c\": \"C13\", \"d\": \"2026-03-10\", \"a\": 116.75}, {\"c\": \"C13\", \"d\": \"2026-06-13\", \"a\": 132.22}, {\"c\": \"C13\", \"d\": \"2026-01-24\", \"a\": 81.71}, {\"c\": \"C13\", \"d\": \"2026-02-07\", \"a\": 61.48}, {\"c\": \"C13\", \"d\": \"2026-02-05\", \"a\": 115.9}, {\"c\": \"C13\", \"d\": \"2026-06-01\", \"a\": 145.27}, {\"c\": \"C14\", \"d\": \"2026-06-17\", \"a\": 205.6}, {\"c\": \"C14\", \"d\": \"2026-09-07\", \"a\": 55.71}, {\"c\": \"C14\", \"d\": \"2026-09-18\", \"a\": 109.14}, {\"c\": \"C14\", \"d\": \"2026-05-24\", \"a\": 91.56}, {\"c\": \"C15\", \"d\": \"2026-04-15\", \"a\": 292.77}, {\"c\": \"C15\", \"d\": \"2026-01-08\", \"a\": 33.89}, {\"c\": \"C15\", \"d\": \"2026-06-24\", \"a\": 266.08}, {\"c\": \"C15\", \"d\": \"2026-08-22\", \"a\": 34.45}, {\"c\": \"C15\", \"d\": \"2026-09-23\", \"a\": 77.25}, {\"c\": \"C15\", \"d\": \"2026-01-25\", \"a\": 271.96}, {\"c\": \"C15\", \"d\": \"2026-10-27\", \"a\": 36.31}, {\"c\": \"C16\", \"d\": \"2026-08-22\", \"a\": 96.76}, {\"c\": \"C16\", \"d\": \"2026-10-05\", \"a\": 156.47}, {\"c\": \"C16\", \"d\": \"2026-01-02\", \"a\": 213.79}, {\"c\": \"C16\", \"d\": \"2026-05-20\", \"a\": 198.72}, {\"c\": \"C16\", \"d\": \"2026-02-20\", \"a\": 243.46}, {\"c\": \"C16\", \"d\": \"2026-06-17\", \"a\": 224.41}, {\"c\": \"C16\", \"d\": \"2026-03-16\", \"a\": 219.82}, {\"c\": \"C17\", \"d\": \"2026-02-09\", \"a\": 21.79}, {\"c\": \"C17\", \"d\": \"2026-06-07\", \"a\": 200.39}, {\"c\": \"C17\", \"d\": \"2026-09-04\", \"a\": 294.79}, {\"c\": \"C17\", \"d\": \"2026-04-21\", \"a\": 64.26}, {\"c\": \"C17\", \"d\": \"2026-10-26\", \"a\": 206.14}, {\"c\": \"C17\", \"d\": \"2026-04-15\", \"a\": 58.35}, {\"c\": \"C17\", \"d\": \"2026-04-17\", \"a\": 283.73}, {\"c\": \"C18\", \"d\": \"2026-02-19\", \"a\": 167.7}, {\"c\": \"C18\", \"d\": \"2026-01-11\", \"a\": 272.46}, {\"c\": \"C18\", \"d\": \"2026-09-05\", \"a\": 181.53}, {\"c\": \"C18\", \"d\": \"2026-08-05\", \"a\": 50.71}, {\"c\": \"C19\", \"d\": \"2026-08-20\", \"a\": 179.8}, {\"c\": \"C20\", \"d\": \"2026-05-13\", \"a\": 231.09}, {\"c\": \"C20\", \"d\": \"2026-01-04\", \"a\": 100.71}]"; json load "rx_r" (analytics rfm [o] "c" "d" "a" "2026-11-01"); json load "rx_p" (analytics pareto [o] "c" "a"); -> "[res]" (concat (json select "rx_r" "/customers") " customers | champions " (json select "rx_r" "/segments/champions") ", loyal " (json select "rx_r" "/segments/loyal") ", at risk " (json select "rx_r" "/segments/at_risk") ", lost " (json select "rx_r" "/segments/lost") " | " (json select "rx_p" "/classes/A/items") " customers make " (math round (* 100 (json select "rx_p" "/classes/A/value_share")) 1) " % of the revenue"); json unload "rx_r"; json unload "rx_p"; [res]
20 customers | champions 4, loyal 1, at risk 2, lost 4 | 12 customers make 80.3 % of the revenue
- Send the champions a loyalty offer, the at_risk customers a reactivation campaign.
analytics cohortfollows the retention of the customers by month of first order.
Customer lifetime value from the transactions (BG/NBD and Gamma-Gamma)
The transactions of 70 customers over 12 months. The summary gives for each customer the number of repeat purchases, the recency and the age; BG/NBD estimates the purchase rates and the churn, Gamma-Gamma the spend, and together they give the expected margin of each customer over the next 26 weeks: the customers to retain first.
-> "[tr]" "[{\"customer\": \"k00\", \"date\": \"2025-07-23\", \"amount\": 34}, {\"customer\": \"k00\", \"date\": \"2025-08-03\", \"amount\": 37}, {\"customer\": \"k01\", \"date\": \"2025-08-22\", \"amount\": 44}, {\"customer\": \"k01\", \"date\": \"2025-09-02\", \"amount\": 36}, {\"customer\": \"k02\", \"date\": \"2025-07-14\", \"amount\": 27}, {\"customer\": \"k03\", \"date\": \"2025-08-16\", \"amount\": 68}, {\"customer\": \"k04\", \"date\": \"2025-08-21\", \"amount\": 36}, {\"customer\": \"k04\", \"date\": \"2025-08-24\", \"amount\": 101}, {\"customer\": \"k05\", \"date\": \"2025-07-10\", \"amount\": 43}, {\"customer\": \"k05\", \"date\": \"2025-08-14\", \"amount\": 35}, {\"customer\": \"k05\", \"date\": \"2025-08-30\", \"amount\": 32}, {\"customer\": \"k06\", \"date\": \"2025-08-03\", \"amount\": 27}, {\"customer\": \"k06\", \"date\": \"2025-08-17\", \"amount\": 27}, {\"customer\": \"k07\", \"date\": \"2025-08-27\", \"amount\": 57}, {\"customer\": \"k08\", \"date\": \"2025-08-19\", \"amount\": 19}, {\"customer\": \"k08\", \"date\": \"2025-08-29\", \"amount\": 30}, {\"customer\": \"k09\", \"date\": \"2025-08-22\", \"amount\": 37}, {\"customer\": \"k09\", \"date\": \"2025-08-29\", \"amount\": 41}, {\"customer\": \"k09\", \"date\": \"2025-09-28\", \"amount\": 24}, {\"customer\": \"k09\", \"date\": \"2025-10-15\", \"amount\": 32}, {\"customer\": \"k09\", \"date\": \"2025-11-21\", \"amount\": 38}, {\"customer\": \"k10\", \"date\": \"2025-07-14\", \"amount\": 46}, {\"customer\": \"k10\", \"date\": \"2025-07-25\", \"amount\": 41}, {\"customer\": \"k10\", \"date\": \"2025-08-16\", \"amount\": 67}, {\"customer\": \"k11\", \"date\": \"2025-07-11\", \"amount\": 34}, {\"customer\": \"k11\", \"date\": \"2025-07-13\", \"amount\": 35}, {\"customer\": \"k11\", \"date\": \"2025-08-11\", \"amount\": 25}, {\"customer\": \"k11\", \"date\": \"2025-08-26\", \"amount\": 26}, {\"customer\": \"k11\", \"date\": \"2025-10-18\", \"amount\": 27}, {\"customer\": \"k12\", \"date\": \"2025-08-22\", \"amount\": 14}, {\"customer\": \"k12\", \"date\": \"2025-09-07\", \"amount\": 27}, {\"customer\": \"k12\", \"date\": \"2025-09-14\", \"amount\": 35}, {\"customer\": \"k12\", \"date\": \"2025-10-18\", \"amount\": 38}, {\"customer\": \"k13\", \"date\": \"2025-07-26\", \"amount\": 22}, {\"customer\": \"k13\", \"date\": \"2025-08-11\", \"amount\": 16}, {\"customer\": \"k13\", \"date\": \"2025-08-17\", \"amount\": 31}, {\"customer\": \"k13\", \"date\": \"2025-09-03\", \"amount\": 42}, {\"customer\": \"k14\", \"date\": \"2025-08-19\", \"amount\": 39}, {\"customer\": \"k15\", \"date\": \"2025-08-18\", \"amount\": 19}, {\"customer\": \"k16\", \"date\": \"2025-07-05\", \"amount\": 22}, {\"customer\": \"k16\", \"date\": \"2025-07-11\", \"amount\": 33}, {\"customer\": \"k16\", \"date\": \"2026-01-26\", \"amount\": 33}, {\"customer\": \"k16\", \"date\": \"2026-04-06\", \"amount\": 37}, {\"customer\": \"k16\", \"date\": \"2026-04-12\", \"amount\": 40}, {\"customer\": \"k17\", \"date\": \"2025-08-29\", \"amount\": 21}, {\"customer\": \"k17\", \"date\": \"2025-08-30\", \"amount\": 21}, {\"customer\": \"k17\", \"date\": \"2025-09-10\", \"amount\": 31}, {\"customer\": \"k17\", \"date\": \"2025-09-16\", \"amount\": 28}, {\"customer\": \"k17\", \"date\": \"2025-09-20\", \"amount\": 25}, {\"customer\": \"k17\", \"date\": \"2025-10-25\", \"amount\": 24}, {\"customer\": \"k17\", \"date\": \"2025-10-26\", \"amount\": 11}, {\"customer\": \"k17\", \"date\": \"2025-10-27\", \"amount\": 31}, {\"customer\": \"k17\", \"date\": \"2025-11-30\", \"amount\": 32}, {\"customer\": \"k17\", \"date\": \"2026-01-18\", \"amount\": 28}, {\"customer\": \"k17\", \"date\": \"2026-03-06\", \"amount\": 25}, {\"customer\": \"k17\", \"date\": \"2026-04-22\", \"amount\": 21}, {\"customer\": \"k17\", \"date\": \"2026-06-12\", \"amount\": 20}, {\"customer\": \"k17\", \"date\": \"2026-06-22\", \"amount\": 20}, {\"customer\": \"k17\", \"date\": \"2026-06-26\", \"amount\": 33}, {\"customer\": \"k18\", \"date\": \"2025-07-13\", \"amount\": 48}, {\"customer\": \"k18\", \"date\": \"2025-08-05\", \"amount\": 36}, {\"customer\": \"k18\", \"date\": \"2025-08-23\", \"amount\": 42}, {\"customer\": \"k18\", \"date\": \"2025-09-08\", \"amount\": 51}, {\"customer\": \"k18\", \"date\": \"2025-11-07\", \"amount\": 57}, {\"customer\": \"k18\", \"date\": \"2025-11-21\", \"amount\": 43}, {\"customer\": \"k18\", \"date\": \"2025-11-29\", \"amount\": 58}, {\"customer\": \"k18\", \"date\": \"2025-12-10\", \"amount\": 34}, {\"customer\": \"k18\", \"date\": \"2026-01-31\", \"amount\": 36}, {\"customer\": \"k18\", \"date\": \"2026-03-03\", \"amount\": 39}, {\"customer\": \"k19\", \"date\": \"2025-08-21\", \"amount\": 69}, {\"customer\": \"k19\", \"date\": \"2025-09-11\", \"amount\": 62}, {\"customer\": \"k19\", \"date\": \"2025-10-30\", \"amount\": 49}, {\"customer\": \"k19\", \"date\": \"2025-12-04\", \"amount\": 39}, {\"customer\": \"k19\", \"date\": \"2025-12-12\", \"amount\": 59}, {\"customer\": \"k19\", \"date\": \"2025-12-28\", \"amount\": 86}, {\"customer\": \"k19\", \"date\": \"2025-12-30\", \"amount\": 61}, {\"customer\": \"k19\", \"date\": \"2026-03-06\", \"amount\": 48}, {\"customer\": \"k19\", \"date\": \"2026-03-10\", \"amount\": 63}, {\"customer\": \"k19\", \"date\": \"2026-05-20\", \"amount\": 63}, {\"customer\": \"k20\", \"date\": \"2025-08-19\", \"amount\": 65}, {\"customer\": \"k20\", \"date\": \"2025-11-15\", \"amount\": 63}, {\"customer\": \"k21\", \"date\": \"2025-08-29\", \"amount\": 36}, {\"customer\": \"k22\", \"date\": \"2025-08-09\", \"amount\": 88}, {\"customer\": \"k22\", \"date\": \"2025-09-01\", \"amount\": 68}, {\"customer\": \"k22\", \"date\": \"2025-10-05\", \"amount\": 61}, {\"customer\": \"k23\", \"date\": \"2025-08-15\", \"amount\": 54}, {\"customer\": \"k23\", \"date\": \"2025-08-22\", \"amount\": 46}, {\"customer\": \"k24\", \"date\": \"2025-07-07\", \"amount\": 30}, {\"customer\": \"k24\", \"date\": \"2025-08-08\", \"amount\": 30}, {\"customer\": \"k24\", \"date\": \"2025-09-30\", \"amount\": 23}, {\"customer\": \"k24\", \"date\": \"2025-10-14\", \"amount\": 21}, {\"customer\": \"k24\", \"date\": \"2025-11-07\", \"amount\": 17}, {\"customer\": \"k24\", \"date\": \"2026-01-29\", \"amount\": 24}, {\"customer\": \"k24\", \"date\": \"2026-03-19\", \"amount\": 24}, {\"customer\": \"k25\", \"date\": \"2025-08-09\", \"amount\": 21}, {\"customer\": \"k25\", \"date\": \"2025-12-03\", \"amount\": 33}, {\"customer\": \"k25\", \"date\": \"2026-05-15\", \"amount\": 23}, {\"customer\": \"k25\", \"date\": \"2026-06-16\", \"amount\": 21}, {\"customer\": \"k26\", \"date\": \"2025-07-25\", \"amount\": 74}, {\"customer\": \"k27\", \"date\": \"2025-07-19\", \"amount\": 46}, {\"customer\": \"k27\", \"date\": \"2025-07-23\", \"amount\": 80}, {\"customer\": \"k28\", \"date\": \"2025-08-26\", \"amount\": 36}, {\"customer\": \"k29\", \"date\": \"2025-07-05\", \"amount\": 32}, {\"customer\": \"k29\", \"date\": \"2025-07-14\", \"amount\": 50}, {\"customer\": \"k29\", \"date\": \"2025-07-16\", \"amount\": 18}, {\"customer\": \"k30\", \"date\": \"2025-07-15\", \"amount\": 42}, {\"customer\": \"k30\", \"date\": \"2025-09-06\", \"amount\": 46}, {\"customer\": \"k30\", \"date\": \"2025-09-20\", \"amount\": 46}, {\"customer\": \"k30\", \"date\": \"2025-09-23\", \"amount\": 42}, {\"customer\": \"k30\", \"date\": \"2025-10-03\", \"amount\": 48}, {\"customer\": \"k30\", \"date\": \"2026-01-16\", \"amount\": 22}, {\"customer\": \"k30\", \"date\": \"2026-01-30\", \"amount\": 76}, {\"customer\": \"k30\", \"date\": \"2026-02-22\", \"amount\": 31}, {\"customer\": \"k30\", \"date\": \"2026-03-15\", \"amount\": 29}, {\"customer\": \"k30\", \"date\": \"2026-04-05\", \"amount\": 43}, {\"customer\": \"k30\", \"date\": \"2026-04-10\", \"amount\": 28}, {\"customer\": \"k30\", \"date\": \"2026-05-07\", \"amount\": 34}, {\"customer\": \"k31\", \"date\": \"2025-07-26\", \"amount\": 81}, {\"customer\": \"k31\", \"date\": \"2025-10-05\", \"amount\": 46}, {\"customer\": \"k31\", \"date\": \"2025-11-21\", \"amount\": 65}, {\"customer\": \"k31\", \"date\": \"2025-11-27\", \"amount\": 95}, {\"customer\": \"k31\", \"date\": \"2025-12-01\", \"amount\": 118}, {\"customer\": \"k31\", \"date\": \"2025-12-14\", \"amount\": 109}, {\"customer\": \"k31\", \"date\": \"2026-01-19\", \"amount\": 92}, {\"customer\": \"k31\", \"date\": \"2026-01-25\", \"amount\": 64}, {\"customer\": \"k31\", \"date\": \"2026-03-08\", \"amount\": 37}, {\"customer\": \"k31\", \"date\": \"2026-04-12\", \"amount\": 51}, {\"customer\": \"k31\", \"date\": \"2026-05-10\", \"amount\": 64}, {\"customer\": \"k31\", \"date\": \"2026-06-05\", \"amount\": 90}, {\"customer\": \"k32\", \"date\": \"2025-08-19\", \"amount\": 55}, {\"customer\": \"k32\", \"date\": \"2025-09-20\", \"amount\": 56}, {\"customer\": \"k32\", \"date\": \"2025-11-25\", \"amount\": 63}, {\"customer\": \"k33\", \"date\": \"2025-08-05\", \"amount\": 28}, {\"customer\": \"k34\", \"date\": \"2025-07-15\", \"amount\": 42}, {\"customer\": \"k35\", \"date\": \"2025-07-21\", \"amount\": 31}, {\"customer\": \"k35\", \"date\": \"2025-08-19\", \"amount\": 41}, {\"customer\": \"k35\", \"date\": \"2025-08-31\", \"amount\": 35}, {\"customer\": \"k35\", \"date\": \"2025-09-21\", \"amount\": 51}, {\"customer\": \"k35\", \"date\": \"2025-09-25\", \"amount\": 29}, {\"customer\": \"k35\", \"date\": \"2025-10-16\", \"amount\": 39}, {\"customer\": \"k35\", \"date\": \"2025-10-17\", \"amount\": 35}, {\"customer\": \"k35\", \"date\": \"2025-10-31\", \"amount\": 15}, {\"customer\": \"k35\", \"date\": \"2025-11-01\", \"amount\": 33}, {\"customer\": \"k35\", \"date\": \"2025-11-02\", \"amount\": 42}, {\"customer\": \"k35\", \"date\": \"2025-11-05\", \"amount\": 24}, {\"customer\": \"k35\", \"date\": \"2025-11-21\", \"amount\": 50}, {\"customer\": \"k35\", \"date\": \"2025-12-18\", \"amount\": 24}, {\"customer\": \"k35\", \"date\": \"2025-12-21\", \"amount\": 28}, {\"customer\": \"k35\", \"date\": \"2026-01-22\", \"amount\": 50}, {\"customer\": \"k36\", \"date\": \"2025-08-02\", \"amount\": 30}, {\"customer\": \"k37\", \"date\": \"2025-08-21\", \"amount\": 65}, {\"customer\": \"k37\", \"date\": \"2025-09-17\", \"amount\": 105}, {\"customer\": \"k37\", \"date\": \"2025-10-04\", \"amount\": 108}, {\"customer\": \"k37\", \"date\": \"2025-10-10\", \"amount\": 101}, {\"customer\": \"k37\", \"date\": \"2025-10-12\", \"amount\": 83}, {\"customer\": \"k37\", \"date\": \"2026-01-25\", \"amount\": 64}, {\"customer\": \"k37\", \"date\": \"2026-02-26\", \"amount\": 77}, {\"customer\": \"k37\", \"date\": \"2026-05-20\", \"amount\": 64}, {\"customer\": \"k37\", \"date\": \"2026-06-12\", \"amount\": 114}, {\"customer\": \"k37\", \"date\": \"2026-06-16\", \"amount\": 90}, {\"customer\": \"k38\", \"date\": \"2025-07-24\", \"amount\": 67}, {\"customer\": \"k38\", \"date\": \"2025-08-18\", \"amount\": 29}, {\"customer\": \"k39\", \"date\": \"2025-07-08\", \"amount\": 24}, {\"customer\": \"k39\", \"date\": \"2026-02-25\", \"amount\": 52}, {\"customer\": \"k40\", \"date\": \"2025-07-27\", \"amount\": 26}, {\"customer\": \"k41\", \"date\": \"2025-07-10\", \"amount\": 79}, {\"customer\": \"k41\", \"date\": \"2025-07-25\", \"amount\": 48}, {\"customer\": \"k41\", \"date\": \"2025-08-06\", \"amount\": 54}, {\"customer\": \"k41\", \"date\": \"2025-08-08\", \"amount\": 70}, {\"customer\": \"k41\", \"date\": \"2025-08-14\", \"amount\": 83}, {\"customer\": \"k41\", \"date\": \"2025-08-29\", \"amount\": 57}, {\"customer\": \"k41\", \"date\": \"2025-09-09\", \"amount\": 41}, {\"customer\": \"k41\", \"date\": \"2025-09-11\", \"amount\": 49}, {\"customer\": \"k41\", \"date\": \"2025-09-19\", \"amount\": 93}, {\"customer\": \"k41\", \"date\": \"2025-10-15\", \"amount\": 67}, {\"customer\": \"k41\", \"date\": \"2025-10-16\", \"amount\": 49}, {\"customer\": \"k41\", \"date\": \"2025-10-23\", \"amount\": 84}, {\"customer\": \"k41\", \"date\": \"2025-11-07\", \"amount\": 55}, {\"customer\": \"k41\", \"date\": \"2025-11-10\", \"amount\": 35}, {\"customer\": \"k42\", \"date\": \"2025-07-31\", \"amount\": 40}, {\"customer\": \"k43\", \"date\": \"2025-08-27\", \"amount\": 53}, {\"customer\": \"k44\", \"date\": \"2025-07-10\", \"amount\": 23}, {\"customer\": \"k44\", \"date\": \"2025-07-19\", \"amount\": 16}, {\"customer\": \"k44\", \"date\": \"2025-08-14\", \"amount\": 19}, {\"customer\": \"k44\", \"date\": \"2025-08-26\", \"amount\": 16}, {\"customer\": \"k44\", \"date\": \"2025-08-29\", \"amount\": 17}, {\"customer\": \"k44\", \"date\": \"2025-11-15\", \"amount\": 24}, {\"customer\": \"k44\", \"date\": \"2025-11-20\", \"amount\": 22}, {\"customer\": \"k44\", \"date\": \"2025-11-25\", \"amount\": 26}, {\"customer\": \"k45\", \"date\": \"2025-08-26\", \"amount\": 39}, {\"customer\": \"k45\", \"date\": \"2025-09-01\", \"amount\": 51}, {\"customer\": \"k45\", \"date\": \"2025-09-19\", \"amount\": 24}, {\"customer\": \"k45\", \"date\": \"2025-10-28\", \"amount\": 44}, {\"customer\": \"k45\", \"date\": \"2025-11-06\", \"amount\": 32}, {\"customer\": \"k45\", \"date\": \"2025-12-12\", \"amount\": 23}, {\"customer\": \"k45\", \"date\": \"2026-01-14\", \"amount\": 37}, {\"customer\": \"k45\", \"date\": \"2026-02-17\", \"amount\": 33}, {\"customer\": \"k45\", \"date\": \"2026-04-28\", \"amount\": 39}, {\"customer\": \"k45\", \"date\": \"2026-05-30\", \"amount\": 45}, {\"customer\": \"k46\", \"date\": \"2025-08-20\", \"amount\": 62}, {\"customer\": \"k47\", \"date\": \"2025-07-22\", \"amount\": 30}, {\"customer\": \"k47\", \"date\": \"2025-09-12\", \"amount\": 17}, {\"customer\": \"k48\", \"date\": \"2025-07-09\", \"amount\": 49}, {\"customer\": \"k48\", \"date\": \"2025-07-14\", \"amount\": 52}, {\"customer\": \"k49\", \"date\": \"2025-08-13\", \"amount\": 41}, {\"customer\": \"k49\", \"date\": \"2025-08-15\", \"amount\": 84}, {\"customer\": \"k49\", \"date\": \"2025-08-27\", \"amount\": 80}, {\"customer\": \"k49\", \"date\": \"2025-09-02\", \"amount\": 48}, {\"customer\": \"k50\", \"date\": \"2025-08-06\", \"amount\": 40}, {\"customer\": \"k50\", \"date\": \"2025-08-17\", \"amount\": 68}, {\"customer\": \"k50\", \"date\": \"2025-08-25\", \"amount\": 54}, {\"customer\": \"k50\", \"date\": \"2025-08-28\", \"amount\": 23}, {\"customer\": \"k51\", \"date\": \"2025-07-17\", \"amount\": 67}, {\"customer\": \"k51\", \"date\": \"2025-07-20\", \"amount\": 58}, {\"customer\": \"k51\", \"date\": \"2025-08-01\", \"amount\": 29}, {\"customer\": \"k51\", \"date\": \"2025-08-04\", \"amount\": 69}, {\"customer\": \"k51\", \"date\": \"2025-08-23\", \"amount\": 86}, {\"customer\": \"k51\", \"date\": \"2025-08-28\", \"amount\": 49}, {\"customer\": \"k52\", \"date\": \"2025-07-27\", \"amount\": 54}, {\"customer\": \"k52\", \"date\": \"2025-10-04\", \"amount\": 50}, {\"customer\": \"k52\", \"date\": \"2025-11-15\", \"amount\": 73}, {\"customer\": \"k52\", \"date\": \"2025-11-29\", \"amount\": 33}, {\"customer\": \"k52\", \"date\": \"2025-12-06\", \"amount\": 76}, {\"customer\": \"k52\", \"date\": \"2025-12-31\", \"amount\": 84}, {\"customer\": \"k52\", \"date\": \"2026-01-26\", \"amount\": 44}, {\"customer\": \"k52\", \"date\": \"2026-03-12\", \"amount\": 49}, {\"customer\": \"k52\", \"date\": \"2026-06-22\", \"amount\": 94}, {\"customer\": \"k53\", \"date\": \"2025-07-19\", \"amount\": 75}, {\"customer\": \"k53\", \"date\": \"2025-07-21\", \"amount\": 83}, {\"customer\": \"k53\", \"date\": \"2025-07-22\", \"amount\": 116}, {\"customer\": \"k54\", \"date\": \"2025-07-01\", \"amount\": 59}, {\"customer\": \"k54\", \"date\": \"2025-07-11\", \"amount\": 20}, {\"customer\": \"k54\", \"date\": \"2025-07-20\", \"amount\": 57}, {\"customer\": \"k54\", \"date\": \"2025-08-04\", \"amount\": 50}, {\"customer\": \"k54\", \"date\": \"2025-08-05\", \"amount\": 79}, {\"customer\": \"k54\", \"date\": \"2025-08-29\", \"amount\": 38}, {\"customer\": \"k54\", \"date\": \"2025-09-04\", \"amount\": 57}, {\"customer\": \"k55\", \"date\": \"2025-08-02\", \"amount\": 44}, {\"customer\": \"k55\", \"date\": \"2025-08-08\", \"amount\": 42}, {\"customer\": \"k55\", \"date\": \"2026-01-09\", \"amount\": 78}, {\"customer\": \"k55\", \"date\": \"2026-01-24\", \"amount\": 71}, {\"customer\": \"k55\", \"date\": \"2026-02-16\", \"amount\": 71}, {\"customer\": \"k55\", \"date\": \"2026-03-01\", \"amount\": 61}, {\"customer\": \"k55\", \"date\": \"2026-03-16\", \"amount\": 32}, {\"customer\": \"k55\", \"date\": \"2026-03-29\", \"amount\": 56}, {\"customer\": \"k56\", \"date\": \"2025-07-05\", \"amount\": 18}, {\"customer\": \"k57\", \"date\": \"2025-07-29\", \"amount\": 23}, {\"customer\": \"k58\", \"date\": \"2025-07-10\", \"amount\": 59}, {\"customer\": \"k58\", \"date\": \"2025-07-14\", \"amount\": 148}, {\"customer\": \"k59\", \"date\": \"2025-08-29\", \"amount\": 34}, {\"customer\": \"k59\", \"date\": \"2025-11-01\", \"amount\": 40}, {\"customer\": \"k59\", \"date\": \"2026-02-11\", \"amount\": 43}, {\"customer\": \"k59\", \"date\": \"2026-04-10\", \"amount\": 56}, {\"customer\": \"k59\", \"date\": \"2026-04-18\", \"amount\": 34}, {\"customer\": \"k59\", \"date\": \"2026-05-11\", \"amount\": 35}, {\"customer\": \"k60\", \"date\": \"2025-07-27\", \"amount\": 46}, {\"customer\": \"k60\", \"date\": \"2026-06-15\", \"amount\": 41}, {\"customer\": \"k61\", \"date\": \"2025-08-17\", \"amount\": 57}, {\"customer\": \"k62\", \"date\": \"2025-07-19\", \"amount\": 33}, {\"customer\": \"k62\", \"date\": \"2025-12-17\", \"amount\": 24}, {\"customer\": \"k63\", \"date\": \"2025-08-01\", \"amount\": 54}, {\"customer\": \"k64\", \"date\": \"2025-07-13\", \"amount\": 52}, {\"customer\": \"k64\", \"date\": \"2025-07-17\", \"amount\": 77}, {\"customer\": \"k64\", \"date\": \"2025-07-27\", \"amount\": 61}, {\"customer\": \"k64\", \"date\": \"2025-08-26\", \"amount\": 76}, {\"customer\": \"k64\", \"date\": \"2025-09-08\", \"amount\": 110}, {\"customer\": \"k64\", \"date\": \"2025-09-23\", \"amount\": 75}, {\"customer\": \"k64\", \"date\": \"2025-11-01\", \"amount\": 84}, {\"customer\": \"k64\", \"date\": \"2025-11-14\", \"amount\": 46}, {\"customer\": \"k64\", \"date\": \"2025-11-27\", \"amount\": 83}, {\"customer\": \"k64\", \"date\": \"2025-12-22\", \"amount\": 73}, {\"customer\": \"k64\", \"date\": \"2026-01-07\", \"amount\": 66}, {\"customer\": \"k64\", \"date\": \"2026-01-15\", \"amount\": 65}, {\"customer\": \"k64\", \"date\": \"2026-01-23\", \"amount\": 82}, {\"customer\": \"k64\", \"date\": \"2026-02-05\", \"amount\": 60}, {\"customer\": \"k64\", \"date\": \"2026-02-10\", \"amount\": 68}, {\"customer\": \"k64\", \"date\": \"2026-02-20\", \"amount\": 105}, {\"customer\": \"k64\", \"date\": \"2026-03-27\", \"amount\": 80}, {\"customer\": \"k64\", \"date\": \"2026-05-27\", \"amount\": 54}, {\"customer\": \"k64\", \"date\": \"2026-06-07\", \"amount\": 67}, {\"customer\": \"k64\", \"date\": \"2026-06-15\", \"amount\": 108}, {\"customer\": \"k65\", \"date\": \"2025-07-15\", \"amount\": 29}, {\"customer\": \"k65\", \"date\": \"2025-08-04\", \"amount\": 75}, {\"customer\": \"k65\", \"date\": \"2025-08-14\", \"amount\": 25}, {\"customer\": \"k65\", \"date\": \"2025-08-26\", \"amount\": 35}, {\"customer\": \"k65\", \"date\": \"2025-09-05\", \"amount\": 35}, {\"customer\": \"k65\", \"date\": \"2025-09-10\", \"amount\": 60}, {\"customer\": \"k65\", \"date\": \"2025-10-20\", \"amount\": 42}, {\"customer\": \"k65\", \"date\": \"2025-11-28\", \"amount\": 40}, {\"customer\": \"k65\", \"date\": \"2025-12-08\", \"amount\": 30}, {\"customer\": \"k65\", \"date\": \"2025-12-10\", \"amount\": 36}, {\"customer\": \"k65\", \"date\": \"2025-12-24\", \"amount\": 74}, {\"customer\": \"k65\", \"date\": \"2026-01-16\", \"amount\": 49}, {\"customer\": \"k65\", \"date\": \"2026-01-20\", \"amount\": 46}, {\"customer\": \"k65\", \"date\": \"2026-01-31\", \"amount\": 47}, {\"customer\": \"k65\", \"date\": \"2026-03-29\", \"amount\": 54}, {\"customer\": \"k65\", \"date\": \"2026-04-11\", \"amount\": 36}, {\"customer\": \"k65\", \"date\": \"2026-04-13\", \"amount\": 37}, {\"customer\": \"k65\", \"date\": \"2026-05-18\", \"amount\": 34}, {\"customer\": \"k65\", \"date\": \"2026-05-21\", \"amount\": 23}, {\"customer\": \"k65\", \"date\": \"2026-06-11\", \"amount\": 37}, {\"customer\": \"k66\", \"date\": \"2025-08-15\", \"amount\": 10}, {\"customer\": \"k66\", \"date\": \"2025-09-13\", \"amount\": 19}, {\"customer\": \"k66\", \"date\": \"2025-09-15\", \"amount\": 22}, {\"customer\": \"k66\", \"date\": \"2025-10-12\", \"amount\": 21}, {\"customer\": \"k67\", \"date\": \"2025-07-10\", \"amount\": 35}, {\"customer\": \"k68\", \"date\": \"2025-08-05\", \"amount\": 97}, {\"customer\": \"k68\", \"date\": \"2025-08-31\", \"amount\": 56}, {\"customer\": \"k68\", \"date\": \"2025-09-06\", \"amount\": 48}, {\"customer\": \"k68\", \"date\": \"2025-12-20\", \"amount\": 47}, {\"customer\": \"k68\", \"date\": \"2026-04-17\", \"amount\": 48}, {\"customer\": \"k68\", \"date\": \"2026-05-15\", \"amount\": 47}, {\"customer\": \"k68\", \"date\": \"2026-05-30\", \"amount\": 51}, {\"customer\": \"k68\", \"date\": \"2026-06-07\", \"amount\": 47}, {\"customer\": \"k68\", \"date\": \"2026-06-22\", \"amount\": 25}, {\"customer\": \"k69\", \"date\": \"2025-08-10\", \"amount\": 36}]"; -> "[cust]" (mkt summary [tr] "{\"end\": \"2026-06-30\"}"); json load "rx_v" (mkt clv [cust] "{\"horizon\": 26, \"margin\": 0.35, \"top\": 3}"); -> "[res]" (concat (json select "rx_v" "/customers") " customers | BG/NBD r " (math round (json select "rx_v" "/bgnbd/r") 3) ", alpha " (math round (json select "rx_v" "/bgnbd/alpha") 2) ", a " (math round (json select "rx_v" "/bgnbd/a") 3) ", b " (math round (json select "rx_v" "/bgnbd/b") 3) " | margin expected over 26 weeks: " (math round (json select "rx_v" "/total_clv") 0) " | best customer " (json select "rx_v" "/top/[0]/customer") ": P(alive) " (math round (json select "rx_v" "/top/[0]/probability_alive") 3) ", " (math round (json select "rx_v" "/top/[0]/expected_purchases") 1) " purchases of " (math round (json select "rx_v" "/top/[0]/expected_spend") 1) " -> " (math round (json select "rx_v" "/top/[0]/clv") 1)); json unload "rx_v"; [res]
70 customers | BG/NBD r 0.404, alpha 1.6, a 0.763, b 2.471 | margin expected over 26 weeks: 1171 | best customer k64: P(alive) 0.922, 7.7 purchases of 75.1 -> 202.9
- The probability alive ranks the customers to win back (many past purchases, long silence).
- analytics rfm gives the classical RFM segments of the same transactions.
Multi-touch attribution: last click against Markov and Shapley
The journeys of the visitors, with the channels seen before the conversion (or not). The last click gives everything to the final channel; the Markov chain measures what is lost when a channel is removed, and the Shapley values share the conversions between the channels that worked together: the budget follows the real contribution.
json load "rx_a" (mkt attribution "[{\"path\": [\"seo\", \"email\"], \"conversions\": 30, \"non_conversions\": 70}, {\"path\": \"ads > seo\", \"conversions\": 20, \"non_conversions\": 180}, {\"path\": [\"email\"], \"conversions\": 25, \"non_conversions\": 75}, {\"path\": [\"ads\"], \"conversions\": 5, \"non_conversions\": 295}, {\"path\": [\"social\", \"ads\", \"email\"], \"conversions\": 12, \"non_conversions\": 88}, {\"path\": [\"social\", \"seo\"], \"conversions\": 8, \"non_conversions\": 92}]"); -> "[res]" (concat (json select "rx_a" "/conversions") " conversions"); json load "rx_c" (json select "rx_a" "/channels"); for (-> "[i]" 0) (< [i] (json count "rx_c" "/")) (++ "[i]") { -> "[c]" (json select "rx_c" (concat "/[" [i] "]")); -> "[res]" (concat [res] " | " [c] ": last " (math round (json select "rx_a" (concat "/credits/last/" [c])) 1) ", Markov " (math round (json select "rx_a" (concat "/credits/markov/" [c])) 1) ", Shapley " (math round (json select "rx_a" (concat "/credits/shapley/" [c])) 1)); }; json unload "rx_a"; json unload "rx_c"; [res]
100 conversions | ads: last 5, Markov 27.5, Shapley 19 | email: last 67, Markov 35, Shapley 44 | seo: last 28, Markov 26.3, Shapley 29 | social: last 0, Markov 11.2, Shapley 8
- Give the value (revenue) of the conversions in each journey to attribute the revenue instead of the counts.
- mkt uplift measures the real incremental effect of a channel with a control group.
Clean the addresses of a newsletter before sending
Addresses typed in a form have typos, and some domains do not receive mail at all. Sending to them hurts the reputation of the sender. Each address is checked: syntax (RFC 5321 and 5322, UTF-8 and IDN domains), then the mail servers of the domain (MX), the null MX of a domain that refuses mail, or the A record when there is no MX. No message is sent: the mailbox itself is not checked. The DNS answers are given in records so that the recipe runs alone.
-> "[dns]" "{\"records\": {\"example.com\": {\"MX\": [\"10 mx1.example.com\", \"20 mx2.example.com\"]}, \"noreply.example.org\": {\"MX\": [\"0 .\"]}, \"small-shop.example\": {\"A\": [\"192.0.2.8\"]}}}"; json load "rec_1" (mail address_check "Jo Martin <jo.martin@example.com>" [dns]); json load "rec_2" (mail address_check "info@noreply.example.org" [dns]); json load "rec_3" (mail address_check "contact@small-shop.example" [dns]); json load "rec_4" (mail address_check "ann..smith@example.com" [dns]); json load "rec_5" (mail address_check "sales@exmaple.com" [dns]); -> "[res]" (concat "jo: " (json select "rec_1" "/deliverable") " | noreply: " (json select "rec_2" "/deliverable") " | small shop: " (json select "rec_3" "/deliverable") " (" (json select "rec_3" "/fallback") ") | two dots: " (json select "rec_4" "/valid") " | typo in the domain: " (json select "rec_5" "/reason")); json unload "rec_1"; json unload "rec_2"; json unload "rec_3"; json unload "rec_4"; json unload "rec_5"; [res]
jo: true | noreply: false | small shop: true (A) | two dots: false | typo in the domain: the domain does not exist
- An address can pass all these checks and still not exist: only a confirmation mail (double opt-in) proves it.
- There is no list of disposable domains in MentDB; keep your own list in a table if you need one.
