financial-risk-api / src /berka_fetch.py
github-actions[bot]
Deploy fcfc762: Validate against real loan outcomes; the synthetic metrics did not survive
cbbd555
Raw History Blame Contribute Delete
2.13 kB
"""Real-outcome validation pipeline (PKDD'99 Berka).
Run in order:
berka_fetch.py pulls the loan/account/client tables from NayaOne
berka_build.py joins loans to transaction history -> berka_validation.db
berka_validate.py scores the CURRENT production model against real outcomes
berka_train.py fits a small sign-constrained model ON the real outcomes
This is the only dataset in the project with credit outcomes adjudicated by a
bank rather than derived from the same transactions the features come from.
"""
import requests, json, collections, os
KEY="n1p.yVKljqrl.dxz6PLvkTw3OmnwW3co8j+lqAS4"
H={"Accept-Profile":"api","sandpit-key":KEY}
BASE="https://data.nayaone.com/"
OUT=r"C:\Users\piplu\financial_risk_system\db"
def pull(ep, cap=200000, batch=10000):
rows=[]
for off in range(0, cap, batch):
r=requests.get(BASE+ep, headers=H, params={"limit":batch,"offset":off}, timeout=60)
if r.status_code!=200: break
b=r.json()
if not b: break
rows+=b
if len(b)<batch: break
return rows
for ep, fname in [("czech_financial_data_1999_loan","berka_loan.json"),
("czech_financial_data_1999_account","berka_account.json"),
("czech_financial_data_1999_disp","berka_disp.json"),
("czech_financial_data_1999_client","berka_client.json")]:
rows=pull(ep)
with open(os.path.join(OUT,fname),"w",encoding="utf-8") as f: json.dump(rows,f)
print(f"{ep:42s} {len(rows):>7} rows -> {fname}")
loans=json.load(open(os.path.join(OUT,"berka_loan.json"),encoding="utf-8"))
st=collections.Counter(r["status"] for r in loans)
print(f"\nREAL loan outcomes, n={len(loans)}")
meaning={"A":"finished, repaid","B":"finished, NOT repaid (default)",
"C":"running, ok","D":"running, in debt (default)"}
for k in sorted(st):
print(f" {k} {st[k]:>4} ({100*st[k]/len(loans):5.1f}%) {meaning.get(k,'')}")
bad=st["B"]+st["D"]
print(f"\n defaults (B+D): {bad} / {len(loans)} = {100*bad/len(loans):.1f}% real default rate")
print(f" distinct accounts: {len({r['account_id'] for r in loans})}")