""" Bank Account Ownership in Africa - Dataset Generator DAG Structure: country → banking_infrastructure, income_quintile urban_rural → branch_access, atm_access gender → account_ownership age → account_ownership, employment_status income_quintile → account_ownership, account_balance employment_status → account_ownership account_ownership → account_type, usage_frequency branch_access → account_ownership Parameter Evidence Table: | Parameter | Value | Source | |-----------|-------|--------| | SSA account ownership | 49% (2021) | World Bank Findex 2021 | | Kenya bank account | 40% | World Bank Findex 2025 | | Nigeria bank account | 45% | World Bank Findex 2025 | | South Africa bank account | 85% | World Bank Findex 2025 | | Ghana bank account | 62% | World Bank Findex 2025 | | Uganda bank account | 29% | World Bank Findex 2025 | | Urban-rural gap | 20-30% | World Bank Findex 2025 | | Gender gap | 12% | World Bank Findex 2025 | | Top barrier: cost | 29% | World Bank Findex 2025 | | Top barrier: documentation | 18% | World Bank Findex 2025 | | Top barrier: distance | 15% | World Bank Findex 2025 | | Dormant account rate | 25-35% | World Bank Findex 2025 | """ import numpy as np import pandas as pd from pathlib import Path COUNTRIES = [ "Kenya", "Uganda", "Nigeria", "Ghana", "Tanzania", "Ethiopia", "Malawi", "Zambia", "Senegal", "Rwanda", "Niger", "Mali", "DRC", "Mozambique", "South Africa" ] YEARS = list(range(2018, 2026)) COUNTRY_BANK_RATES = { "Kenya": 0.40, "Uganda": 0.29, "Tanzania": 0.32, "Ghana": 0.62, "Nigeria": 0.45, "Ethiopia": 0.35, "Malawi": 0.27, "Zambia": 0.38, "Senegal": 0.42, "Rwanda": 0.36, "Niger": 0.15, "Mali": 0.25, "DRC": 0.18, "Mozambique": 0.22, "South Africa": 0.85 } ACCOUNT_TYPES = ["savings", "current", "fixed_deposit", "salary_account"] BARRIERS = ["cost", "documentation", "distance", "trust", "religious", "no_need", "other"] BANK_NAMES = { "Kenya": ["Equity Bank", "KCB", "NCBA", "Stanbic", "Cooperative Bank"], "Uganda": ["Stanbic", "Centenary", "Equity Bank", "DFCU", "KCB"], "Nigeria": ["GTBank", "First Bank", "Access Bank", "UBA", "Zenith Bank"], "Ghana": ["Ecobank", "GCB", "Stanbic", "Fidelity", "Absa"], "Tanzania": ["CRDB", "NMB", "Stanbic", "NBC", "Absa"], "Ethiopia": ["Commercial Bank of Ethiopia", "Awash Bank", "Dashen Bank"], "Malawi": ["National Bank", "Standard Bank", "NBS Bank", "FDH Bank"], "Zambia": ["Stanbic", "Zanaco", "Absa", "First National Bank"], "Senegal": ["Société Générale", "Ecobank", "CBAO", "BHS"], "Rwanda": ["Bank of Kigali", "Ecobank", "I&M Bank", "Equity Bank"], "Niger": ["Sonibank", "BAK", "Ecobank", "Orabank"], "Mali": ["BDM", "BMCD", "Ecobank", "Coris Bank"], "DRC": ["Rawbank", "BCDC", "Equity BCDC", "FBNBank"], "Mozambique": ["Millennium BIM", "Standard Bank", "BCI", "Moza Banco"], "South Africa": ["Standard Bank", "FNB", "Absa", "Nedbank", "Capitec"] } def generate_scenario(n_samples: int, seed: int, scenario: str) -> pd.DataFrame: rng = np.random.default_rng(seed) country = rng.choice(COUNTRIES, n_samples) year = rng.choice(YEARS, n_samples) urban_rural = np.where(rng.random(n_samples) < 0.42, "urban", "rural") gender = rng.choice(["male", "female"], n_samples, p=[0.48, 0.52]) age = rng.integers(18, 75, n_samples) age_group = pd.cut(age, bins=[17, 25, 35, 45, 55, 75], labels=["18-24", "25-34", "35-44", "45-54", "55+"]) income_quintile = rng.choice(["lowest", "second", "middle", "fourth", "highest"], n_samples, p=[0.25, 0.22, 0.20, 0.18, 0.15]) education = rng.choice( ["none", "primary", "secondary", "tertiary"], n_samples, p=[0.18, 0.32, 0.35, 0.15] ) employment_status = rng.choice( ["employed_formal", "employed_informal", "self_employed", "unemployed", "student"], n_samples, p=[0.15, 0.28, 0.25, 0.22, 0.10] ) branch_access_km = np.where( urban_rural == "urban", rng.exponential(3, n_samples), rng.exponential(15, n_samples) ) atm_access_km = np.where( urban_rural == "urban", rng.exponential(1.5, n_samples), rng.exponential(8, n_samples) ) base_rates = np.array([COUNTRY_BANK_RATES[c] for c in country]) year_adj = (year - 2018) * 0.015 urban_adj = np.where(urban_rural == "urban", 0.20, 0.0) gender_adj = np.where(gender == "female", -0.12, 0.0) age_adj = np.where((age >= 25) & (age <= 54), 0.08, -0.04) quintile_adj = np.select( [income_quintile == "lowest", income_quintile == "second", income_quintile == "middle", income_quintile == "fourth", income_quintile == "highest"], [-0.25, -0.12, 0.0, 0.12, 0.25] ) educ_adj = np.select( [education == "none", education == "primary", education == "secondary", education == "tertiary"], [-0.20, -0.05, 0.08, 0.18] ) emp_adj = np.select( [employment_status == "employed_formal", employment_status == "employed_informal", employment_status == "self_employed", employment_status == "unemployed", employment_status == "student"], [0.25, 0.05, 0.10, -0.15, 0.05] ) distance_adj = np.where(branch_access_km > 10, -0.15, 0.0) ownership_prob = np.clip( base_rates + year_adj + urban_adj + gender_adj + age_adj + quintile_adj + educ_adj + emp_adj + distance_adj, 0.02, 0.98 ) account_ownership = (rng.random(n_samples) < ownership_prob).astype(int) account_type = np.empty(n_samples, dtype=object) account_type[account_ownership == 0] = "none" own_mask = account_ownership == 1 account_type[own_mask] = rng.choice( ACCOUNT_TYPES, own_mask.sum(), p=[0.55, 0.25, 0.08, 0.12] ) bank_name = np.empty(n_samples, dtype=object) bank_name[account_ownership == 0] = "none" for i, c in enumerate(country): if account_ownership[i] == 1: bank_name[i] = rng.choice(BANK_NAMES[c]) usage_frequency = np.zeros(n_samples, dtype=int) usage_frequency[own_mask] = rng.choice( [0, 1, 2, 3, 4], own_mask.sum(), p=[0.25, 0.30, 0.25, 0.12, 0.08] ) months_inactive = np.zeros(n_samples, dtype=int) inactive_mask = (account_ownership == 1) & (usage_frequency == 0) months_inactive[inactive_mask] = rng.integers(1, 25, inactive_mask.sum()) account_balance = np.zeros(n_samples) active_mask = (account_ownership == 1) & (usage_frequency > 0) account_balance[active_mask] = rng.lognormal( mean=np.log(500) + np.select( [income_quintile[active_mask] == "lowest", income_quintile[active_mask] == "second", income_quintile[active_mask] == "middle", income_quintile[active_mask] == "fourth", income_quintile[active_mask] == "highest"], [-1.0, -0.4, 0.0, 0.5, 1.2] ), sigma=0.8, size=active_mask.sum() ) primary_barrier = np.empty(n_samples, dtype=object) primary_barrier[account_ownership == 1] = "none" barrier_mask = account_ownership == 0 primary_barrier[barrier_mask] = rng.choice( BARRIERS, barrier_mask.sum(), p=[0.29, 0.18, 0.15, 0.10, 0.05, 0.18, 0.05] ) has_overdraft = np.zeros(n_samples, dtype=int) has_overdraft[own_mask] = (rng.random(own_mask.sum()) < 0.15).astype(int) has_debit_card = np.zeros(n_samples, dtype=int) has_debit_card[own_mask] = (rng.random(own_mask.sum()) < 0.45).astype(int) has_online_banking = np.zeros(n_samples, dtype=int) has_online_banking[own_mask] = ( rng.random(own_mask.sum()) < np.where(urban_rural[own_mask] == "urban", 0.35, 0.15) ).astype(int) df = pd.DataFrame({ "country": country, "year": year, "urban_rural": urban_rural, "gender": gender, "age": age, "age_group": age_group, "income_quintile": income_quintile, "education": education, "employment_status": employment_status, "branch_access_km": np.round(branch_access_km, 1), "atm_access_km": np.round(atm_access_km, 1), "account_ownership": account_ownership, "account_type": account_type, "bank_name": bank_name, "usage_frequency_monthly": usage_frequency, "months_inactive": months_inactive, "account_balance_usd": np.round(account_balance, 2), "primary_barrier": primary_barrier, "has_overdraft": has_overdraft, "has_debit_card": has_debit_card, "has_online_banking": has_online_banking, "scenario": scenario }) return df def main(): output_dir = Path(__file__).parent scenarios = [ ("low_burden", 4000, 42), ("moderate_burden", 5000, 43), ("high_burden", 6000, 44) ] all_dfs = [] for scenario_name, n, seed in scenarios: df = generate_scenario(n, seed, scenario_name) all_dfs.append(df) output_path = output_dir / f"bank_account_{scenario_name}.csv" df.to_csv(output_path, index=False) print(f"Generated {output_path}: {len(df)} records") combined = pd.concat(all_dfs, ignore_index=True) combined_path = output_dir / "bank_account_combined.csv" combined.to_csv(combined_path, index=False) print(f"Generated {combined_path}: {len(combined)} records") if __name__ == "__main__": main()