Skip to content
jesusrlgonzalezPublic

About

Censored demand and route orders for small grocery stores (synthetic data): censored negative binomial forecast, WAPE 31.7% to 22.1% on a time holdout, and an order policy. In-browser demo.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Predix: demand and inventory for small grocery stores

English · Español

Live demo | Project page

How much to order every Monday and Thursday in a corner grocery store, based on real demand and not only on what the store managed to sell.

Python (pandas, statsmodels, SciPy) · censored negative binomial · inventory simulation · Streamlit

Synthetic data. Three fictitious stores, five staple products, 2024–2025. predix/generate.py simulates demand (day of week, payday fortnight, December, price and growth) and the operation under the owner's current ordering rule, including expiry and stock-outs. The figures do not belong to any real business.

Executive summary

With the current rule ("top up to X units on Mondays and Thursdays"), on 6 Mondays out of 10 each store runs out of milk, bread or eggs before closing, while it throws away 3 % of the bread. An order computed from the demand forecast cuts the cost of poor replenishment by 46 % and frees 30 % of the capital tied up in inventory.

Indicator, July–December 2025 (simulation with the same demand) Current rule Predix Change
Units not sold (lost sales) 2,195 831 −62 %
Stock-out days (store × product) 104 74 −29 %
Waste (MXN at cost) 17,109 8,171 −52 %
Average inventory (MXN at cost) 49,756 34,959 −30 %
Cost of poor replenishment (lost margin + waste + holding cost) 42,119 22,826 −46 %

Uncertainty: resampling the 27 weeks of the evaluation period (bootstrap, 2,000 draws) gives a reduction of 32 % to 58 % (95 % interval), or MXN 12,300 to 26,800 saved over the six months. The interval reflects week-to-week demand variation, not model-parameter uncertainty.

Recorded sales versus estimated demand

What makes this analysis different

  1. It treats stock-outs as censoring. A sold-out day does not say "demand was 40" but "demand was at least 40". The demand model (negative binomial) uses P(D ≥ sales) on those days, a Tobit-style censored likelihood for counts. Because the data are simulated, the bias of each method can be measured:

    Effect True value Sales as demand Excluding sold-out days Censored
    Saturday vs Monday 1.42 1.57 1.46 1.41
    Price elasticity of eggs −0.60 −0.50 −0.60 −0.60
    December 1.12 1.10 1.11 1.12
  2. It validates out of sample. Trained on January 2024 to June 2025, evaluated on July to December 2025:

    Daily forecast WAPE Bias
    Same weekday last week 31.7 % −0.7 %
    4-week average 25.4 % −1.6 %
    Negative binomial (sales as demand) 22.3 % −2.3 %
    Censored negative binomial 22.1 % −0.3 %

    Estimated lost sales come within 6 % of the true ones, which are only known because the data are simulated.

  3. It does not use a single service level. Each order's target level is the forecast demand until the next delivery plus a buffer from the model's variance. Each product's service level is chosen by simulation (the newsvendor problem):

    • bread, which expires in 5 days: ≈ 70 %;
    • milk: 90–95 %;
    • eggs, rice and oil: 98 %.

Profit by service level Forecast with an 80 % interval

Policy comparison

The full analysis is in notebooks/analisis_predix.ipynb (Spanish).

Route order app (MVP)

Open the app · works offline after the first visit · data stay on the device

For distributors and route sales that set each store's order. Upload the visit history (CSV: date, store, product, units left, expired units collected, sold out on arrival) and get:

  1. A load sheet for the next visit, with the expected returns and lost sales of the suggested order against the current rule, according to the model.
  2. A phone screen per store: units to leave per product, a one-tap "sold out on arrival" mark and a stepper for expired units collected. Marking a stock-out updates the suggestion on the spot, because a sold-out interval only says demand was at least what was left.
  3. A close-of-route export in the same CSV format, so next week's plan uses today's marks, and an optional pilot with a control group (half the stores, chosen at random, keep the current rule).

How it decides: sales between visits = units left − expired collected; sold-out intervals are censored observations of a negative binomial demand per store and product, with more weight on recent weeks and a shared dispersion per product estimated with the censored likelihood. The order is the smallest quantity that lasts until the following visit with the chosen probability (default 70 %), or the margin-versus-return critical ratio if higher.

On the bundled simulated route (23 stores, 5 products, 12 weeks of Monday/Thursday visits, 38 % of intervals sold out), the censored estimate cuts the demand error from 18 % to 10 %, and the model expects 27 % fewer returns for the same lost sales (cost of poor replenishment MXN 7,231 vs 9,655 per visit day). Pilot protocol: docs/MVP_PILOTO.md (Spanish). Design: Figma file "Predix · MVP 3", direction B "Señal de campo" (black, white and a single signal yellow, built to be read in direct sunlight).

Dashboard

streamlit_app.py has four tabs:

  • Suggested order: for each ordering day, closing inventory, expected demand until the next delivery, target level and suggested order against the current rule's. Downloadable as CSV.
  • Forecast: 14 days ahead with an 80 % interval, by store and product.
  • Patterns: day of week, payday fortnight and all model effects.
  • Inventory policy: simulation of both rules, with a control to change each product's service level.

Dashboard: inventory policy

How to run it

python -m venv .venv
.venv\Scripts\activate            # Windows · macOS/Linux: source .venv/bin/activate
pip install -r requirements-dev.txt

python -m predix.generate          # optional: regenerate data/ (fixed seed)
pytest                             # 14 tests: data, model and inventory
streamlit run streamlit_app.py
jupyter lab notebooks/analisis_predix.ipynb

Structure

├── predix/
│   ├── config.py      # stores, products, calendar and assumptions
│   ├── generate.py    # simulated demand + FIFO inventory with expiry
│   ├── model.py       # censored negative binomial, forecast and reference methods
│   ├── inventory.py   # current rule, dynamic target level, simulation and service-level tuning
│   └── plots.py
├── data/              # ventas_diarias.csv (10,965 rows) and catalogo.csv
├── notebooks/
├── reports/figures/
├── tests/
└── streamlit_app.py

ventas_diarias.csv has one row per store, product and day (column names in Spanish):

  • precio_mxn: price
  • inventario_inicial: opening stock, after receiving and removing expired units
  • recibido: received
  • merma: waste
  • ventas: sales
  • inventario_final: closing stock; if 0, the product sold out and sales are a lower bound of demand
  • pedido: order, delivered the next day

Assumptions and limitations

  • Orders and deliveries: orders on Mondays and Thursdays, delivered the next day; holding cost is 25 % a year.
  • Lost sales: assumed lost, with no substitution by the customer. If customers substitute, the real benefit is smaller.
  • Scope: a real store has hundreds of products and cross promotions. The method scales, but the parameters must be estimated from the store's own data.
  • Data worth starting to record: the time each product sells out. With it, censored demand is estimated even better.

Skills demonstrated

Skill Evidence
Experiment design (A/B pilot) / Diseño de experimentos site/ruta/model.js (pilotGroup: tiendas piloto y control), site/ruta/app.js
Operations research (newsvendor) / Investigación de operaciones predix/inventory.py
Monte Carlo simulation / Simulación de Monte Carlo predix/inventory.py, predix/plots.py
Demand forecasting / Previsión de la demanda predix/model.py (binomial negativa censurada)
Inventory management, JavaScript / Gestión de inventarios predix/inventory.py, site/ruta/
Data visualization, Git, CI, testing / Visualización de datos, Git, integración continua, pruebas streamlit_app.py, .github/workflows/, tests/

License

Code under MIT.


Jesús R. L. González · Data Analyst and Data Scientist · Operations and quality · LinkedIn

About

Censored demand and route orders for small grocery stores (synthetic data): censored negative binomial forecast, WAPE 31.7% to 22.1% on a time holdout, and an order policy. In-browser demo.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages