Metadata-Version: 2.4
Name: causaldemand
Version: 1.0.0
Summary: CausalDemand: synthetic retail sales datasets for testing whether a method predicts how sales respond to price changes. Download, scoring and regeneration tools.
Author: Juwon Hong, Minha Hwang, Venkatesh Shankar
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/jean-jsj/CausalDemand
Project-URL: Repository, https://github.com/jean-jsj/CausalDemand
Project-URL: Dataset, https://huggingface.co/datasets/jean-jsj/CausalDemand
Keywords: benchmark,causal inference,counterfactual prediction,demand estimation,price elasticity,retail,synthetic data
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Requires-Dist: pandas>=2.0
Requires-Dist: scipy>=1.13
Requires-Dist: pyarrow>=14
Requires-Dist: PyYAML>=6.0
Requires-Dist: huggingface_hub>=0.23
Provides-Extra: generate
Requires-Dist: numpy==1.24.3; python_version < "3.11" and extra == "generate"
Requires-Dist: pandas==2.3.3; python_version < "3.11" and extra == "generate"
Requires-Dist: scipy==1.13.1; python_version < "3.11" and extra == "generate"
Requires-Dist: statsmodels==0.14.6; python_version < "3.11" and extra == "generate"
Requires-Dist: pyarrow==21.0.0; python_version < "3.11" and extra == "generate"
Requires-Dist: PyYAML==6.0.3; python_version < "3.11" and extra == "generate"
Requires-Dist: scikit-learn==1.6.1; python_version < "3.11" and extra == "generate"
Requires-Dist: matplotlib==3.9.4; python_version < "3.11" and extra == "generate"
Requires-Dist: xlrd==2.0.2; python_version < "3.11" and extra == "generate"
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Dynamic: license-file

# CausalDemand

CausalDemand is a benchmark for methods that predict how retail sales respond to a change in price. It tests whether
a method that forecasts sales well also gets the effect of price right when prices are confounded: in half of the
datasets, stores deepen discounts when an unobserved demand shock is high, so deep discounts and high sales coincide
for a reason other than the discount, and a model that learns the price effect from this co-movement overstates it. A
held-out forecast cannot reveal the error, because the held-out weeks follow the same pricing. The generator records
every random quantity it draws, so it computes what sales would have been at prices that were never charged: in closed
form for the log-linear model, and for the discrete-choice model by averaging over 10,000 simulated shoppers per store
and week, the same shoppers at the actual and at the changed price.

The datasets are synthetic weekly sales of 40 facial-tissue products in 731 stores over 156 weeks, from a generator
calibrated on licensed store sales records. 20 further datasets repeat the design for a second product category,
yogurt.

| | |
|---|---|
| Datasets | Hugging Face: <https://huggingface.co/datasets/jean-jsj/CausalDemand> (DOI [10.57967/hf/10744](https://doi.org/10.57967/hf/10744)) |
| Code | this repository, release [v1.0](https://github.com/jean-jsj/CausalDemand/releases/tag/v1.0) |
| Paper | "CausalDemand: Benchmarking Causal Recovery of Demand from Retail Transactions and Product Text" (KDD 2027 Datasets and Benchmarks Track, submitted) |

## Datasets

Each dataset is one **configuration**, a demand model with confounding off or on, generated with one of five seeds:
2 demand models x 2 confounding settings x 5 seeds = 20 datasets, named `<configuration>_seed<NNN>`.

| Configuration | Demand model | Confounding | Seeds |
|---|---|---|---|
| `loglinear_confounding_off` | log-linear | off | 1, 10, 20, 30, 40 |
| `loglinear_confounding_on` | log-linear | on | 1, 10, 20, 30, 40 |
| `discretechoice_confounding_off` | discrete-choice | off | 1, 10, 20, 30, 40 |
| `discretechoice_confounding_on` | discrete-choice | on | 1, 10, 20, 30, 40 |

- **Log-linear model:** expected weekly units are a log-linear function of the product's regular price and discount,
  the prices and discounts of the other products (more strongly for products that are closer in a product distance
  computed from the product descriptions), and demand terms for product, brand, store, region and week.
- **Discrete-choice model:** in each store and week, simulated households decide whether to buy and which stocked
  product to buy; their tastes are correlated more strongly across closer products.
- **Confounding on:** the discount of a promoted week responds to the unobserved demand shock (loading 0.30).
  **Confounding off:** it does not. The two datasets of a demand model and seed are generated from the same random
  draws and differ in the discounts of promoted weeks and in what follows from them.

20 further **dose-response datasets** repeat the confounding-on configurations at confounding strengths 0.15 and 0.45.

20 **yogurt datasets** repeat the four configurations and five seeds for a second product category: 100 yogurt
products in 721 stores over the same 156 weeks, generated by the same generator and calibrated on yogurt sales
records. Beyond that, they differ from the facial-tissue datasets in four main ways: the confounding strength with
confounding on is 0.42 (log-linear) and 0.36 (discrete-choice); the product distance is estimated from the products
between which households switch, not from product descriptions; the product table holds the pack size and catalogue
attributes instead of a description; and each market's seasonal pattern follows its own yogurt sales instead of
influenza activity. They are named `yogurt_<configuration>_seed<NNN>` and lie in `yogurt/` on Hugging Face, outside
the dataset subsets, with the predictions and run records of their reference-model runs in `runs/yogurt/`.
[`docs/DATA.md`](docs/DATA.md) describes every file and column (yogurt: Section 11).

## Install

In a copy of this repository (Python 3.9 to 3.12 and 3.14 are tested):

```
pip install .
```

From PyPI: `pip install causaldemand`; from GitHub: `pip install git+https://github.com/jean-jsj/CausalDemand`. Either installs the `causaldemand` command and the scorer.
Regenerating datasets and running the reference models need their own environments ([`locks/README.md`](locks/README.md)).

## Quickstart

Run the commands from the repository root. Every command that reads released files downloads them from Hugging
Face, or reads a local copy of the release with `--source <folder>`. An output folder that a command creates
(`causaldemand_data/`, `scores/` and so on) holds a `.gitignore` file, so git ignores it.

1. **Download a dataset** with its ground truth (776 MB of CSV):

   ```
   causaldemand download loglinear_confounding_on_seed001 --ground-truth
   ```

   This writes `causaldemand_data/loglinear_confounding_on_seed001/`: `public/` holds the files a method may use
   (training sales, the prices of the 16 evaluation weeks, the 32 counterfactual scenarios, products and stores),
   `ground_truth/` the expected units the scores compare with.

2. **Predict.** Fit a method on the 140 training weeks and write two files from the same fitted model:
   `forecast.csv` (`product_id, store_id, week, predicted_units`, for every row of
   `public/transactions_holdout_context_public.csv`) and `scenarios.csv` (`intervention_id, product_id, store_id,
   week, predicted_delta_units`, the predicted change in units under each scenario, for every row of
   `public/counterfactual_sweep_context_panel.csv` whose `intervention_price` differs from its `baseline_price`).
   [`notebooks/quickstart.ipynb`](notebooks/quickstart.ipynb) writes both files for a trivial method in a few lines,
   into `notebooks/quickstart/` (see [Notebooks](#notebooks) for how to run it).

3. **Score** the two files, here those of the notebook:

   ```
   causaldemand score --dataset causaldemand_data/loglinear_confounding_on_seed001 --forecast notebooks/quickstart/forecast.csv --scenarios notebooks/quickstart/scenarios.csv --out scores
   ```

   For the trivial method of the notebook (each pair's mean weekly training units, no response to any price change):
   forecast error 0.4625, counterfactual error 0.1384, mean absolute counterfactual bias 0.9934, share of scenarios
   with an overstated response 0, status valid.

`causaldemand --help` lists every command (download, score, elasticity, rescore, generate).

## Notebooks

- [`notebooks/quickstart.ipynb`](notebooks/quickstart.ipynb) runs steps 1 to 3 of the quickstart for a trivial
  method.
- [`notebooks/full_run.ipynb`](notebooks/full_run.ipynb) runs a reference model of the paper (LightGBM) on the same
  dataset, compares its scores with the released run, and rebuilds the default-settings table from the released
  predictions. It runs the model with the CPU-model environment: create it as `.venv-models` in the repository root
  ([`locks/README.md`](locks/README.md); on macOS, LightGBM also needs the OpenMP runtime `libomp`), or set the
  environment variable `CAUSALDEMAND_MODEL_PYTHON` to the Python of that environment.

Both read the dataset from `causaldemand_data/` (step 1), downloading it there if it is missing, and write their
output into `notebooks/quickstart/` and `notebooks/full_run/`. To read a local copy of the release instead of Hugging
Face, set the environment variable `CAUSALDEMAND_SOURCE` to its folder. The package does not install Jupyter; to run
a notebook from the command line, in the package environment:

```
pip install nbconvert ipykernel
jupyter nbconvert --to notebook --execute --output-dir notebooks/executed notebooks/quickstart.ipynb
```

This writes the executed copy to `notebooks/executed/`. The quickstart takes about 15 seconds plus the download,
`full_run.ipynb` about 2 minutes.

## Reproduce the paper

From the repository root:

```
causaldemand rescore --out rescored --compare-with results
```

This re-scores the released predictions of 560 reference-model runs on the facial-tissue datasets (280 CPU runs and
280 foundation-model runs) and compares every score with the archived one in `results/` (about 3 minutes, plus a
download of about 1.8 GB). The 320 runs on the yogurt datasets (CPU and foundation models) are re-scored with
`causaldemand rescore --runs yogurt --out rescored --compare-with results` (about 3.5 minutes, plus a download of about
2.3 GB). [`docs/REPRODUCE.md`](docs/REPRODUCE.md) gives four tiers:
rescoring the released predictions, rerunning the CPU models, regenerating a dataset from the calibration bundle, and
rerunning the foundation models on a GPU.

## Tasks and scores

A method is fitted on the 140-week training window of a dataset, using only that dataset's input files, and evaluated
on the following 16 weeks in two tasks.

- **Sales forecasting:** predict the units `q̂` of every product-store-week of the evaluation file at the actual
  prices. Score: the **forecast error** `Σ |q̂ - q| / Σ q`, where `q` are the true expected units.
- **Counterfactual prediction:** predict how these units change if the price of one product or one brand is raised or
  lowered by 10% through its promotion discount (32 scenarios: the top 12 products and the top 4 brands, each +10% and
  -10%). On the rows `R` where a scenario changes a target's price, the predicted percentage change is applied to the
  true units, `q̃_cf = q (1 + Δq̂ / q̂)`, so that an error in the sales level cannot offset an error in the response:
  - **counterfactual error** `= Σ_R |q̃_cf - q_cf| / Σ_R q_cf`;
  - **counterfactual bias** `= Σ_R (q̃_cf - q_cf) / Σ_R |q_cf - q|` (a method that overstates the effect of price has a
    negative bias after a price increase and a positive bias after a decrease).

The headline score is the mean counterfactual error over the 32 scenarios; the mean absolute bias and the share of
scenarios with an overstated response describe the direction of the errors. Methods are ranked by the mean
counterfactual error on the confounding-on configurations, separately for each demand model, and scores are reported as
means over the five seeds with 95% t-intervals. A score is valid only if every ground-truth row has a prediction, no
row is excluded, all 32 scenarios are scored, no key repeats in the forecast file and the scores are finite. A method
may also submit its 40 x 40 table of elasticities, which is compared with the true table and reported, not ranked
(`causaldemand elasticity`). [`docs/SUBMISSION.md`](docs/SUBMISSION.md) gives the exact rules.

## Before you use the results

- **Rounding floor.** Prices paid are rounded to end in 9 cents, while demand responds to the unrounded price. A
  reference that knows the true demand but reads the scenario file's price change at face value scores a
  counterfactual error of 0.038 to 0.042 (mean over seeds per configuration). These reference rows are in
  [`results/main/reference_rows/`](results/main/reference_rows/README.md).
- **Pseudonyms can be decoded by inference.** Brands, markets, chains and manufacturers are replaced by codes, and
  product and store identifiers by numbers, but the codes keep the order of the real labels (so that regeneration
  reproduces the data exactly) and the released sales and public influenza data reveal more. Many codes can be
  matched to a real market, chain, brand, manufacturer, product or store ([`docs/DATASHEET.md`](docs/DATASHEET.md)).
- **Reference models fill the seasonal index of the evaluation weeks with 1.0**, a high-season value on the
  discrete-choice index. The archived results keep it. Filling the training-window mean instead changes one examined
  run's counterfactual error by 0.002 and its forecast error by 0.013; the five-seed counterfactual error of that
  configuration does not change at three decimals ([`docs/EXPERIMENT_RULES.md`](docs/EXPERIMENT_RULES.md), Section 4.4).
- **The elasticity diagnostic** (Appendix F.4 of the paper) is supplementary. The true table describes a change of the
  regular price; the log-linear cross elasticities count a competitor only where it is stocked
  ([`docs/DATA.md`](docs/DATA.md), Section 4.4).
- **Foundation-model runs made again.** The predictions of the original foundation-model runs on the facial-tissue
  datasets were not kept, so the runs of the main experiment and of the confounding strengths 0.15 and 0.45 were made
  again on 2026-10-03 with the original settings. Their predictions and run records are released, and the scores in
  `results/` are theirs. The foundation-model entries of the main results table and of the confounding-strength table
  at each strength are unchanged at three decimals. A run repeated on the GPU model of the original run gave the same
  scores; on another GPU model the counterfactual and forecast errors of single runs differ by at most 0.0017 (GPU
  rounding; [`results/README.md`](results/README.md)).
- **Not released:** the licensed store sales records and store profiles the generator was calibrated on; the datasets
  and predictions of the household-switching robustness check (its scores are in `results/`); the pipeline that wrote
  the product descriptions and computed the product distances (both are released as files); the code and runs of the
  paper's real-data check.
- **Yogurt.** The 20 yogurt datasets, their ground truth and the predictions and run records of the 320 yogurt runs
  (100 CPU runs with default settings, 80 forecast-tuned CPU runs and 140 foundation-model runs) are released as files.
  The licensed yogurt sales records, product dictionary and household purchase panel they rest on, and the code that
  computed their product distance, are not released; no calibration bundle is given for them, so they cannot be
  regenerated. Their product, store, brand, chain and manufacturer codes are numbered separately from those of the
  facial-tissue datasets; only the market codes are shared. All 20 fail the generator's seasonal-swing check (a swing
  of 0.016 to 0.044 against the band 0.057 ± 0.01) and are released as generated ([`docs/DATA.md`](docs/DATA.md),
  Section 11).

## Documentation

| Document | Contents |
|---|---|
| [`docs/DATA.md`](docs/DATA.md) | Every file, column and key of the datasets, the runs and the generator inputs |
| [`docs/SUBMISSION.md`](docs/SUBMISSION.md) | Prediction formats, what a method may use, validity, scores, reporting |
| [`docs/REPRODUCE.md`](docs/REPRODUCE.md) | Four tiers: rescore the released predictions, rerun the CPU models, regenerate a dataset, rerun the foundation models |
| [`docs/EXPERIMENT_RULES.md`](docs/EXPERIMENT_RULES.md) | The protocol behind the reference results: scenarios, scores, models and settings |
| [`docs/DATASHEET.md`](docs/DATASHEET.md) | Datasheet: motivation, composition, collection, uses, distribution, maintenance |
| [`locks/README.md`](locks/README.md) | Hash-checked environments for the generator, the CPU models and the foundation models |

## Repository

| Folder | Contents |
|---|---|
| [`causaldemand/`](causaldemand) | The `causaldemand` command: download, score, elasticity, rescore, generate |
| [`metrics/`](metrics/README.md) | The scorer (`panel_scorer.py`) and the elasticity diagnostic (`elasticity.py`) |
| [`generator/`](generator/README.md) | The data generator, the counterfactual scenarios and the ground truth |
| [`config/`](config/README.md) | One configuration per dataset |
| `inputs/` | Generator inputs: the [calibration bundle](inputs/calibration_bundle/README.md), the [product descriptions and distances](inputs/product_distances/README.md), the [CDC influenza curves](inputs/cdc-ili/README.md) |
| [`packaging/`](packaging/README.md) | Turns generator output into the released files |
| [`models/`](models/README.md) | The reference models of the paper, as the code of the archived runs |
| [`experiments/`](experiments/README.md) | The scripts behind every result of the paper |
| [`results/`](results/README.md) | Scores of the paper's reference-model runs on the synthetic datasets (facial tissue and yogurt), with run records except for the household-switching runs |
| [`notebooks/`](notebooks) | `quickstart.ipynb` and `full_run.ipynb` ([Notebooks](#notebooks)) |
| `tests/` | Tests of the package; with `metrics/`, the default pytest run of the repository |

## License and citation

Code: Apache License 2.0 ([`LICENSE`](LICENSE)). Data: CC BY 4.0 ([`DATA_LICENSE.md`](DATA_LICENSE.md)), which also
lists the sources and their terms. To cite, use [`CITATION.cff`](CITATION.cff): the paper, and the datasets by their
DOI:

```bibtex
@inproceedings{hong2027causaldemand,
  title     = {CausalDemand: Benchmarking Causal Recovery of Demand from Retail Transactions and Product Text},
  author    = {Hong, Juwon and Hwang, Minha and Shankar, Venkatesh},
  booktitle = {Proceedings of the ACM SIGKDD Conference on Knowledge Discovery and Data Mining (KDD 2027), Datasets and Benchmarks Track},
  year      = {2027},
  note      = {Submitted}
}

@misc{hong2026causaldemanddata,
  title     = {CausalDemand},
  author    = {Hong, Juwon and Hwang, Minha and Shankar, Venkatesh},
  year      = {2026},
  publisher = {Hugging Face},
  doi       = {10.57967/hf/10744},
  url       = {https://huggingface.co/datasets/jean-jsj/CausalDemand},
  note      = {Datasets, version 1.0.0 (Hugging Face revision 3b0a380)}
}
```

The first author maintains the benchmark; questions and problems go to the GitHub issues of this repository.
