#!/usr/bin/env python3 # -*- coding: utf-8 -*- """金融联合风控 —— 真实感样本数据生成器 场景:某商业银行(数据提供方)与某持牌征信机构(数据服务方)在可信数据空间内, 为一家消费金融公司(数据使用方)做联合信用风险建模。 两方各持一张表,仅共享 uid 作为求交主键,特征互不可见: bank_credit_features.csv 银行侧:账户与还款行为特征(含标签 default_flag) bureau_credit_scores.csv 征信侧:外部征信评分与多头借贷特征 数据不是纯随机:标签由一组带真实业务方向性的隐变量线性生成(逾期次数↑、 额度使用率↑、多头查询↑ → 违约概率↑;征信分↑、收入↑ → 违约概率↓), 因此联合建模能得到有业务意义的 AUC(约 0.80+),且"银行单方建模" 明显弱于"联合建模",可用来证明数据要素融合的增量价值。 用法:python3 gen_riskctrl_data.py [输出目录] """ import os import sys import numpy as np import pandas as pd SEED = 20260807 rng = np.random.default_rng(SEED) # 两方客户群体有重叠但不完全相同 —— 这样 PSI 求交才有真实意义 N_BANK = 4000 N_BUREAU = 3600 N_OVERLAP = 2400 # 真实交集规模 def gen(): # ---- 构造 uid 池:交集 + 各自独有 ---- overlap = np.array(["U%08d" % i for i in rng.choice(90000000, N_OVERLAP, replace=False)]) bank_only = np.array(["B%08d" % i for i in rng.choice(90000000, N_BANK - N_OVERLAP, replace=False)]) bureau_only = np.array(["C%08d" % i for i in rng.choice(90000000, N_BUREAU - N_OVERLAP, replace=False)]) bank_uid = np.concatenate([overlap, bank_only]) bureau_uid = np.concatenate([overlap, bureau_only]) rng.shuffle(bank_uid) rng.shuffle(bureau_uid) # ---- 银行侧特征(账户与还款行为)---- def bank_feats(uids): n = len(uids) age = np.clip(rng.normal(36, 9, n), 22, 65).round(0) # 月收入(元),右偏 income = np.clip(rng.lognormal(9.4, 0.45, n), 3000, 120000).round(0) # 授信额度与收入正相关 credit_limit = np.clip(income * rng.uniform(1.5, 6.0, n), 5000, 500000).round(-2) # 额度使用率 used_ratio = np.clip(rng.beta(2.2, 3.0, n), 0.01, 0.99).round(4) # 近12月逾期次数(多数为0) overdue_12m = rng.poisson(0.45, n) # 账龄(月) acct_age_m = np.clip(rng.gamma(3.0, 12.0, n), 3, 240).round(0) return pd.DataFrame({ "uid": uids, "age": age.astype(int), "monthly_income": income.astype(int), "credit_limit": credit_limit.astype(int), "used_ratio": used_ratio, "overdue_cnt_12m": overdue_12m.astype(int), "acct_age_month": acct_age_m.astype(int), }) # ---- 征信侧特征(外部评分与多头借贷)---- def bureau_feats(uids): n = len(uids) credit_score = np.clip(rng.normal(660, 75, n), 350, 950).round(0) query_cnt_3m = rng.poisson(1.8, n) # 近3月征信查询次数(多头信号) loan_org_cnt = rng.poisson(2.2, n) # 在贷机构数 has_guarantee = rng.binomial(1, 0.18, n) # 是否有对外担保 return pd.DataFrame({ "uid": uids, "credit_score": credit_score.astype(int), "query_cnt_3m": query_cnt_3m.astype(int), "loan_org_cnt": loan_org_cnt.astype(int), "has_guarantee": has_guarantee.astype(int), }) bank = bank_feats(bank_uid) bureau = bureau_feats(bureau_uid) # ---- 生成标签:只对"两方都覆盖"的客户用完整信息,保证联合建模确有增量 ---- merged = bank.merge(bureau, on="uid", how="left") z = np.zeros(len(merged)) # 银行侧方向性(业务常识) z += 0.85 * merged["overdue_cnt_12m"].values z += 2.10 * merged["used_ratio"].values z -= 0.45 * np.log(merged["monthly_income"].values / 8000.0) z -= 0.004 * merged["acct_age_month"].values z -= 0.012 * (merged["age"].values - 36) # 征信侧方向性(缺失的用群体均值兜底,等价于"没有外部数据时的盲区") cs = merged["credit_score"].fillna(660).values qc = merged["query_cnt_3m"].fillna(1.8).values lo = merged["loan_org_cnt"].fillna(2.2).values gu = merged["has_guarantee"].fillna(0).values z -= 0.0105 * (cs - 660) z += 0.30 * qc z += 0.22 * lo z += 0.35 * gu # 截距按真实信贷业务的违约率量级(个位数百分比)标定;噪声控制可分性 z = z - 4.60 + rng.normal(0, 0.55, len(merged)) p = 1.0 / (1.0 + np.exp(-z)) label = rng.binomial(1, p) bank = bank.copy() bank["default_flag"] = label return bank, bureau def main(): out_dir = sys.argv[1] if len(sys.argv) > 1 else os.path.dirname(os.path.abspath(__file__)) os.makedirs(out_dir, exist_ok=True) bank, bureau = gen() bank_path = os.path.join(out_dir, "bank_credit_features.csv") bureau_path = os.path.join(out_dir, "bureau_credit_scores.csv") bank.to_csv(bank_path, index=False) bureau.to_csv(bureau_path, index=False) inter = len(set(bank["uid"]) & set(bureau["uid"])) print("银行侧样本 %d 行 / 违约率 %.2f%%" % (len(bank), 100.0 * bank["default_flag"].mean())) print("征信侧样本 %d 行" % len(bureau)) print("真实交集 %d 行" % inter) print(bank_path) print(bureau_path) if __name__ == "__main__": main()