Datasets:
Upload folder using huggingface_hub
Browse files- README.md +207 -0
- data/all_scenarios.csv +0 -0
- data/baseline.csv +0 -0
- data/debt_consolidation.csv +0 -0
- data/debt_distress.csv +0 -0
- data/metadata.json +49 -0
- data/plots/diagnostic_plots.png +3 -0
- generate_dataset.py +463 -0
- requirements.txt +4 -0
- validate_dataset.py +273 -0
README.md
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
pretty_name: African Public Debt Management
|
| 3 |
+
license: cc-by-4.0
|
| 4 |
+
task_categories:
|
| 5 |
+
- tabular-classification
|
| 6 |
+
- tabular-regression
|
| 7 |
+
tags:
|
| 8 |
+
- governance
|
| 9 |
+
- public-debt
|
| 10 |
+
- fiscal-policy
|
| 11 |
+
- sub-saharan-africa
|
| 12 |
+
- synthetic
|
| 13 |
+
- lmic
|
| 14 |
+
- debt-sustainability
|
| 15 |
+
- macroeconomics
|
| 16 |
+
- development-economics
|
| 17 |
+
- debt-management
|
| 18 |
+
- africa
|
| 19 |
+
- imf
|
| 20 |
+
- world-bank
|
| 21 |
+
size_categories:
|
| 22 |
+
- 10K<n<100K
|
| 23 |
+
configs:
|
| 24 |
+
- config_name: baseline
|
| 25 |
+
data_files: data/baseline.csv
|
| 26 |
+
- config_name: debt_consolidation
|
| 27 |
+
data_files: data/debt_consolidation.csv
|
| 28 |
+
- config_name: debt_distress
|
| 29 |
+
data_files: data/debt_distress.csv
|
| 30 |
+
dataset_info:
|
| 31 |
+
description: |
|
| 32 |
+
Synthetic dataset of public debt management indicators for 12 Sub-Saharan African
|
| 33 |
+
countries across 3 policy scenarios, parameterized from IMF, World Bank, and AfDB
|
| 34 |
+
reports.
|
| 35 |
+
features:
|
| 36 |
+
- name: country
|
| 37 |
+
dtype: string
|
| 38 |
+
description: Country name
|
| 39 |
+
- name: year
|
| 40 |
+
dtype: int16
|
| 41 |
+
description: Observation year
|
| 42 |
+
- name: scenario
|
| 43 |
+
dtype: string
|
| 44 |
+
description: Policy scenario (baseline, debt_consolidation, debt_distress)
|
| 45 |
+
- name: debt_to_gdp_pct
|
| 46 |
+
dtype: float32
|
| 47 |
+
description: Total government debt as percentage of GDP
|
| 48 |
+
- name: external_debt_pct
|
| 49 |
+
dtype: float32
|
| 50 |
+
description: Share of total debt owed to external creditors (%)
|
| 51 |
+
- name: domestic_debt_pct
|
| 52 |
+
dtype: float32
|
| 53 |
+
description: Share of total debt held by domestic creditors (%)
|
| 54 |
+
- name: debt_service_ratio_pct
|
| 55 |
+
dtype: float32
|
| 56 |
+
description: Debt service payments as percentage of government revenue
|
| 57 |
+
- name: concessional_debt_pct
|
| 58 |
+
dtype: float32
|
| 59 |
+
description: Share of external debt on concessional terms (%)
|
| 60 |
+
- name: average_maturity_years
|
| 61 |
+
dtype: float32
|
| 62 |
+
description: Weighted average remaining maturity of debt stock (years)
|
| 63 |
+
- name: average_interest_rate_pct
|
| 64 |
+
dtype: float32
|
| 65 |
+
description: Weighted average effective interest rate on debt stock (%)
|
| 66 |
+
- name: debt_sustainability_rating
|
| 67 |
+
dtype: int8
|
| 68 |
+
description: Composite debt sustainability rating (1=low risk, 5=debt distress)
|
| 69 |
+
- name: refinancing_risk_score
|
| 70 |
+
dtype: float32
|
| 71 |
+
description: Refinancing risk score (0-100, higher = greater risk)
|
| 72 |
+
- name: primary_balance_pct
|
| 73 |
+
dtype: float32
|
| 74 |
+
description: Primary fiscal balance as percentage of GDP (positive = surplus)
|
| 75 |
+
- name: fiscal_space_score
|
| 76 |
+
dtype: float32
|
| 77 |
+
description: Fiscal space score (0-100, higher = more fiscal room)
|
| 78 |
+
- name: gdp_growth_pct
|
| 79 |
+
dtype: float32
|
| 80 |
+
description: Real GDP growth rate (%)
|
| 81 |
+
- name: inflation_pct
|
| 82 |
+
dtype: float32
|
| 83 |
+
description: Consumer price inflation (%)
|
| 84 |
+
- name: reserves_months_imports
|
| 85 |
+
dtype: float32
|
| 86 |
+
description: Foreign exchange reserves in months of import cover
|
| 87 |
+
- name: revenue_to_gdp_pct
|
| 88 |
+
dtype: float32
|
| 89 |
+
description: Government revenue as percentage of GDP
|
| 90 |
+
- name: expenditure_to_gdp_pct
|
| 91 |
+
dtype: float32
|
| 92 |
+
description: Government expenditure as percentage of GDP
|
| 93 |
+
---
|
| 94 |
+
|
| 95 |
+
# African Public Debt Management Dataset
|
| 96 |
+
|
| 97 |
+
## Overview
|
| 98 |
+
|
| 99 |
+
This dataset provides synthetic public debt management indicators for **12 Sub-Saharan African (SSA) countries** across **3 policy scenarios** (baseline, debt consolidation, debt distress), totaling **29,988 records** with **19 variables** per record.
|
| 100 |
+
|
| 101 |
+
The data is parameterized from published indicators and stylized facts reported by the **IMF**, **World Bank**, **AfDB**, and **UNCTAD**, capturing realistic distributions of debt-to-GDP ratios, debt composition, debt service burdens, concessional financing shares, and macroeconomic fundamentals.
|
| 102 |
+
|
| 103 |
+
## Countries
|
| 104 |
+
|
| 105 |
+
| Country | Region | Debt Profile |
|
| 106 |
+
|---------|--------|-------------|
|
| 107 |
+
| Nigeria | West Africa | Moderate debt/GDP (~40-55%), high service-to-revenue, Eurobond-dependent |
|
| 108 |
+
| Kenya | East Africa | Elevated debt/GDP (~67%), refinancing pressure, infrastructure-driven |
|
| 109 |
+
| Ghana | West Africa | Post-restructuring (~44%), IMF program, was in debt distress (>80%) |
|
| 110 |
+
| South Africa | Southern Africa | High debt/GDP (~78%), predominantly domestic, SOE risks |
|
| 111 |
+
| DRC | Central Africa | Low debt/GDP (~24%), heavily concessional, IDA-eligible |
|
| 112 |
+
| Ethiopia | East Africa | Low-moderate (~28%), restructuring under G20 Common Framework |
|
| 113 |
+
| Tanzania | East Africa | Moderate (~46%), concessional-heavy, fiscal discipline |
|
| 114 |
+
| Rwanda | East Africa | Rising rapidly (~85%), concessional-heavy, ambitious spending |
|
| 115 |
+
| Mozambique | Southern Africa | Very high (~97%), debt distress, hidden debt legacy |
|
| 116 |
+
| Zambia | Southern Africa | Post-restructuring (~70%), was in debt distress |
|
| 117 |
+
| Senegal | West Africa | Rising (~65%), oil/gas transition, infrastructure investment |
|
| 118 |
+
| Côte d'Ivoire | West Africa | Moderate (~56%), infrastructure-driven, first IDA Eurobond issuer |
|
| 119 |
+
|
| 120 |
+
## Scenarios
|
| 121 |
+
|
| 122 |
+
### Baseline
|
| 123 |
+
Current trajectory reflecting prevailing fiscal policies, debt composition, and macroeconomic conditions. Debt-to-GDP ratios average ~58%, with debt service consuming ~25-30% of revenue. Years: 2018–2025.
|
| 124 |
+
|
| 125 |
+
### Debt Consolidation
|
| 126 |
+
Favorable policy path: fiscal tightening, extended maturities, increased concessional borrowing, lower interest rates, stronger growth. Debt-to-GDP falls ~15%, debt service ratio drops ~25%. Years: 2022–2030.
|
| 127 |
+
|
| 128 |
+
### Debt Distress
|
| 129 |
+
Adverse scenario: rising debt stocks, refinancing difficulties, loss of concessional access, higher interest rates, weaker growth, rising inflation. Debt-to-GDP rises ~25%, debt service ratio increases ~50%. Years: 2020–2028.
|
| 130 |
+
|
| 131 |
+
## Variable Descriptions
|
| 132 |
+
|
| 133 |
+
### Core Debt Indicators
|
| 134 |
+
- **debt_to_gdp_pct**: Total government debt as percentage of GDP. World Bank/IMF threshold of 55% used for low-income countries; many SSA countries exceed this.
|
| 135 |
+
- **external_debt_pct**: Share of total public debt owed to external creditors (multilateral, bilateral, private). SSA average ~42% of total debt stock.
|
| 136 |
+
- **domestic_debt_pct**: Share held by domestic creditors (banks, pension funds, central bank). Complement of external_debt_pct.
|
| 137 |
+
- **debt_service_ratio_pct**: Total debt service (principal + interest) as percentage of government revenue. SSA median ~18.7% for external debt service alone (UNCTAD 2024); many countries exceed 30%.
|
| 138 |
+
|
| 139 |
+
### Debt Composition & Terms
|
| 140 |
+
- **concessional_debt_pct**: Share of external debt on concessional terms (below-market interest, long grace periods). IDA-eligible countries typically >50%; market-accessing countries <30%.
|
| 141 |
+
- **average_maturity_years**: Weighted average remaining maturity. Shorter maturity = higher rollover risk. Concessional loans: 15-30 years; Eurobonds: 5-10 years.
|
| 142 |
+
- **average_interest_rate_pct**: Weighted average effective rate. Concessional: 0.5-2%; commercial: 5-10%; Eurobonds: 6-12%.
|
| 143 |
+
|
| 144 |
+
### Risk & Sustainability
|
| 145 |
+
- **debt_sustainability_rating**: Composite rating 1-5 based on debt-to-GDP, debt service ratio, concessionality, and interest rates. 1=low risk, 3=moderate risk, 5=debt distress. Based on IMF-World Bank DSF methodology.
|
| 146 |
+
- **refinancing_risk_score**: Score 0-100 capturing rollover risk from maturity structure, debt level, and concessionality.
|
| 147 |
+
- **fiscal_space_score**: Score 0-100 capturing room for counter-cyclical spending based on primary balance, debt level, revenue mobilization, and debt service burden.
|
| 148 |
+
|
| 149 |
+
### Fiscal & Macro
|
| 150 |
+
- **primary_balance_pct**: Fiscal balance excluding interest payments (% of GDP). Negative = deficit.
|
| 151 |
+
- **gdp_growth_pct**: Real GDP growth. SSA average ~3.5-4% pre-COVID, recovering post-2020.
|
| 152 |
+
- **inflation_pct**: Consumer price inflation. Varies widely: 3-5% (WAEMU) to 20%+ (Ethiopia, Ghana).
|
| 153 |
+
- **reserves_months_imports**: Foreign exchange reserves in months of import cover. <3 months = vulnerability threshold.
|
| 154 |
+
- **revenue_to_gdp_pct**: Government revenue/GDP. SSA average ~16%, well below OECD ~34%. Critical constraint.
|
| 155 |
+
- **expenditure_to_gdp_pct**: Government expenditure/GDP.
|
| 156 |
+
|
| 157 |
+
## Data Sources & Parameterization
|
| 158 |
+
|
| 159 |
+
Parameters derived from:
|
| 160 |
+
|
| 161 |
+
1. **IMF Regional Economic Outlook: Sub-Saharan Africa** (April 2025) — median debt-to-GDP <60%, debt stabilization trends
|
| 162 |
+
2. **World Bank International Debt Report 2025** — LMIC external debt stocks, service ratios, creditor composition
|
| 163 |
+
3. **AfDB "State of Play of Debt Burden in Africa 2024"** — debt sustainability assessments, liquidity indicators
|
| 164 |
+
4. **UNCTAD external debt sustainability data** — SSA debt service at 18.7% of revenue (3× 2014 levels)
|
| 165 |
+
5. **ONE Data "African Debt"** (Feb 2025) — 21 low-income African countries in/at risk of debt distress
|
| 166 |
+
6. **Debt Service Watch 2024** — SSA spending 55% of revenue on debt service
|
| 167 |
+
|
| 168 |
+
Key stylized facts embedded in parameters:
|
| 169 |
+
- SSA aggregate debt-to-GDP ~61% (2024), with wide country dispersion (24% DRC to 97% Mozambique)
|
| 170 |
+
- Debt service consuming 15-55% of government revenue across the region
|
| 171 |
+
- Commercial debt share rising from 20% to 43% of total since 2000 (AfDB)
|
| 172 |
+
- 25+ African countries carrying excess debt or high risk (AfDB)
|
| 173 |
+
- Concessional financing declining as countries access capital markets
|
| 174 |
+
|
| 175 |
+
## Intended Use
|
| 176 |
+
|
| 177 |
+
- **Debt sustainability analysis** — Train models to predict debt distress risk
|
| 178 |
+
- **Fiscal policy simulation** — Evaluate consolidation vs. expansionary scenarios
|
| 179 |
+
- **Macroeconomic forecasting** — Model debt trajectory under different growth/inflation assumptions
|
| 180 |
+
- **Development finance research** — Study concessional vs. commercial borrowing tradeoffs
|
| 181 |
+
- **ML benchmarking** — Classification (sustainability rating), regression (debt ratios), scenario analysis
|
| 182 |
+
|
| 183 |
+
## Limitations
|
| 184 |
+
|
| 185 |
+
- **Synthetic data**: Not a substitute for official national accounts or IMF Article IV data
|
| 186 |
+
- **Cross-sectional stylization**: Each row is an independent draw, not a time series per country
|
| 187 |
+
- **No bilateral/multilateral creditor-level detail**: Concessional share is aggregate
|
| 188 |
+
- **No currency composition**: Exchange rate effects modeled implicitly through scenario shifts
|
| 189 |
+
- **No domestic debt detail**: Treasury bills, bonds, and central bank financing not disaggregated
|
| 190 |
+
|
| 191 |
+
## Citation
|
| 192 |
+
|
| 193 |
+
If you use this dataset, please cite:
|
| 194 |
+
|
| 195 |
+
```
|
| 196 |
+
@dataset{african_public_debt_management_2026,
|
| 197 |
+
title={African Public Debt Management Synthetic Dataset},
|
| 198 |
+
author={Electric Sheep Africa},
|
| 199 |
+
year={2026},
|
| 200 |
+
url={https://huggingface.co/datasets/electricsheepafrica/african-public-debt-management},
|
| 201 |
+
license={CC-BY-4.0}
|
| 202 |
+
}
|
| 203 |
+
```
|
| 204 |
+
|
| 205 |
+
## License
|
| 206 |
+
|
| 207 |
+
CC BY 4.0 — You may share and adapt with attribution.
|
data/all_scenarios.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
data/baseline.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
data/debt_consolidation.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
data/debt_distress.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
data/metadata.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "African Public Debt Management",
|
| 3 |
+
"description": "Synthetic dataset of public debt indicators for 12 Sub-Saharan African countries",
|
| 4 |
+
"n_countries": 12,
|
| 5 |
+
"countries": [
|
| 6 |
+
"Cote d'Ivoire",
|
| 7 |
+
"DRC",
|
| 8 |
+
"Ethiopia",
|
| 9 |
+
"Ghana",
|
| 10 |
+
"Kenya",
|
| 11 |
+
"Mozambique",
|
| 12 |
+
"Nigeria",
|
| 13 |
+
"Rwanda",
|
| 14 |
+
"Senegal",
|
| 15 |
+
"South Africa",
|
| 16 |
+
"Tanzania",
|
| 17 |
+
"Zambia"
|
| 18 |
+
],
|
| 19 |
+
"scenarios": [
|
| 20 |
+
"baseline",
|
| 21 |
+
"debt_consolidation",
|
| 22 |
+
"debt_distress"
|
| 23 |
+
],
|
| 24 |
+
"n_records_per_scenario": 10000,
|
| 25 |
+
"n_total_records": 29988,
|
| 26 |
+
"n_variables": 19,
|
| 27 |
+
"columns": [
|
| 28 |
+
"country",
|
| 29 |
+
"year",
|
| 30 |
+
"scenario",
|
| 31 |
+
"debt_to_gdp_pct",
|
| 32 |
+
"external_debt_pct",
|
| 33 |
+
"domestic_debt_pct",
|
| 34 |
+
"debt_service_ratio_pct",
|
| 35 |
+
"concessional_debt_pct",
|
| 36 |
+
"average_maturity_years",
|
| 37 |
+
"average_interest_rate_pct",
|
| 38 |
+
"debt_sustainability_rating",
|
| 39 |
+
"refinancing_risk_score",
|
| 40 |
+
"primary_balance_pct",
|
| 41 |
+
"fiscal_space_score",
|
| 42 |
+
"gdp_growth_pct",
|
| 43 |
+
"inflation_pct",
|
| 44 |
+
"reserves_months_imports",
|
| 45 |
+
"revenue_to_gdp_pct",
|
| 46 |
+
"expenditure_to_gdp_pct"
|
| 47 |
+
],
|
| 48 |
+
"seed": 42
|
| 49 |
+
}
|
data/plots/diagnostic_plots.png
ADDED
|
Git LFS Details
|
generate_dataset.py
ADDED
|
@@ -0,0 +1,463 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
African Public Debt Management Synthetic Dataset Generator
|
| 3 |
+
|
| 4 |
+
Generates realistic synthetic data for 12 Sub-Saharan African countries across
|
| 5 |
+
3 policy scenarios, parameterized from IMF, World Bank, and AfDB reports.
|
| 6 |
+
|
| 7 |
+
Sources informing parameters:
|
| 8 |
+
- IMF Regional Economic Outlook: Sub-Saharan Africa (April 2025)
|
| 9 |
+
- World Bank International Debt Report 2025
|
| 10 |
+
- AfDB "State of Play of Debt Burden in Africa 2024"
|
| 11 |
+
- UNCTAD external debt sustainability data
|
| 12 |
+
- ONE Data "African Debt" (Feb 2025)
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import argparse
|
| 16 |
+
import json
|
| 17 |
+
import os
|
| 18 |
+
from pathlib import Path
|
| 19 |
+
|
| 20 |
+
import numpy as np
|
| 21 |
+
import pandas as pd
|
| 22 |
+
from scipy import stats
|
| 23 |
+
|
| 24 |
+
SEED = 42
|
| 25 |
+
N_RECORDS_PER_SCENARIO = 10_000
|
| 26 |
+
|
| 27 |
+
# ── Country parameters (literature-informed) ──────────────────────────────
|
| 28 |
+
# Each dict: debt_to_gdp (mean, std), external_pct (mean, std),
|
| 29 |
+
# debt_service_ratio (mean, std), concessional_pct (mean, std),
|
| 30 |
+
# avg_maturity_years (mean, std), avg_interest_rate (mean, std),
|
| 31 |
+
# primary_balance_pct (mean, std), gdp_growth_pct (mean, std),
|
| 32 |
+
# inflation_pct (mean, std), reserves_months_imports (mean, std),
|
| 33 |
+
# revenue_gdp_pct (mean, std), expenditure_gdp_pct (mean, std)
|
| 34 |
+
#
|
| 35 |
+
# References:
|
| 36 |
+
# Nigeria ~39-55% debt/GDP, external share rising, high service/revenue
|
| 37 |
+
# Kenya ~67% debt/GDP, Eurobond-heavy, refinancing pressure
|
| 38 |
+
# Ghana ~44% post-restructuring (was 80%+), IMF program, debt distress
|
| 39 |
+
# South Africa ~78-79%, predominantly domestic, SOE contingent liabilities
|
| 40 |
+
# DRC ~26%, heavily concessional, IDA-eligible
|
| 41 |
+
# Ethiopia ~28%, restructuring under Common Framework
|
| 42 |
+
# Tanzania ~46%, moderate debt, strong fiscal discipline
|
| 43 |
+
# Rwanda ~85%, rising rapidly, concessional-heavy
|
| 44 |
+
# Mozambique ~97%, debt distress, hidden debt scandal legacy
|
| 45 |
+
# Zambia ~restructured under Common Framework, ~80% pre-restructuring
|
| 46 |
+
# Senegal ~55-100% (oil/gas transition), rising
|
| 47 |
+
# Côte d'Ivoire ~55%, infrastructure-driven borrowing
|
| 48 |
+
|
| 49 |
+
COUNTRY_PARAMS = {
|
| 50 |
+
"Nigeria": {
|
| 51 |
+
"debt_to_gdp": (48.0, 6.0),
|
| 52 |
+
"external_pct": (42.0, 8.0),
|
| 53 |
+
"debt_service_ratio": (28.0, 5.0),
|
| 54 |
+
"concessional_pct": (30.0, 8.0),
|
| 55 |
+
"avg_maturity_years": (8.5, 2.0),
|
| 56 |
+
"avg_interest_rate": (7.5, 1.5),
|
| 57 |
+
"primary_balance_pct": (-2.5, 1.5),
|
| 58 |
+
"gdp_growth_pct": (3.0, 1.2),
|
| 59 |
+
"inflation_pct": (18.0, 4.0),
|
| 60 |
+
"reserves_months_imports": (7.0, 1.5),
|
| 61 |
+
"revenue_gdp_pct": (7.5, 1.5),
|
| 62 |
+
"expenditure_gdp_pct": (12.0, 2.0),
|
| 63 |
+
},
|
| 64 |
+
"Kenya": {
|
| 65 |
+
"debt_to_gdp": (68.0, 7.0),
|
| 66 |
+
"external_pct": (52.0, 8.0),
|
| 67 |
+
"debt_service_ratio": (32.0, 6.0),
|
| 68 |
+
"concessional_pct": (35.0, 8.0),
|
| 69 |
+
"avg_maturity_years": (7.0, 2.0),
|
| 70 |
+
"avg_interest_rate": (8.0, 1.8),
|
| 71 |
+
"primary_balance_pct": (-3.5, 1.5),
|
| 72 |
+
"gdp_growth_pct": (5.0, 1.0),
|
| 73 |
+
"inflation_pct": (7.0, 2.5),
|
| 74 |
+
"reserves_months_imports": (4.5, 1.0),
|
| 75 |
+
"revenue_gdp_pct": (16.0, 2.0),
|
| 76 |
+
"expenditure_gdp_pct": (22.0, 2.5),
|
| 77 |
+
},
|
| 78 |
+
"Ghana": {
|
| 79 |
+
"debt_to_gdp": (55.0, 10.0),
|
| 80 |
+
"external_pct": (48.0, 10.0),
|
| 81 |
+
"debt_service_ratio": (40.0, 8.0),
|
| 82 |
+
"concessional_pct": (28.0, 10.0),
|
| 83 |
+
"avg_maturity_years": (6.5, 2.0),
|
| 84 |
+
"avg_interest_rate": (9.0, 2.0),
|
| 85 |
+
"primary_balance_pct": (-4.0, 2.0),
|
| 86 |
+
"gdp_growth_pct": (4.5, 1.5),
|
| 87 |
+
"inflation_pct": (22.0, 6.0),
|
| 88 |
+
"reserves_months_imports": (3.0, 1.0),
|
| 89 |
+
"revenue_gdp_pct": (13.0, 2.0),
|
| 90 |
+
"expenditure_gdp_pct": (20.0, 3.0),
|
| 91 |
+
},
|
| 92 |
+
"South Africa": {
|
| 93 |
+
"debt_to_gdp": (77.0, 5.0),
|
| 94 |
+
"external_pct": (12.0, 4.0),
|
| 95 |
+
"debt_service_ratio": (18.0, 3.0),
|
| 96 |
+
"concessional_pct": (8.0, 4.0),
|
| 97 |
+
"avg_maturity_years": (14.0, 3.0),
|
| 98 |
+
"avg_interest_rate": (7.0, 1.0),
|
| 99 |
+
"primary_balance_pct": (-4.5, 1.0),
|
| 100 |
+
"gdp_growth_pct": (1.5, 1.0),
|
| 101 |
+
"inflation_pct": (5.5, 1.5),
|
| 102 |
+
"reserves_months_imports": (5.0, 1.0),
|
| 103 |
+
"revenue_gdp_pct": (27.0, 2.0),
|
| 104 |
+
"expenditure_gdp_pct": (33.0, 2.5),
|
| 105 |
+
},
|
| 106 |
+
"DRC": {
|
| 107 |
+
"debt_to_gdp": (24.0, 5.0),
|
| 108 |
+
"external_pct": (75.0, 10.0),
|
| 109 |
+
"debt_service_ratio": (8.0, 3.0),
|
| 110 |
+
"concessional_pct": (80.0, 8.0),
|
| 111 |
+
"avg_maturity_years": (20.0, 4.0),
|
| 112 |
+
"avg_interest_rate": (2.5, 1.0),
|
| 113 |
+
"primary_balance_pct": (1.0, 2.0),
|
| 114 |
+
"gdp_growth_pct": (6.5, 2.0),
|
| 115 |
+
"inflation_pct": (12.0, 5.0),
|
| 116 |
+
"reserves_months_imports": (1.5, 0.8),
|
| 117 |
+
"revenue_gdp_pct": (12.0, 2.5),
|
| 118 |
+
"expenditure_gdp_pct": (14.0, 3.0),
|
| 119 |
+
},
|
| 120 |
+
"Ethiopia": {
|
| 121 |
+
"debt_to_gdp": (30.0, 5.0),
|
| 122 |
+
"external_pct": (58.0, 10.0),
|
| 123 |
+
"debt_service_ratio": (12.0, 4.0),
|
| 124 |
+
"concessional_pct": (55.0, 10.0),
|
| 125 |
+
"avg_maturity_years": (15.0, 4.0),
|
| 126 |
+
"avg_interest_rate": (3.5, 1.5),
|
| 127 |
+
"primary_balance_pct": (-1.5, 2.0),
|
| 128 |
+
"gdp_growth_pct": (6.0, 2.0),
|
| 129 |
+
"inflation_pct": (20.0, 8.0),
|
| 130 |
+
"reserves_months_imports": (2.0, 0.8),
|
| 131 |
+
"revenue_gdp_pct": (10.0, 2.0),
|
| 132 |
+
"expenditure_gdp_pct": (14.0, 2.5),
|
| 133 |
+
},
|
| 134 |
+
"Tanzania": {
|
| 135 |
+
"debt_to_gdp": (42.0, 5.0),
|
| 136 |
+
"external_pct": (48.0, 8.0),
|
| 137 |
+
"debt_service_ratio": (15.0, 4.0),
|
| 138 |
+
"concessional_pct": (55.0, 10.0),
|
| 139 |
+
"avg_maturity_years": (14.0, 3.0),
|
| 140 |
+
"avg_interest_rate": (3.5, 1.2),
|
| 141 |
+
"primary_balance_pct": (-1.0, 1.5),
|
| 142 |
+
"gdp_growth_pct": (5.5, 1.0),
|
| 143 |
+
"inflation_pct": (4.5, 2.0),
|
| 144 |
+
"reserves_months_imports": (5.0, 1.0),
|
| 145 |
+
"revenue_gdp_pct": (15.0, 2.0),
|
| 146 |
+
"expenditure_gdp_pct": (18.0, 2.0),
|
| 147 |
+
},
|
| 148 |
+
"Rwanda": {
|
| 149 |
+
"debt_to_gdp": (72.0, 8.0),
|
| 150 |
+
"external_pct": (62.0, 8.0),
|
| 151 |
+
"debt_service_ratio": (18.0, 5.0),
|
| 152 |
+
"concessional_pct": (60.0, 10.0),
|
| 153 |
+
"avg_maturity_years": (16.0, 3.0),
|
| 154 |
+
"avg_interest_rate": (3.0, 1.0),
|
| 155 |
+
"primary_balance_pct": (-5.0, 2.0),
|
| 156 |
+
"gdp_growth_pct": (7.5, 1.5),
|
| 157 |
+
"inflation_pct": (7.0, 3.0),
|
| 158 |
+
"reserves_months_imports": (4.0, 1.0),
|
| 159 |
+
"revenue_gdp_pct": (16.0, 2.0),
|
| 160 |
+
"expenditure_gdp_pct": (24.0, 3.0),
|
| 161 |
+
},
|
| 162 |
+
"Mozambique": {
|
| 163 |
+
"debt_to_gdp": (95.0, 8.0),
|
| 164 |
+
"external_pct": (65.0, 10.0),
|
| 165 |
+
"debt_service_ratio": (22.0, 5.0),
|
| 166 |
+
"concessional_pct": (40.0, 12.0),
|
| 167 |
+
"avg_maturity_years": (10.0, 3.0),
|
| 168 |
+
"avg_interest_rate": (5.0, 1.5),
|
| 169 |
+
"primary_balance_pct": (-3.0, 2.0),
|
| 170 |
+
"gdp_growth_pct": (4.0, 2.0),
|
| 171 |
+
"inflation_pct": (10.0, 4.0),
|
| 172 |
+
"reserves_months_imports": (3.5, 1.0),
|
| 173 |
+
"revenue_gdp_pct": (25.0, 3.0),
|
| 174 |
+
"expenditure_gdp_pct": (30.0, 3.0),
|
| 175 |
+
},
|
| 176 |
+
"Zambia": {
|
| 177 |
+
"debt_to_gdp": (70.0, 10.0),
|
| 178 |
+
"external_pct": (58.0, 10.0),
|
| 179 |
+
"debt_service_ratio": (25.0, 6.0),
|
| 180 |
+
"concessional_pct": (30.0, 10.0),
|
| 181 |
+
"avg_maturity_years": (8.0, 2.5),
|
| 182 |
+
"avg_interest_rate": (7.0, 2.0),
|
| 183 |
+
"primary_balance_pct": (-3.5, 2.0),
|
| 184 |
+
"gdp_growth_pct": (4.0, 1.5),
|
| 185 |
+
"inflation_pct": (12.0, 4.0),
|
| 186 |
+
"reserves_months_imports": (3.0, 1.0),
|
| 187 |
+
"revenue_gdp_pct": (18.0, 2.5),
|
| 188 |
+
"expenditure_gdp_pct": (24.0, 3.0),
|
| 189 |
+
},
|
| 190 |
+
"Senegal": {
|
| 191 |
+
"debt_to_gdp": (65.0, 8.0),
|
| 192 |
+
"external_pct": (55.0, 8.0),
|
| 193 |
+
"debt_service_ratio": (16.0, 4.0),
|
| 194 |
+
"concessional_pct": (45.0, 10.0),
|
| 195 |
+
"avg_maturity_years": (12.0, 3.0),
|
| 196 |
+
"avg_interest_rate": (4.5, 1.5),
|
| 197 |
+
"primary_balance_pct": (-4.5, 2.0),
|
| 198 |
+
"gdp_growth_pct": (7.0, 2.0),
|
| 199 |
+
"inflation_pct": (3.0, 1.5),
|
| 200 |
+
"reserves_months_imports": (5.5, 1.5),
|
| 201 |
+
"revenue_gdp_pct": (22.0, 2.5),
|
| 202 |
+
"expenditure_gdp_pct": (28.0, 3.0),
|
| 203 |
+
},
|
| 204 |
+
"Cote d'Ivoire": {
|
| 205 |
+
"debt_to_gdp": (56.0, 6.0),
|
| 206 |
+
"external_pct": (48.0, 8.0),
|
| 207 |
+
"debt_service_ratio": (14.0, 4.0),
|
| 208 |
+
"concessional_pct": (42.0, 10.0),
|
| 209 |
+
"avg_maturity_years": (11.0, 3.0),
|
| 210 |
+
"avg_interest_rate": (5.0, 1.5),
|
| 211 |
+
"primary_balance_pct": (-2.5, 1.5),
|
| 212 |
+
"gdp_growth_pct": (6.5, 1.5),
|
| 213 |
+
"inflation_pct": (4.0, 2.0),
|
| 214 |
+
"reserves_months_imports": (4.5, 1.0),
|
| 215 |
+
"revenue_gdp_pct": (16.0, 2.0),
|
| 216 |
+
"expenditure_gdp_pct": (21.0, 2.5),
|
| 217 |
+
},
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
# ── Scenario multipliers ───────────────────────────────────────────────────
|
| 221 |
+
SCENARIO_SHIFTS = {
|
| 222 |
+
"baseline": {
|
| 223 |
+
"debt_to_gdp": 1.0,
|
| 224 |
+
"external_pct": 1.0,
|
| 225 |
+
"debt_service_ratio": 1.0,
|
| 226 |
+
"concessional_pct": 1.0,
|
| 227 |
+
"avg_maturity_years": 1.0,
|
| 228 |
+
"avg_interest_rate": 1.0,
|
| 229 |
+
"primary_balance_pct": 1.0,
|
| 230 |
+
"gdp_growth_pct": 1.0,
|
| 231 |
+
"inflation_pct": 1.0,
|
| 232 |
+
"reserves_months_imports": 1.0,
|
| 233 |
+
},
|
| 234 |
+
"debt_consolidation": {
|
| 235 |
+
"debt_to_gdp": 0.85,
|
| 236 |
+
"external_pct": 0.90,
|
| 237 |
+
"debt_service_ratio": 0.75,
|
| 238 |
+
"concessional_pct": 1.15,
|
| 239 |
+
"avg_maturity_years": 1.15,
|
| 240 |
+
"avg_interest_rate": 0.85,
|
| 241 |
+
"primary_balance_pct": -0.5,
|
| 242 |
+
"gdp_growth_pct": 1.20,
|
| 243 |
+
"inflation_pct": 0.80,
|
| 244 |
+
"reserves_months_imports": 1.20,
|
| 245 |
+
},
|
| 246 |
+
"debt_distress": {
|
| 247 |
+
"debt_to_gdp": 1.25,
|
| 248 |
+
"external_pct": 1.15,
|
| 249 |
+
"debt_service_ratio": 1.50,
|
| 250 |
+
"concessional_pct": 0.70,
|
| 251 |
+
"avg_maturity_years": 0.75,
|
| 252 |
+
"avg_interest_rate": 1.30,
|
| 253 |
+
"primary_balance_pct": -2.0,
|
| 254 |
+
"gdp_growth_pct": 0.60,
|
| 255 |
+
"inflation_pct": 1.50,
|
| 256 |
+
"reserves_months_imports": 0.70,
|
| 257 |
+
},
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
def compute_derived_features(df: pd.DataFrame) -> pd.DataFrame:
|
| 262 |
+
"""Compute derived/aggregate features from base variables."""
|
| 263 |
+
# Domestic debt percentage (complement of external)
|
| 264 |
+
df["domestic_debt_pct"] = 100.0 - df["external_debt_pct"]
|
| 265 |
+
|
| 266 |
+
# Debt sustainability rating (DSR) from debt-to-GDP and service ratio
|
| 267 |
+
dsr_score = (
|
| 268 |
+
0.4 * df["debt_to_gdp_pct"] / 100
|
| 269 |
+
+ 0.35 * df["debt_service_ratio_pct"] / 50
|
| 270 |
+
+ 0.15 * (1 - df["concessional_debt_pct"] / 100)
|
| 271 |
+
+ 0.10 * df["average_interest_rate_pct"] / 15
|
| 272 |
+
)
|
| 273 |
+
# Map to 1-5 rating (1=low risk, 5=debt distress)
|
| 274 |
+
df["debt_sustainability_rating"] = np.clip(
|
| 275 |
+
np.round(1 + 4 * dsr_score / dsr_score.quantile(0.95)).values, 1, 5
|
| 276 |
+
).astype(int)
|
| 277 |
+
|
| 278 |
+
# Refinancing risk score (0-100)
|
| 279 |
+
df["refinancing_risk_score"] = np.clip(
|
| 280 |
+
20
|
| 281 |
+
+ 0.3 * df["debt_to_gdp_pct"]
|
| 282 |
+
+ 0.5 * (100 / df["average_maturity_years"].clip(lower=1))
|
| 283 |
+
- 0.3 * df["concessional_debt_pct"]
|
| 284 |
+
+ 0.2 * df["average_interest_rate_pct"]
|
| 285 |
+
+ np.random.normal(0, 5, len(df)),
|
| 286 |
+
0,
|
| 287 |
+
100,
|
| 288 |
+
).round(1)
|
| 289 |
+
|
| 290 |
+
# Fiscal space score (0-100, higher = more room)
|
| 291 |
+
df["fiscal_space_score"] = np.clip(
|
| 292 |
+
50
|
| 293 |
+
+ 1.5 * df["primary_balance_pct"]
|
| 294 |
+
- 0.4 * df["debt_to_gdp_pct"]
|
| 295 |
+
+ 0.8 * df["revenue_to_gdp_pct"]
|
| 296 |
+
- 0.3 * df["debt_service_ratio_pct"]
|
| 297 |
+
+ np.random.normal(0, 6, len(df)),
|
| 298 |
+
0,
|
| 299 |
+
100,
|
| 300 |
+
).round(1)
|
| 301 |
+
|
| 302 |
+
return df
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
def assign_year(scenario: str, rng: np.random.Generator) -> np.ndarray:
|
| 306 |
+
"""Assign observation years with scenario-dependent distributions."""
|
| 307 |
+
if scenario == "baseline":
|
| 308 |
+
return rng.choice(range(2018, 2026), size=1, p=[0.06, 0.07, 0.08, 0.10, 0.12, 0.14, 0.18, 0.25])[0]
|
| 309 |
+
elif scenario == "debt_consolidation":
|
| 310 |
+
return rng.choice(range(2022, 2031), size=1, p=[0.05, 0.06, 0.08, 0.10, 0.12, 0.14, 0.14, 0.16, 0.15])[0]
|
| 311 |
+
else:
|
| 312 |
+
return rng.choice(range(2020, 2029), size=1, p=[0.06, 0.08, 0.10, 0.12, 0.13, 0.14, 0.14, 0.13, 0.10])[0]
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
def generate_scenario(
|
| 316 |
+
scenario: str, n_records: int, rng: np.random.Generator
|
| 317 |
+
) -> pd.DataFrame:
|
| 318 |
+
"""Generate n_records for a given scenario across all countries."""
|
| 319 |
+
rows = []
|
| 320 |
+
records_per_country = n_records // len(COUNTRY_PARAMS)
|
| 321 |
+
shifts = SCENARIO_SHIFTS[scenario]
|
| 322 |
+
|
| 323 |
+
for country, params in COUNTRY_PARAMS.items():
|
| 324 |
+
for _ in range(records_per_country):
|
| 325 |
+
year = assign_year(scenario, rng)
|
| 326 |
+
|
| 327 |
+
# Apply scenario shifts with random noise
|
| 328 |
+
dtg = max(5, rng.normal(
|
| 329 |
+
params["debt_to_gdp"][0] * shifts["debt_to_gdp"],
|
| 330 |
+
params["debt_to_gdp"][1],
|
| 331 |
+
))
|
| 332 |
+
ext_pct = np.clip(
|
| 333 |
+
rng.normal(
|
| 334 |
+
params["external_pct"][0] * shifts["external_pct"],
|
| 335 |
+
params["external_pct"][1],
|
| 336 |
+
),
|
| 337 |
+
5, 95,
|
| 338 |
+
)
|
| 339 |
+
dsr = max(1, rng.normal(
|
| 340 |
+
params["debt_service_ratio"][0] * shifts["debt_service_ratio"],
|
| 341 |
+
params["debt_service_ratio"][1],
|
| 342 |
+
))
|
| 343 |
+
con_pct = np.clip(
|
| 344 |
+
rng.normal(
|
| 345 |
+
params["concessional_pct"][0] * shifts["concessional_pct"],
|
| 346 |
+
params["concessional_pct"][1],
|
| 347 |
+
),
|
| 348 |
+
0, 95,
|
| 349 |
+
)
|
| 350 |
+
maturity = np.clip(rng.normal(
|
| 351 |
+
params["avg_maturity_years"][0] * shifts["avg_maturity_years"],
|
| 352 |
+
params["avg_maturity_years"][1],
|
| 353 |
+
), 1, 40)
|
| 354 |
+
interest = max(0.1, rng.normal(
|
| 355 |
+
params["avg_interest_rate"][0] * shifts["avg_interest_rate"],
|
| 356 |
+
params["avg_interest_rate"][1],
|
| 357 |
+
))
|
| 358 |
+
pb = rng.normal(
|
| 359 |
+
params["primary_balance_pct"][0] + shifts["primary_balance_pct"] - 1.0,
|
| 360 |
+
params["primary_balance_pct"][1],
|
| 361 |
+
)
|
| 362 |
+
gdp_g = max(-10, rng.normal(
|
| 363 |
+
params["gdp_growth_pct"][0] * shifts["gdp_growth_pct"],
|
| 364 |
+
params["gdp_growth_pct"][1],
|
| 365 |
+
))
|
| 366 |
+
inflation = max(0, rng.normal(
|
| 367 |
+
params["inflation_pct"][0] * shifts["inflation_pct"],
|
| 368 |
+
params["inflation_pct"][1],
|
| 369 |
+
))
|
| 370 |
+
reserves = max(0.1, rng.normal(
|
| 371 |
+
params["reserves_months_imports"][0] * shifts["reserves_months_imports"],
|
| 372 |
+
params["reserves_months_imports"][1],
|
| 373 |
+
))
|
| 374 |
+
rev_gdp = max(1, rng.normal(
|
| 375 |
+
params["revenue_gdp_pct"][0],
|
| 376 |
+
params["revenue_gdp_pct"][1],
|
| 377 |
+
))
|
| 378 |
+
exp_gdp = max(rev_gdp, rng.normal(
|
| 379 |
+
params["expenditure_gdp_pct"][0],
|
| 380 |
+
params["expenditure_gdp_pct"][1],
|
| 381 |
+
))
|
| 382 |
+
|
| 383 |
+
rows.append({
|
| 384 |
+
"country": country,
|
| 385 |
+
"year": year,
|
| 386 |
+
"scenario": scenario,
|
| 387 |
+
"debt_to_gdp_pct": round(dtg, 2),
|
| 388 |
+
"external_debt_pct": round(ext_pct, 2),
|
| 389 |
+
"debt_service_ratio_pct": round(dsr, 2),
|
| 390 |
+
"concessional_debt_pct": round(con_pct, 2),
|
| 391 |
+
"average_maturity_years": round(maturity, 2),
|
| 392 |
+
"average_interest_rate_pct": round(interest, 2),
|
| 393 |
+
"primary_balance_pct": round(pb, 2),
|
| 394 |
+
"gdp_growth_pct": round(gdp_g, 2),
|
| 395 |
+
"inflation_pct": round(inflation, 2),
|
| 396 |
+
"reserves_months_imports": round(reserves, 2),
|
| 397 |
+
"revenue_to_gdp_pct": round(rev_gdp, 2),
|
| 398 |
+
"expenditure_to_gdp_pct": round(exp_gdp, 2),
|
| 399 |
+
})
|
| 400 |
+
|
| 401 |
+
df = pd.DataFrame(rows)
|
| 402 |
+
df = compute_derived_features(df)
|
| 403 |
+
|
| 404 |
+
# Reorder columns for clarity
|
| 405 |
+
col_order = [
|
| 406 |
+
"country", "year", "scenario",
|
| 407 |
+
"debt_to_gdp_pct", "external_debt_pct", "domestic_debt_pct",
|
| 408 |
+
"debt_service_ratio_pct", "concessional_debt_pct",
|
| 409 |
+
"average_maturity_years", "average_interest_rate_pct",
|
| 410 |
+
"debt_sustainability_rating", "refinancing_risk_score",
|
| 411 |
+
"primary_balance_pct", "fiscal_space_score",
|
| 412 |
+
"gdp_growth_pct", "inflation_pct",
|
| 413 |
+
"reserves_months_imports", "revenue_to_gdp_pct", "expenditure_to_gdp_pct",
|
| 414 |
+
]
|
| 415 |
+
return df[col_order]
|
| 416 |
+
|
| 417 |
+
|
| 418 |
+
def main():
|
| 419 |
+
parser = argparse.ArgumentParser(description="Generate African Public Debt Management dataset")
|
| 420 |
+
parser.add_argument("--output-dir", default="data", help="Output directory")
|
| 421 |
+
parser.add_argument("--records-per-scenario", type=int, default=N_RECORDS_PER_SCENARIO)
|
| 422 |
+
parser.add_argument("--seed", type=int, default=SEED)
|
| 423 |
+
args = parser.parse_args()
|
| 424 |
+
|
| 425 |
+
os.makedirs(args.output_dir, exist_ok=True)
|
| 426 |
+
rng = np.random.default_rng(args.seed)
|
| 427 |
+
|
| 428 |
+
for scenario in ["baseline", "debt_consolidation", "debt_distress"]:
|
| 429 |
+
print(f"Generating {scenario} scenario ({args.records_per_scenario} records)...")
|
| 430 |
+
df = generate_scenario(scenario, args.records_per_scenario, rng)
|
| 431 |
+
out_path = os.path.join(args.output_dir, f"{scenario}.csv")
|
| 432 |
+
df.to_csv(out_path, index=False)
|
| 433 |
+
print(f" Saved to {out_path} — {len(df)} rows, {len(df.columns)} columns")
|
| 434 |
+
|
| 435 |
+
# Save combined dataset
|
| 436 |
+
dfs = []
|
| 437 |
+
for scenario in ["baseline", "debt_consolidation", "debt_distress"]:
|
| 438 |
+
dfs.append(pd.read_csv(os.path.join(args.output_dir, f"{scenario}.csv")))
|
| 439 |
+
combined = pd.concat(dfs, ignore_index=True)
|
| 440 |
+
combined_path = os.path.join(args.output_dir, "all_scenarios.csv")
|
| 441 |
+
combined.to_csv(combined_path, index=False)
|
| 442 |
+
print(f"Combined dataset saved to {combined_path} — {len(combined)} rows")
|
| 443 |
+
|
| 444 |
+
# Save dataset card metadata
|
| 445 |
+
meta = {
|
| 446 |
+
"name": "African Public Debt Management",
|
| 447 |
+
"description": "Synthetic dataset of public debt indicators for 12 Sub-Saharan African countries",
|
| 448 |
+
"n_countries": len(COUNTRY_PARAMS),
|
| 449 |
+
"countries": sorted(COUNTRY_PARAMS.keys()),
|
| 450 |
+
"scenarios": ["baseline", "debt_consolidation", "debt_distress"],
|
| 451 |
+
"n_records_per_scenario": args.records_per_scenario,
|
| 452 |
+
"n_total_records": len(combined),
|
| 453 |
+
"n_variables": len(combined.columns),
|
| 454 |
+
"columns": list(combined.columns),
|
| 455 |
+
"seed": args.seed,
|
| 456 |
+
}
|
| 457 |
+
with open(os.path.join(args.output_dir, "metadata.json"), "w") as f:
|
| 458 |
+
json.dump(meta, f, indent=2)
|
| 459 |
+
print("Metadata saved to data/metadata.json")
|
| 460 |
+
|
| 461 |
+
|
| 462 |
+
if __name__ == "__main__":
|
| 463 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy>=1.24
|
| 2 |
+
pandas>=2.0
|
| 3 |
+
scipy>=1.11
|
| 4 |
+
matplotlib>=3.7
|
validate_dataset.py
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
African Public Debt Management Dataset Validator
|
| 3 |
+
|
| 4 |
+
Runs plausibility checks and generates 8-panel diagnostic plots.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
import numpy as np
|
| 12 |
+
import pandas as pd
|
| 13 |
+
from matplotlib.gridspec import GridSpec
|
| 14 |
+
|
| 15 |
+
OUTPUT_DIR = "data"
|
| 16 |
+
PLOT_DIR = os.path.join(OUTPUT_DIR, "plots")
|
| 17 |
+
|
| 18 |
+
# ── Plausibility thresholds ────────────────────────────────────────────────
|
| 19 |
+
CHECKS = {
|
| 20 |
+
"debt_to_gdp_pct": (1, 300),
|
| 21 |
+
"external_debt_pct": (0, 100),
|
| 22 |
+
"domestic_debt_pct": (0, 100),
|
| 23 |
+
"debt_service_ratio_pct": (0, 100),
|
| 24 |
+
"concessional_debt_pct": (0, 100),
|
| 25 |
+
"average_maturity_years": (0.5, 40),
|
| 26 |
+
"average_interest_rate_pct": (0, 20),
|
| 27 |
+
"debt_sustainability_rating": (1, 5),
|
| 28 |
+
"refinancing_risk_score": (0, 100),
|
| 29 |
+
"primary_balance_pct": (-20, 15),
|
| 30 |
+
"fiscal_space_score": (0, 100),
|
| 31 |
+
"gdp_growth_pct": (-15, 20),
|
| 32 |
+
"inflation_pct": (0, 100),
|
| 33 |
+
"reserves_months_imports": (0, 30),
|
| 34 |
+
"revenue_to_gdp_pct": (1, 60),
|
| 35 |
+
"expenditure_to_gdp_pct": (2, 70),
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def run_checks(df: pd.DataFrame) -> list[str]:
|
| 40 |
+
"""Run plausibility checks; return list of failure messages."""
|
| 41 |
+
failures = []
|
| 42 |
+
n = len(df)
|
| 43 |
+
|
| 44 |
+
# 1. Range checks
|
| 45 |
+
for col, (lo, hi) in CHECKS.items():
|
| 46 |
+
if col not in df.columns:
|
| 47 |
+
failures.append(f"MISSING COLUMN: {col}")
|
| 48 |
+
continue
|
| 49 |
+
below = (df[col] < lo).sum()
|
| 50 |
+
above = (df[col] > hi).sum()
|
| 51 |
+
if below > 0:
|
| 52 |
+
failures.append(f"RANGE: {col} has {below} values below {lo}")
|
| 53 |
+
if above > 0:
|
| 54 |
+
failures.append(f"RANGE: {col} has {above} values above {hi}")
|
| 55 |
+
|
| 56 |
+
# 2. External + domestic ≈ 100
|
| 57 |
+
if "external_debt_pct" in df.columns and "domestic_debt_pct" in df.columns:
|
| 58 |
+
comp_sum = df["external_debt_pct"] + df["domestic_debt_pct"]
|
| 59 |
+
bad = ((comp_sum - 100).abs() > 0.1).sum()
|
| 60 |
+
if bad > 0:
|
| 61 |
+
failures.append(f"COMPOSITION: {bad} rows where external+domestic ≠ 100%")
|
| 62 |
+
|
| 63 |
+
# 3. Expenditure > revenue (fiscal deficit)
|
| 64 |
+
if "expenditure_to_gdp_pct" in df.columns and "revenue_to_gdp_pct" in df.columns:
|
| 65 |
+
exp_lt_rev = (df["expenditure_to_gdp_pct"] < df["revenue_to_gdp_pct"]).sum()
|
| 66 |
+
if exp_lt_rev / n > 0.85:
|
| 67 |
+
failures.append(
|
| 68 |
+
f"FISCAL: {exp_lt_rev}/{n} ({100*exp_lt_rev/n:.1f}%) rows have expenditure < revenue (unusual for SSA)"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# 4. No NaN/inf
|
| 72 |
+
nan_counts = df.isna().sum()
|
| 73 |
+
for col, cnt in nan_counts.items():
|
| 74 |
+
if cnt > 0:
|
| 75 |
+
failures.append(f"NaN: {col} has {cnt} missing values")
|
| 76 |
+
|
| 77 |
+
inf_cols = df.select_dtypes(include=[np.number]).columns
|
| 78 |
+
for col in inf_cols:
|
| 79 |
+
inf_cnt = np.isinf(df[col]).sum()
|
| 80 |
+
if inf_cnt > 0:
|
| 81 |
+
failures.append(f"INF: {col} has {inf_cnt} infinite values")
|
| 82 |
+
|
| 83 |
+
# 5. Row count
|
| 84 |
+
if len(df) < 25_000:
|
| 85 |
+
failures.append(f"SIZE: Dataset has only {len(df)} rows (expected ≥25K)")
|
| 86 |
+
|
| 87 |
+
# 6. All 12 countries present
|
| 88 |
+
expected_countries = {
|
| 89 |
+
"Nigeria", "Kenya", "Ghana", "South Africa", "DRC", "Ethiopia",
|
| 90 |
+
"Tanzania", "Rwanda", "Mozambique", "Zambia", "Senegal", "Cote d'Ivoire",
|
| 91 |
+
}
|
| 92 |
+
actual = set(df["country"].unique())
|
| 93 |
+
missing = expected_countries - actual
|
| 94 |
+
if missing:
|
| 95 |
+
failures.append(f"COUNTRIES: Missing: {missing}")
|
| 96 |
+
|
| 97 |
+
# 7. All 3 scenarios present
|
| 98 |
+
expected_scenarios = {"baseline", "debt_consolidation", "debt_distress"}
|
| 99 |
+
actual_scenarios = set(df["scenario"].unique())
|
| 100 |
+
missing_sc = expected_scenarios - actual_scenarios
|
| 101 |
+
if missing_sc:
|
| 102 |
+
failures.append(f"SCENARIOS: Missing: {missing_sc}")
|
| 103 |
+
|
| 104 |
+
# 8. Scenario-specific plausibility
|
| 105 |
+
for sc in ["baseline", "debt_consolidation", "debt_distress"]:
|
| 106 |
+
sub = df[df["scenario"] == sc]
|
| 107 |
+
if len(sub) == 0:
|
| 108 |
+
continue
|
| 109 |
+
mean_dsr = sub["debt_service_ratio_pct"].mean()
|
| 110 |
+
if sc == "debt_distress" and mean_dsr < 15:
|
| 111 |
+
failures.append(f"SCENARIO: {sc} mean DSR={mean_dsr:.1f}% seems too low")
|
| 112 |
+
if sc == "debt_consolidation" and mean_dsr > 30:
|
| 113 |
+
failures.append(f"SCENARIO: {sc} mean DSR={mean_dsr:.1f}% seems too high")
|
| 114 |
+
|
| 115 |
+
return failures
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def generate_diagnostic_plots(df: pd.DataFrame):
|
| 119 |
+
"""Generate 8-panel diagnostic figure."""
|
| 120 |
+
os.makedirs(PLOT_DIR, exist_ok=True)
|
| 121 |
+
plt.style.use("seaborn-v0_8-whitegrid")
|
| 122 |
+
fig = plt.figure(figsize=(20, 16))
|
| 123 |
+
gs = GridSpec(3, 3, figure=fig, hspace=0.35, wspace=0.30)
|
| 124 |
+
colors = {"baseline": "#2196F3", "debt_consolidation": "#4CAF50", "debt_distress": "#F44336"}
|
| 125 |
+
|
| 126 |
+
# Panel 1: Debt-to-GDP distribution by scenario
|
| 127 |
+
ax1 = fig.add_subplot(gs[0, 0])
|
| 128 |
+
for sc, c in colors.items():
|
| 129 |
+
sub = df[df["scenario"] == sc]
|
| 130 |
+
ax1.hist(sub["debt_to_gdp_pct"], bins=50, alpha=0.5, label=sc.replace("_", " ").title(), color=c)
|
| 131 |
+
ax1.set_xlabel("Debt-to-GDP (%)")
|
| 132 |
+
ax1.set_ylabel("Count")
|
| 133 |
+
ax1.set_title("A) Debt-to-GDP Distribution by Scenario")
|
| 134 |
+
ax1.legend(fontsize=8)
|
| 135 |
+
|
| 136 |
+
# Panel 2: Debt service ratio by country
|
| 137 |
+
ax2 = fig.add_subplot(gs[0, 1])
|
| 138 |
+
country_order = df.groupby("country")["debt_service_ratio_pct"].median().sort_values(ascending=False).index
|
| 139 |
+
bp = df.boxplot(
|
| 140 |
+
column="debt_service_ratio_pct", by="country", ax=ax2,
|
| 141 |
+
vert=True, patch_artist=True, showfliers=False,
|
| 142 |
+
positions=range(len(country_order)),
|
| 143 |
+
)
|
| 144 |
+
ax2.set_xticklabels(country_order, rotation=45, ha="right", fontsize=7)
|
| 145 |
+
ax2.set_title("B) Debt Service Ratio by Country")
|
| 146 |
+
ax2.set_xlabel("")
|
| 147 |
+
plt.sca(ax2)
|
| 148 |
+
plt.title("B) Debt Service Ratio by Country")
|
| 149 |
+
|
| 150 |
+
# Panel 3: External vs Domestic composition
|
| 151 |
+
ax3 = fig.add_subplot(gs[0, 2])
|
| 152 |
+
for sc, c in colors.items():
|
| 153 |
+
sub = df[df["scenario"] == sc]
|
| 154 |
+
ax3.scatter(sub["external_debt_pct"], sub["domestic_debt_pct"],
|
| 155 |
+
alpha=0.05, s=5, color=c, label=sc.replace("_", " ").title())
|
| 156 |
+
ax3.plot([0, 100], [100, 0], "k--", alpha=0.3, linewidth=1)
|
| 157 |
+
ax3.set_xlabel("External Debt (%)")
|
| 158 |
+
ax3.set_ylabel("Domestic Debt (%)")
|
| 159 |
+
ax3.set_title("C) External vs Domestic Debt Composition")
|
| 160 |
+
ax3.legend(fontsize=8, markerscale=10)
|
| 161 |
+
|
| 162 |
+
# Panel 4: Debt sustainability rating distribution
|
| 163 |
+
ax4 = fig.add_subplot(gs[1, 0])
|
| 164 |
+
for sc, c in colors.items():
|
| 165 |
+
sub = df[df["scenario"] == sc]
|
| 166 |
+
counts = sub["debt_sustainability_rating"].value_counts().sort_index()
|
| 167 |
+
ax4.bar(counts.index + (0.2 if sc == "debt_consolidation" else (-0.2 if sc == "baseline" else 0)),
|
| 168 |
+
counts.values, width=0.2, alpha=0.7, color=c, label=sc.replace("_", " ").title())
|
| 169 |
+
ax4.set_xlabel("Debt Sustainability Rating (1=Low Risk, 5=Distress)")
|
| 170 |
+
ax4.set_ylabel("Count")
|
| 171 |
+
ax4.set_title("D) Debt Sustainability Rating Distribution")
|
| 172 |
+
ax4.legend(fontsize=8)
|
| 173 |
+
|
| 174 |
+
# Panel 5: Concessional vs interest rate
|
| 175 |
+
ax5 = fig.add_subplot(gs[1, 1])
|
| 176 |
+
sample = df.sample(min(5000, len(df)), random_state=42)
|
| 177 |
+
for sc, c in colors.items():
|
| 178 |
+
sub = sample[sample["scenario"] == sc]
|
| 179 |
+
ax5.scatter(sub["concessional_debt_pct"], sub["average_interest_rate_pct"],
|
| 180 |
+
alpha=0.3, s=8, color=c)
|
| 181 |
+
ax5.set_xlabel("Concessional Debt (%)")
|
| 182 |
+
ax5.set_ylabel("Average Interest Rate (%)")
|
| 183 |
+
ax5.set_title("E) Concessional Debt vs Interest Rate")
|
| 184 |
+
|
| 185 |
+
# Panel 6: Refinancing risk vs maturity
|
| 186 |
+
ax6 = fig.add_subplot(gs[1, 2])
|
| 187 |
+
for sc, c in colors.items():
|
| 188 |
+
sub = sample[sample["scenario"] == sc]
|
| 189 |
+
ax6.scatter(sub["average_maturity_years"], sub["refinancing_risk_score"],
|
| 190 |
+
alpha=0.3, s=8, color=c, label=sc.replace("_", " ").title())
|
| 191 |
+
ax6.set_xlabel("Average Maturity (Years)")
|
| 192 |
+
ax6.set_ylabel("Refinancing Risk Score")
|
| 193 |
+
ax6.set_title("F) Refinancing Risk vs Debt Maturity")
|
| 194 |
+
ax6.legend(fontsize=8, markerscale=5)
|
| 195 |
+
|
| 196 |
+
# Panel 7: Primary balance vs fiscal space
|
| 197 |
+
ax7 = fig.add_subplot(gs[2, 0])
|
| 198 |
+
for sc, c in colors.items():
|
| 199 |
+
sub = sample[sample["scenario"] == sc]
|
| 200 |
+
ax7.scatter(sub["primary_balance_pct"], sub["fiscal_space_score"],
|
| 201 |
+
alpha=0.3, s=8, color=c)
|
| 202 |
+
ax7.set_xlabel("Primary Balance (% GDP)")
|
| 203 |
+
ax7.set_ylabel("Fiscal Space Score")
|
| 204 |
+
ax7.set_title("G) Primary Balance vs Fiscal Space")
|
| 205 |
+
ax7.axvline(0, color="gray", linestyle="--", alpha=0.5)
|
| 206 |
+
|
| 207 |
+
# Panel 8: Scenario comparison heatmap (mean values)
|
| 208 |
+
ax8 = fig.add_subplot(gs[2, 1:])
|
| 209 |
+
metrics = [
|
| 210 |
+
"debt_to_gdp_pct", "debt_service_ratio_pct", "concessional_debt_pct",
|
| 211 |
+
"average_interest_rate_pct", "refinancing_risk_score", "fiscal_space_score",
|
| 212 |
+
"primary_balance_pct", "debt_sustainability_rating",
|
| 213 |
+
]
|
| 214 |
+
scenario_means = df.groupby("scenario")[metrics].mean()
|
| 215 |
+
# Normalize for heatmap
|
| 216 |
+
normed = (scenario_means - scenario_means.min()) / (scenario_means.max() - scenario_means.min() + 1e-9)
|
| 217 |
+
im = ax8.imshow(normed.values, aspect="auto", cmap="RdYlGn_r", vmin=0, vmax=1)
|
| 218 |
+
ax8.set_xticks(range(len(metrics)))
|
| 219 |
+
ax8.set_xticklabels([m.replace("_", "\n") for m in metrics], fontsize=7, rotation=0, ha="center")
|
| 220 |
+
ax8.set_yticks(range(len(scenario_means.index)))
|
| 221 |
+
ax8.set_yticklabels([s.replace("_", " ").title() for s in scenario_means.index])
|
| 222 |
+
ax8.set_title("H) Scenario Comparison (Normalized Mean Values)")
|
| 223 |
+
# Add text annotations
|
| 224 |
+
for i in range(len(scenario_means.index)):
|
| 225 |
+
for j in range(len(metrics)):
|
| 226 |
+
val = scenario_means.values[i, j]
|
| 227 |
+
ax8.text(j, i, f"{val:.1f}", ha="center", va="center", fontsize=6,
|
| 228 |
+
color="white" if normed.values[i, j] > 0.6 else "black")
|
| 229 |
+
|
| 230 |
+
fig.suptitle("African Public Debt Management — Diagnostic Plots", fontsize=16, fontweight="bold", y=0.98)
|
| 231 |
+
plot_path = os.path.join(PLOT_DIR, "diagnostic_plots.png")
|
| 232 |
+
fig.savefig(plot_path, dpi=150, bbox_inches="tight")
|
| 233 |
+
plt.close(fig)
|
| 234 |
+
print(f"Diagnostic plots saved to {plot_path}")
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def main():
|
| 238 |
+
combined_path = os.path.join(OUTPUT_DIR, "all_scenarios.csv")
|
| 239 |
+
if not os.path.exists(combined_path):
|
| 240 |
+
print(f"ERROR: {combined_path} not found. Run generate_dataset.py first.")
|
| 241 |
+
sys.exit(1)
|
| 242 |
+
|
| 243 |
+
df = pd.read_csv(combined_path)
|
| 244 |
+
print(f"Loaded {len(df)} records from {combined_path}")
|
| 245 |
+
print(f" Countries: {df['country'].nunique()}")
|
| 246 |
+
print(f" Scenarios: {df['scenario'].unique()}")
|
| 247 |
+
print(f" Columns: {len(df.columns)}")
|
| 248 |
+
|
| 249 |
+
# Run plausibility checks
|
| 250 |
+
print("\n── Plausibility Checks ──")
|
| 251 |
+
failures = run_checks(df)
|
| 252 |
+
if failures:
|
| 253 |
+
for f in failures:
|
| 254 |
+
print(f" ✗ {f}")
|
| 255 |
+
print(f"\n{len(failures)} check(s) failed.")
|
| 256 |
+
else:
|
| 257 |
+
print(" ✓ All checks passed.")
|
| 258 |
+
|
| 259 |
+
# Summary statistics
|
| 260 |
+
print("\n── Summary Statistics ──")
|
| 261 |
+
numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
|
| 262 |
+
summary = df.groupby("scenario")[numeric_cols].agg(["mean", "std"]).round(2)
|
| 263 |
+
print(summary.to_string(max_cols=12))
|
| 264 |
+
|
| 265 |
+
# Generate plots
|
| 266 |
+
print("\n── Generating Diagnostic Plots ──")
|
| 267 |
+
generate_diagnostic_plots(df)
|
| 268 |
+
|
| 269 |
+
return 0 if not failures else 1
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
if __name__ == "__main__":
|
| 273 |
+
sys.exit(main())
|