← Back to case study

PROJECT 007 · PYTHON SOURCE

Observed reorder intervals

Study-specific code. Shared modules, dependency versions, and reproduction instructions are included in all project files.

Download Python file ↓
from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
import numpy as np
import pandas as pd
from scipy.stats import spearmanr
from sklearn.decomposition import PCA
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, precision_score, recall_score
from portfolio.data import load
from portfolio.constants import *
from portfolio.methods import result, sql, rates, associations, distribution, regression, classification, cluster
from portfolio.engine import execute

META = {'id': 7, 'dataset': 'retail', 'title': 'Observed reorder intervals', 'question': 'What is the distribution of elapsed time between identified customers’ purchases?', 'method': 'Distinct invoices, within-customer chronological differences and interval buckets.', 'action': 'Use purchase cadence to propose a reminder experiment with a fixed observation window.', 'limitations': 'Historical invoice lines; credits are not reliably matched to original sales. Gross purchases are not profit. Unidentified customers cannot support customer-level conclusions. Exact repeated lines remain unless the study explicitly compares removal. '}

def analyze():
    df = load('retail').copy()
    orders=df[df.is_sale & df.customerid.notna()].groupby(['customerid','invoiceno']).invoicedate.min().reset_index().sort_values(['customerid','invoicedate'])
    orders['gap_days']=orders.groupby('customerid').invoicedate.diff().dt.total_seconds()/86400
    g=orders.gap_days.dropna()
    t=distribution(g,'days')
    out=result(t,'statistic','days','Days','Only repeat purchasers contribute observed intervals. Right-censored intervals after the final purchase are absent.',extra={'interval_counts':pd.DataFrame({'bucket':pd.cut(g,[-1,1,7,30,90,np.inf]).value_counts(sort=False).index.astype(str),'intervals':pd.cut(g,[-1,1,7,30,90,np.inf]).value_counts(sort=False).values})})
    return out

if __name__ == "__main__":
    execute(Path(__file__).parent, META, analyze)