# 004. Market basket associations

**Question:** Which frequently purchased product pairs co-occur more than their individual popularity suggests?

## Result

The largest computed metric is 7.994 for 20728 + 22384; the smallest is 0.7298 for 85099B + POST. Metric: lift (Lift).

Product search is restricted to 30 popular codes; require at least 50 co-purchase invoices. The denominator includes all positive-sales invoices.

![Market basket associations](outputs/chart.png)

| pair | co_invoices | all_sale_invoices | support | confidence_a_to_b | lift |
| --- | --- | --- | --- | --- | --- |
| 20728 + 22384 | 502 | 19960 | 0.02515 | 0.4365 | 7.994 |
| 20727 + 22384 | 544 | 19960 | 0.02725 | 0.4273 | 7.825 |
| 21931 + 22411 | 524 | 19960 | 0.02625 | 0.4426 | 7.518 |
| 23203 + 23209 | 504 | 19960 | 0.02525 | 0.4094 | 7.443 |
| 20728 + 22383 | 539 | 19960 | 0.027 | 0.4687 | 7.286 |
| 20728 + 22382 | 483 | 19960 | 0.0242 | 0.42 | 7.246 |
| 22960 + 22961 | 472 | 19960 | 0.02365 | 0.417 | 7.162 |
| 22382 + 22383 | 533 | 19960 | 0.0267 | 0.4607 | 7.161 |
| 20727 + 22383 | 584 | 19960 | 0.02926 | 0.4588 | 7.131 |
| 21931 + 22386 | 515 | 19960 | 0.0258 | 0.435 | 7.128 |
| 20727 + 20728 | 522 | 19960 | 0.02615 | 0.4101 | 7.117 |
| 20725 + 22384 | 606 | 19960 | 0.03036 | 0.3872 | 7.091 |
| 20727 + 22382 | 512 | 19960 | 0.02565 | 0.4022 | 6.939 |
| 22382 + 22384 | 428 | 19960 | 0.02144 | 0.3699 | 6.774 |

The chart shows 14 of 417 result rows; the table previews the first 14 in the analysis-defined order. [Download the full result table](outputs/results.csv). Numerical values are computed from the source; missing results stay unavailable.

## Method

Invoice presence matrix and pair support, confidence and lift among the 30 most frequent codes.

The study uses shared source preparation and reusable statistical routines. Its specific transformations are in [analysis.py](analysis.py), and common model/evaluation code is in [portfolio/methods.py](../../portfolio/methods.py). The [notebook](analysis.ipynb) executes the study and displays the saved results.

## Decision and limitations

Use a small merchandising experiment to test a selected pair; co-purchase alone does not establish an uplift.

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. 

Related studies may share observations or holdouts. These are focused analytical studies, not independent replications or deployed business systems. Any model refinements informed by these results need new untouched evaluation data. No commercial impact is inferred from an association or backtest.

## Reproduce

From the repository root, after installing `requirements.txt`:

```powershell
python projects/004-market-basket-associations/analysis.py
```

Source data are downloaded automatically if absent. Original archives are retained unchanged and checked by SHA-256. The cleaned cache normalizes column names; field-specific changes are visible in [data preparation](../../portfolio/data.py). Runtime evidence is in [receipt.json](outputs/receipt.json).

## Source

[Online Retail](https://archive.ics.uci.edu/dataset/352/online+retail), Chen (2015). [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/). Source data are transformed and aggregated in this study. [Source provenance](../../data/provenance/retail.json) and [prepared-data audit](../../data/provenance/retail_prepared.json) record the downloaded files, field coverage and hashes.
