# rahul-gembali/r4-demand-forecasting architecture

> Architecture diagram of the GitHub repository rahul-gembali/r4-demand-forecasting, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rahul-gembali/r4-demand-forecasting
- Repository: https://github.com/rahul-gembali/r4-demand-forecasting

## Overview

R4 is a cold-start retail demand forecasting platform: catalog data is cleaned and feature-engineered, baseline and supervised models produce launch forecasts, uncertainty is calibrated, and inventory recommendations and explanations are served through a REST API to operational dashboard screens. The README documents the end-to-end stages; sampled source confirms selected pipeline, API, persistence, and dashboard connections. Several model and screen implementations were not sampled, so their internal links are shown only where documented.

Main entry points: [`src/api/main.py`](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/api/main.py) (FastAPI gateway), [`src/models/supervised.py`](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/models/supervised.py) (CatBoost forecaster), [`src/inventory/optimizer.py`](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/inventory/optimizer.py) (Buy optimization), [`src/db/repository.py`](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/db/repository.py) (Data repository)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data preparation"]
  node_pipeline["Data pipeline<br/>[pipeline.py]"]
  node_quality["Quality and schema<br/>[quality.py]"]
end

subgraph group_forecast["Forecasting and evaluation"]
  node_analogue["Analogue retrieval<br/>[analogue.py]"]
  node_launchcurve["Launch curve baseline<br/>[baseline.py]"]
  node_supervised["CatBoost forecaster<br/>[supervised.py]"]
  node_uncertainty["Conformal uncertainty<br/>[uncertainty.py]"]
  node_metrics["Forecast metrics<br/>[metrics.py]"]
  node_evaluation["Official evaluation"]
end

subgraph group_decision["Inventory and insight"]
  node_inventory["Buy optimization<br/>[optimizer.py]"]
  node_constraints["Order constraints<br/>[constraints.py]"]
  node_costmodel["Inventory cost model<br/>[cost_model.py]"]
  node_shap["TreeSHAP explanations<br/>[shap_engine.py]"]
  node_confidence["Confidence scoring<br/>[confidence.py]"]
  node_synthesizer["Explanation synthesis<br/>[synthesizer.py]"]
end

subgraph group_platform["API and persistence"]
  node_api["FastAPI gateway<br/>[main.py]"]
  node_forecast_routes["Forecast endpoints<br/>[forecasts.py]"]
  node_inventory_routes["Inventory endpoints<br/>[inventory.py]"]
  node_data_routes["Data endpoints<br/>[data.py]"]
  node_repository["Data repository<br/>[repository.py]"]
  node_dbmodels[("Database records<br/>[models.py]")]
  node_session["Database sessions<br/>[session.py]"]
  node_auth["Role-based access<br/>[auth.py]"]
  node_monitoring["Telemetry and drift<br/>[monitoring.py]"]
end

subgraph group_dashboard["Operations dashboard"]
  node_app["Dashboard application<br/>[App.tsx]"]
  node_client["Typed API client<br/>[api.ts]"]
  node_types["API data types<br/>[api.ts]"]
  node_screens["Operational screens"]
  node_charts["Launch curve chart"]
end

node_operator(("Retail operator"))

node_operator -->|"uses"| node_app
node_app -->|"requests data"| node_client
node_client -->|"uses types"| node_types
node_app -->|"selects screen"| node_screens
node_screens -->|"renders"| node_charts
node_client -->|"calls REST API"| node_api
node_api -->|"registers routes"| node_forecast_routes
node_api -->|"registers routes"| node_inventory_routes
node_api -->|"registers routes"| node_data_routes
node_forecast_routes -->|"checks roles"| node_auth
node_forecast_routes -->|"reads and writes"| node_repository
node_inventory_routes -->|"reads and writes"| node_repository
node_data_routes -->|"reads and writes"| node_repository
node_repository -->|"persists records"| node_dbmodels
node_session -->|"binds metadata"| node_dbmodels
node_api -->|"tracks requests"| node_monitoring
node_pipeline -->|"audits data"| node_quality
node_pipeline -->|"feeds forecasting"| node_launchcurve
node_pipeline -->|"feeds retrieval"| node_analogue
node_launchcurve -->|"informs forecasts"| node_supervised
node_analogue -->|"informs forecasts"| node_supervised
node_supervised -->|"calibrates bounds"| node_uncertainty
node_uncertainty -->|"evaluated by"| node_evaluation
node_evaluation -->|"uses metrics"| node_metrics
node_uncertainty -->|"informs buy plan"| node_inventory
node_inventory -->|"applies constraints"| node_constraints
node_inventory -->|"uses costs"| node_costmodel
node_supervised -->|"explained by"| node_shap
node_synthesizer -->|"uses attributions"| node_shap
node_synthesizer -->|"uses scoring"| node_confidence
node_inventory_routes -->|"uses optimizer"| node_inventory

click node_pipeline "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/data/pipeline.py"
click node_quality "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/data/quality.py"
click node_analogue "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/models/analogue.py"
click node_launchcurve "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/models/baseline.py"
click node_supervised "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/models/supervised.py"
click node_uncertainty "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/evaluation/uncertainty.py"
click node_metrics "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/evaluation/metrics.py"
click node_evaluation "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/evaluation/official_evaluator.py"
click node_inventory "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/inventory/optimizer.py"
click node_constraints "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/inventory/constraints.py"
click node_costmodel "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/inventory/cost_model.py"
click node_shap "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/explainability/shap_engine.py"
click node_confidence "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/explainability/confidence.py"
click node_synthesizer "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/explainability/synthesizer.py"
click node_api "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/api/main.py"
click node_forecast_routes "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/api/routes/forecasts.py"
click node_inventory_routes "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/api/routes/inventory.py"
click node_data_routes "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/api/routes/data.py"
click node_repository "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/db/repository.py"
click node_dbmodels "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/db/models.py"
click node_session "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/db/session.py"
click node_auth "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/api/auth.py"
click node_monitoring "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/core/monitoring.py"
click node_app "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/frontend/src/App.tsx"
click node_client "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/frontend/src/services/api.ts"
click node_types "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/frontend/src/types/api.ts"
click node_screens "https://github.com/rahul-gembali/r4-demand-forecasting/tree/main/frontend/src/screens"
click node_charts "https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/frontend/src/components/LaunchCurveChart.tsx"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pipeline,node_quality toneBlue
class node_analogue,node_launchcurve,node_supervised,node_uncertainty,node_metrics,node_evaluation toneAmber
class node_inventory,node_constraints,node_costmodel,node_shap,node_confidence,node_synthesizer toneMint
class node_api,node_forecast_routes,node_inventory_routes,node_data_routes,node_repository,node_dbmodels,node_session,node_auth,node_monitoring toneRose
class node_app,node_client,node_types,node_screens,node_charts,node_operator toneIndigo
```

## Components

### Data preparation

- [Data pipeline](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/data/pipeline.py): `src/data/pipeline.py`
- [Quality and schema](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/data/quality.py): `src/data/quality.py`

### Forecasting and evaluation

- [Analogue retrieval](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/models/analogue.py): `src/models/analogue.py`
- [Launch curve baseline](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/models/baseline.py): `src/models/baseline.py`
- [CatBoost forecaster](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/models/supervised.py): `src/models/supervised.py`
- [Conformal uncertainty](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/evaluation/uncertainty.py): `src/evaluation/uncertainty.py`
- [Forecast metrics](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/evaluation/metrics.py): `src/evaluation/metrics.py`
- [Official evaluation](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/evaluation/official_evaluator.py): `src/evaluation/official_evaluator.py`

### Inventory and insight

- [Buy optimization](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/inventory/optimizer.py): `src/inventory/optimizer.py`
- [Order constraints](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/inventory/constraints.py): `src/inventory/constraints.py`
- [Inventory cost model](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/inventory/cost_model.py): `src/inventory/cost_model.py`
- [TreeSHAP explanations](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/explainability/shap_engine.py): `src/explainability/shap_engine.py`
- [Confidence scoring](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/explainability/confidence.py): `src/explainability/confidence.py`
- [Explanation synthesis](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/explainability/synthesizer.py): `src/explainability/synthesizer.py`

### API and persistence

- [FastAPI gateway](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/api/main.py): `src/api/main.py`
- [Forecast endpoints](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/api/routes/forecasts.py): `src/api/routes/forecasts.py`
- [Inventory endpoints](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/api/routes/inventory.py): `src/api/routes/inventory.py`
- [Data endpoints](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/api/routes/data.py): `src/api/routes/data.py`
- [Data repository](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/db/repository.py): `src/db/repository.py`
- [Database records](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/db/models.py): `src/db/models.py`
- [Database sessions](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/db/session.py): `src/db/session.py`
- [Role-based access](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/api/auth.py): `src/api/auth.py`
- [Telemetry and drift](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/src/core/monitoring.py): `src/core/monitoring.py`

### Operations dashboard

- [Dashboard application](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/frontend/src/App.tsx): `frontend/src/App.tsx`
- [Typed API client](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/frontend/src/services/api.ts): `frontend/src/services/api.ts`
- [API data types](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/frontend/src/types/api.ts): `frontend/src/types/api.ts`
- [Operational screens](https://github.com/rahul-gembali/r4-demand-forecasting/tree/main/frontend/src/screens): `frontend/src/screens`
- [Launch curve chart](https://github.com/rahul-gembali/r4-demand-forecasting/blob/main/frontend/src/components/LaunchCurveChart.tsx): `frontend/src/components/LaunchCurveChart.tsx`

### Other components

- **Retail operator**

## Connections

- Retail operator → Dashboard application: uses (evidence: `README.md`)
- Dashboard application → Typed API client: requests data (evidence: `frontend/src/App.tsx`)
- Typed API client → API data types: uses types (evidence: `frontend/src/services/api.ts`)
- Dashboard application → Operational screens: selects screen (evidence: `frontend/src/App.tsx`)
- Operational screens → Launch curve chart: renders (evidence: `frontend/src/screens/SKUDetailsScreen.tsx`)
- Typed API client → FastAPI gateway: calls REST API (evidence: `README.md`)
- FastAPI gateway → Forecast endpoints: registers routes (evidence: `src/api/main.py`)
- FastAPI gateway → Inventory endpoints: registers routes (evidence: `src/api/main.py`)
- FastAPI gateway → Data endpoints: registers routes (evidence: `src/api/main.py`)
- Forecast endpoints → Role-based access: checks roles (evidence: `src/api/routes/forecasts.py`)
- Forecast endpoints → Data repository: reads and writes (evidence: `src/api/routes/forecasts.py`)
- Inventory endpoints → Data repository: reads and writes (evidence: `src/api/routes/inventory.py`)
- Data endpoints → Data repository: reads and writes (evidence: `src/api/routes/data.py`)
- Data repository → Database records: persists records (evidence: `src/db/repository.py`)
- Database sessions → Database records: binds metadata (evidence: `src/db/session.py`)
- FastAPI gateway → Telemetry and drift: tracks requests (evidence: `src/api/main.py`)
- Data pipeline → Quality and schema: audits data (evidence: `src/data/pipeline.py`)
- Data pipeline → Launch curve baseline: feeds forecasting (evidence: `README.md`)
- Data pipeline → Analogue retrieval: feeds retrieval (evidence: `README.md`)
- Launch curve baseline → CatBoost forecaster: informs forecasts (evidence: `README.md`)
- Analogue retrieval → CatBoost forecaster: informs forecasts (evidence: `README.md`)
- CatBoost forecaster → Conformal uncertainty: calibrates bounds (evidence: `README.md`)
- Conformal uncertainty → Official evaluation: evaluated by (evidence: `src/evaluation/official_evaluator.py`)
- Official evaluation → Forecast metrics: uses metrics (evidence: `src/evaluation/official_evaluator.py`)
- Conformal uncertainty → Buy optimization: informs buy plan (evidence: `README.md`)
- Buy optimization → Order constraints: applies constraints (evidence: `src/inventory/optimizer.py`)
- Buy optimization → Inventory cost model: uses costs (evidence: `src/inventory/optimizer.py`)
- CatBoost forecaster → TreeSHAP explanations: explained by (evidence: `src/explainability/shap_engine.py`)
- Explanation synthesis → TreeSHAP explanations: uses attributions (evidence: `src/explainability/synthesizer.py`)
- Explanation synthesis → Confidence scoring: uses scoring (evidence: `src/explainability/synthesizer.py`)
- Inventory endpoints → Buy optimization: uses optimizer (evidence: `src/api/routes/inventory.py`)
