Dual-model real-time credit risk engine with:
- Universal Historian (structural baseline risk)
- VECTOR Behavioral model (transactional/velocity risk)
- Fusion layer (single final score)
- MongoDB-backed runtime + FastAPI endpoints
realtime_risk_engine/
models/
behavioral_engine_v2.pkl
universal_historian_v1.pkl
universal_features_map.pkl
scripts/
init_mongo.py
seed_mongo_sample.py
score_from_mongo.py
src/
server.py
demo_stream.py
inference.py
historian.py
feature_engine.py
fusion.py
mongo_store.py
- Python 3.10+
- Local MongoDB running on
localhost:27017 - Dependencies from repo root
requirements.txt
Install dependencies in your venv:
pip install -r requirements.txt- Ensure MongoDB service is running locally.
- Create/update
realtime_risk_engine/.env. - Initialize DB connection + indexes:
python realtime_risk_engine/scripts/init_mongo.pyExpected success output:
MongoDB connection successful.
Indexes ensured for customers.account_id and transactions.account_id.
Use realtime_risk_engine/.env.example as base:
MONGODB_URI=mongodb://<username>:<password>@localhost:27017/?authSource=admin
MONGODB_DB_NAME=realtime_risk_engine
MONGODB_CUSTOMERS_COLLECTION=customers
MONGODB_TRANSACTIONS_COLLECTION=transactions
SAMPLE_ACCOUNT_PREFIX=CUST
SAMPLE_ACCOUNT_START=1
SAMPLE_CUSTOMER_COUNT=10
DEMO_INTERVAL_SECONDS=2Notes:
- If auth is disabled locally, URI can be
mongodb://localhost:27017. - If auth is enabled, include valid username/password and
authSource=admin(or your auth DB).
Seed sample customer profiles + transaction history:
python realtime_risk_engine/scripts/seed_mongo_sample.pyThis creates:
- one customer document per account in
customers - one transaction document per account in
transactions
For a fixed starter dataset, see:
- [INITIAL_CUSTOMERS.md](/e:/Machine learning/barclayss/risk-prediction/realtime_risk_engine/INITIAL_CUSTOMERS.md)
From repo root:
uvicorn realtime_risk_engine.src.server:app --host 127.0.0.1 --port 8000 --reloadSwagger UI:
http://127.0.0.1:8000/docs
-
GET /demoStarts background transaction flow for all customers. If DB is empty, it seeds sample customers automatically. -
POST /stop-demoStops the background transaction flow. -
GET /demo-statsReturns runtime stream stats:running,interval_seconds,estimated_transactions_per_second,account_count,last_cycle_generated,total_generated,last_cycle_at. -
GET /risk-scoreRuns one scoring pass for all customers and stores a timestamped snapshot into each customer document (latest_prediction+ append torisk_history). -
GET /all_scoresReturns latest and historical risk score data for all customers, including:risk_score_timestamp,risk_score_timestamps_all,risk_score_history,prediction_count.
- Historian scores structural profile risk.
- Behavioral model scores recent transaction behavior.
- Fusion combines both:
final_risk_score = clamp(historian_score + 0.30 * (behavioral_score - 0.46), 0.0, 1.0)
Fallbacks:
- missing historian -> use behavioral score
- missing behavioral -> use historian score
- both missing ->
INSUFFICIENT_DATA
python realtime_risk_engine/scripts/init_mongo.pypython realtime_risk_engine/scripts/seed_mongo_sample.py- start server with
uvicorn ... - call
GET /demo - wait 10-20 seconds
- call
GET /risk-score - call
GET /all_scores - call
POST /stop-demo
account_idprofile(raw structural inputs)latest_prediction(most recent score snapshot + timestamp)risk_history(array of historical score snapshots)updated_at
account_idtransactions(array of MoneyVis-style records)updated_at