PocketQuant is an end-to-end ML system for predicting whether a merchant will face a liquidity shortage in the next 48 hours (liquidity_shortage_next_48h).
It includes:
- data validation + EDA + feature engineering notebooks,
- model training/evaluation/explainability outputs,
- a production-style FastAPI prediction service,
- and a Streamlit risk intelligence dashboard.
- Rows: 50,000 merchant-day records
- Raw columns: 59
- Unique merchants: 1,000
- Date range: 2024-01-01 to 2024-06-28
- Target positives: 645 / 50,000
- Shortage rate: 1.29%
- Class imbalance: 76.52:1 (negative:positive)
From models/artifacts/model_metrics.csv:
| Model | Accuracy | Precision | Recall | F1 | ROC-AUC |
|---|---|---|---|---|---|
| Logistic Regression | 0.9929 | 0.6726 | 0.8760 | 0.7609 | 0.9976 |
| Random Forest | 0.9945 | 0.7102 | 0.9690 | 0.8197 | 0.9988 |
| XGBoost | 0.9954 | 0.7677 | 0.9225 | 0.8380 | 0.9992 |
| XGBoost (Tuned) | 0.9956 | 0.7778 | 0.9225 | 0.8440 | 0.9991 |
From models/artifacts/threshold_analysis.csv:
- Best F1 around threshold 0.60 (
F1 ≈ 0.8530,Precision ≈ 0.7933,Recall ≈ 0.9225) - Operational threshold 0.40 (used in API startup) gives:
- Precision:
0.7547 - Recall:
0.9302 - F1:
0.8333
- Precision:
This is aligned with risk operations where missing true shortages is costly.
From reports/explainability_report.txt and explainability artifacts:
- Most influential feature:
liquidity_buffer_ratio- Native importance:
0.6853 - SHAP importance:
8.4543
- Native importance:
- Feature category contribution:
- Liquidity Health:
0.7860(dominant) - Merchant Profile:
0.0694 - Transaction Metrics:
0.0560
- Liquidity Health:
- Bias check by merchant category:
- Recall variance:
0.0189 - Precision variance:
0.0506 - Report status: No significant bias detected
- Recall variance:
PocketQuant/
├─ configs/
│ └─ config.yaml
├─ data/
│ ├─ merchant_liquidity.csv
│ └─ processed/
│ ├─ merchant_liquidity_validated.csv
│ ├─ merchant_liquidity_engineered.csv
│ ├─ feature_matrix_X.csv
│ ├─ target_y.csv
│ └─ feature_list.txt
├─ models/
│ ├─ trained/
│ │ ├─ xgboost_liquidity_model.pkl
│ │ ├─ xgboost_liquidity_model.joblib
│ │ ├─ random_forest_model.pkl
│ │ ├─ logistic_regression_model.pkl
│ │ └─ ...
│ └─ artifacts/
│ ├─ model_metrics.csv
│ ├─ best_hyperparameters.csv
│ ├─ feature_importance.csv
│ ├─ feature_importance_detailed.csv
│ ├─ shap_feature_importance.csv
│ ├─ threshold_analysis.csv
│ └─ bias_analysis.csv
├─ notebooks/
│ ├─ 01_data_audit_validation.ipynb
│ ├─ 02_Exploratory_Data_Analysis.ipynb
│ ├─ 03_Feature_Engineering.ipynb
│ ├─ 04_Model_Training.ipynb
│ └─ 05_Model_Explainability.ipynb
├─ reports/
│ ├─ eda_summary.txt
│ ├─ explainability_report.txt
│ └─ figures/
├─ src/
│ ├─ api/
│ │ ├─ main.py
│ │ ├─ predict.py
│ │ ├─ schemas.py
│ │ └─ logger.py
│ └─ dashboard/
│ ├─ app.py
│ ├─ api_client.py
│ └─ config.py
├─ tests/
│ └─ test_api.py
└─ requirements.txt
The project follows this phase sequence:
- Data Audit & Validation (
notebooks/01_data_audit_validation.ipynb)- integrity checks, missingness, duplicates, consistency, summary stats.
- EDA (
notebooks/02_Exploratory_Data_Analysis.ipynb)- target imbalance analysis, correlations, volatility/risk patterns.
- Feature Engineering (
notebooks/03_Feature_Engineering.ipynb)- rolling metrics recalculation, derived risk signals, encoding, final
X/y.
- rolling metrics recalculation, derived risk signals, encoding, final
- Model Training (
notebooks/04_Model_Training.ipynb)- stratified split, imbalance handling, baseline vs tuned models.
- Explainability & Validation (
notebooks/05_Model_Explainability.ipynb)- feature importance, SHAP, threshold sensitivity, category bias checks.
Core file: src/api/main.py
- Loads global predictor at startup (
threshold=0.40) - Single and batch predictions
- Typed request/response via Pydantic schemas
- Request ID + processing-time middleware
- Structured prediction/error logging in
logs/
GET /— service infoGET /health— health + model-loaded statusPOST /predict-risk— single merchant scoringPOST /predict-risk/batch— batch scoring (up to 100)GET /model/info— model metadata + top features + performance snapshot
uvicorn src.api.main:app --reloadDocs available at:
http://localhost:8000/docshttp://localhost:8000/redoc
Core file: src/dashboard/app.py
- Merchant multi-select and comparison table
- Real-time API health/status banner
- KPI cards (inflow, net cash flow, buffer days, credit utilization)
- Risk panel with probability + threshold + action context
- Contributing risk factors
- Analytics charts:
- 7-day cashflow trend (inflow/outflow/net)
- radar-based financial health profile
- Detailed tabs: Liquidity Metrics, Risk Indicators, Merchant Profile
- Dashboard contains a synthetic merchant generator (
SAMPLE_MERCHANTS) for interactive demo. - If API is unreachable, it falls back to heuristic risk estimation so UI remains usable.
streamlit run src/dashboard/app.py --server.port 8505- Python 3.9+
- pip
git clone <your-repo-url>
cd PocketQuant
python -m venv .venv
# Windows
.venv\Scripts\activate
# Linux/Mac
# source .venv/bin/activate
pip install -r requirements.txtThe current requirements.txt is strong for notebook/ML workflow, but API/dashboard runtime packages are not listed there yet.
Install these for full app execution:
pip install fastapi uvicorn streamlit requests shap- Start API:
uvicorn src.api.main:app --reload
- Open interactive docs:
http://localhost:8000/docs
- Start API (
localhost:8000) - In another terminal, start dashboard:
streamlit run src/dashboard/app.py --server.port 8505
- Open dashboard URL printed by Streamlit.
python tests/test_api.py- Best model:
models/trained/xgboost_liquidity_model.pkl - Model comparison:
models/artifacts/model_metrics.csv - Best hyperparameters:
models/artifacts/best_hyperparameters.csv - Feature importance:
models/artifacts/feature_importance.csvmodels/artifacts/feature_importance_detailed.csv
- SHAP importance:
models/artifacts/shap_feature_importance.csv - Threshold operating analysis:
models/artifacts/threshold_analysis.csv - Bias analysis:
models/artifacts/bias_analysis.csv - EDA summary:
reports/eda_summary.txt - Explainability report:
reports/explainability_report.txt
- Excellent ranking performance (ROC-AUC ~0.999), but this level warrants ongoing leakage/temporal robustness checks before production scaling.
- Operational threshold design is sensible for risk management (0.40 prioritizes recall).
- Liquidity indicators dominate model decisions, which matches financial intuition and improves explainability credibility.
- Bias results are promising, but fairness checks should be extended to additional slices (city/state/merchant age bands).
- Engineering next step: align
requirements.txtwith deployable API/dashboard stack to simplify onboarding.
- ✅ Data audit, EDA, feature engineering, training, and explainability phases implemented
- ✅ Trained models and evaluation artifacts available
- ✅ FastAPI prediction service implemented
- ✅ Streamlit intelligence dashboard implemented
⚠️ Dependency manifest needs API/dashboard additions for one-command setup
Before publishing, recommended housekeeping:
- add a
LICENSEfile, - add CI for API tests and linting,
- include deployment instructions (Docker/Cloud Run/Render/etc.),
- add sample request/response JSON snippets in API section.
If you want, I can also generate:
- a production-ready
requirements-api.txtor unified dependency file, and - a short
CONTRIBUTING.md+LICENSEstarter template.