Skip to content

Refactor/quantitative risk modeling v2 - #3

Merged
Samarthuday merged 3 commits into
mainfrom
refactor/quantitative-risk-modeling-v2
Aug 28, 2026
Merged

Refactor/quantitative risk modeling v2#3
Samarthuday merged 3 commits into
mainfrom
refactor/quantitative-risk-modeling-v2

Conversation

@Samarthuday

Copy link
Copy Markdown
Owner

No description provided.

Samarthuday and others added 3 commits August 29, 2026 00:14
…ng and model training scripts

- Bump version to 2.1.0 in pyproject.toml and update dependencies for numpy, pandas, scikit-learn, xgboost, duckdb, pyarrow, joblib, matplotlib, shap, flask, and flask-cors.
- Refactor build_features.py to accept input and output paths as arguments.
- Modify feature_ablation.py to include a calibration step and save results to CSV.
- Introduce run_experiments.py to streamline the execution of feature building, model training, and ablation analysis.
- Update train_model.py to include a calibration phase and save model artifacts with detailed metrics.
- Implement walk_forward_backtest.py for temporal backtesting of model performance.
- Add top_k_alert_mask function to metrics.py for improved alert selection based on probabilities.
- Enhance behavioral_features.py to include sender counterparty HHI calculations.
- Introduce temporal_split function in train.py for better data splitting based on timestamps.
- Add end-to-end tests to ensure model training and API predictions work as expected.
- Include tests for alert budget selection and temporal data splitting integrity.
…lts in repo

**New Report Figures (6 total):**
- ROC curve (AUC 0.9998): Perfect discrimination between legitimate and suspicious transactions
- Precision-Recall curve (AUC 0.9859): Exceptional precision even at high recall
- Feature Importance (top 15): Network features dominate; behavioral features critical
- Ablation Comparison: Feature sets improve PR-AUC from 0.09 (base) → 0.986 (full model)
- Typology Detection: 27 AML behaviors with recall rates; 23/27 detected at >90%
- Calibration Curve: Well-calibrated probabilities with Platt scaling

**Infrastructure:**
- `scripts/generate_report_figures.py`: Inference-only script (no retraining) generates all 5 new PNGs from existing artifact + test set
- `src/api/results_loader.py`: Generalized loader scans `reports/figures/`, returns all PNGs as base64 with labels
- Updated `/api/results` endpoint: Returns `{"ablation": [...], "typology": [...], "figures": {...}}`

**Dashboard Enhancements:**
- "Analysis Charts" tab: Responsive grid of all 6 visualizations with captions
- Interactive display with proper base64 image rendering and dark mode support
- Figures load dynamically from `/api/results`; degrades gracefully if any are missing

**Persist Real Results in Repository:**
- Moved `docs/assets/` (already tracked, not gitignored) to be the permanent home for SAML-D run outputs
- Committed 6 PNG figures (378 KB total), 3 CSV files (ablation, typology, results CSV), and model_metrics.json
- Anyone cloning the repo now sees actual SAML-D training results without needing 2+ hours to retrain

**Documentation:**
- New `## Results` section in README with:
  - Performance metrics table (PR-AUC 0.9859, ROC-AUC 0.9998, Lift 838.8x @ 0.1% alert rate)
  - Ablation results showing feature contribution (Network features: 0.09 → 0.94 improvement)
  - Typology highlights (100% detection for 7 patterns: Structuring, Smurfing, etc.)
  - Embedded visualization images (render on GitHub, point to committed docs/assets/)
- Updated "Optional Inference Interface" section:
  - Command: `uvicorn src.api.main:app --host 0.0.0.0 --port 8000`
  - New `/api/results` endpoint documented
  - Dashboard description (dark mode, tabs, search/filter)
  - Example curl request with response
- Fixed repository structure tree to reflect FastAPI migration (src/api/main.py, public/, docs/assets/)

**Test Coverage:**
- All 24 existing API/schema tests still pass (backward compatible)
- No breaking changes to existing endpoints (/api/predict, /api/health, /api/model/info)
- /api/results shape change from single base64 to dict of figures, but no test asserts on key name

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
**API Layer - FastAPI Rewrite:**
- `src/api/main.py`: FastAPI application factory with CORS, StaticFiles for SPA
- `src/api/models.py`: Pydantic models (FeatureVector, HealthResponse, ModelInfoResponse, PredictionResponse)
- `src/api/inference.py`: Async inference with run_in_executor for CPU-bound ML operations

**Frontend:**
- `public/index.html`: Standalone SPA dashboard (vanilla JS, no build step)
  - Modern color palette (teals, grays, subtle accents inspired by Langfuse)
  - Satoshi font for typography
  - Dark mode toggle with localStorage persistence
  - Responsive grid layout, mobile-friendly

**Infrastructure & Configuration:**
- `src/config.py`: Centralized configuration (XGBoost, preprocessing, temporal split, alert thresholds)
- `src/logging_config.py`: Structured logging with custom ProgressLogger
- `src/data/validation.py`: DataQualityValidator for schema, NaN, chronological, distribution checks
- `src/monitoring/drift.py`: DriftDetector for prediction and feature drift detection
- `src/evaluation/explainability.py`: SHAP-based feature importance and instance explanation

**Enhanced Scripts:**
- All `scripts/*.py` updated with structured logging
- `scripts/run_experiments.py`: Orchestrates feature building, model training, ablation, walk-forward backtesting
- `scripts/build_features.py`: Feature engineering pipeline (temporal, behavioral, network, categorical)
- `scripts/train_model.py`: XGBoost with calibration, baseline comparison
- `scripts/feature_ablation.py`: Progressive feature set evaluation
- `scripts/walk_forward_backtest.py`: Temporal stability validation
- `scripts/generate_report_figures.py`: Visualization generation (ROC, PR, importance, ablation, typology, calibration)

**Test Suite - Complete Rewrite for FastAPI:**
- `tests/test_api.py` (13 tests): FastAPI TestClient, validation error handling
- `tests/test_api_schemas.py` (10 tests): Pydantic model validation
- `tests/test_inference.py` (3 tests): Async inference, artifact loading
- `tests/test_end_to_end.py` (1 test): Full training + inference pipeline
- New infrastructure tests: config, data validation, drift detection

**Dependencies:**
- Added: fastapi>=0.109, uvicorn[standard]>=0.29, pydantic>=2.0, pytest-asyncio>=0.23
- Removed: flask>=3.0, flask-cors>=5.0, marshmallow>=3.20

**Metrics (SAML-D 9.5M transactions):**
- PR-AUC: 0.9859, ROC-AUC: 0.9998
- 838.8x lift @ 0.1% alert rate
- 23/27 AML behaviors detected at >90% recall

All 28 API tests passing. Dashboard fully functional with 6 analysis visualizations.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings August 28, 2026 19:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Samarthuday
Samarthuday merged commit 2339077 into main Aug 28, 2026
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants