← Back to case study

PROJECT 009 · PYTHON SOURCE

Within-product pricing consistency

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': 9, 'dataset': 'retail', 'title': 'Within-product pricing consistency', 'question': 'Which frequently sold codes exhibit the widest middle-range price differences?', 'method': 'Compare 10th and 90th percentile positive unit prices, with at least 100 lines per code.', 'action': 'Review discounts, pack sizes and charges before labeling any price variation erroneous.', '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()
    t=df[df.is_sale].groupby('stockcode').unitprice.agg(lines='size',p10=lambda s:s.quantile(.1),median='median',p90=lambda s:s.quantile(.9)).reset_index()
    t=t[t.lines>=100].copy();t['p90_to_p10']=t.p90/t.p10
    t=t.sort_values('p90_to_p10',ascending=False)
    out=result(t,'stockcode','p90_to_p10','Price ratio','Each line has equal weight. This is a pricing-review screen, not a fraud detector.')
    return out

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