Kossisoroyce's picture
Upload folder using huggingface_hub
d473aa5 verified
Raw
History Blame Contribute Delete
9.33 kB
#!/usr/bin/env python3
"""
African Civil Service Capacity — Synthetic Dataset Generator
============================================================
Electric Sheep Africa | electricsheepafrica on HuggingFace
References
----------
[1] SA Public Service Commission, Public Service Reforms Report, 2024
[2] SA National Treasury, Compensation and Employment Data MTBPS, 2024
[3] Stats SA, Annual Report 2023/24
[4] Uganda Ministry of Public Service, State of HR 2023
[5] OECD/AUC, Africa's Development Dynamics 2024
[6] Africa Careers Network, Employability in Africa 2023
"""
import argparse, os, sys
import numpy as np
import pandas as pd
from scipy.stats import truncnorm
COUNTRIES = {
"South Africa": {"population_2024": 62.0, "tier": "advanced", "civil_servants_per_10k": 530, "vacancy_baseline": 0.19, "degree_rate": 0.45, "training_hours": 40, "growth_rate": 0.008},
"Kenya": {"population_2024": 55.0, "tier": "advanced", "civil_servants_per_10k": 350, "vacancy_baseline": 0.15, "degree_rate": 0.40, "training_hours": 35, "growth_rate": 0.021},
"Nigeria": {"population_2024": 230.0, "tier": "developing", "civil_servants_per_10k": 200, "vacancy_baseline": 0.25, "degree_rate": 0.30, "training_hours": 20, "growth_rate": 0.025},
"Ghana": {"population_2024": 34.0, "tier": "developing", "civil_servants_per_10k": 250, "vacancy_baseline": 0.20, "degree_rate": 0.35, "training_hours": 25, "growth_rate": 0.021},
"Tanzania": {"population_2024": 67.0, "tier": "developing", "civil_servants_per_10k": 180, "vacancy_baseline": 0.22, "degree_rate": 0.28, "training_hours": 18, "growth_rate": 0.030},
"Uganda": {"population_2024": 49.0, "tier": "developing", "civil_servants_per_10k": 80, "vacancy_baseline": 0.30, "degree_rate": 0.25, "training_hours": 15, "growth_rate": 0.032},
"Rwanda": {"population_2024": 14.0, "tier": "advanced", "civil_servants_per_10k": 300, "vacancy_baseline": 0.12, "degree_rate": 0.42, "training_hours": 50, "growth_rate": 0.024},
"Ethiopia": {"population_2024": 126.0, "tier": "early", "civil_servants_per_10k": 120, "vacancy_baseline": 0.35, "degree_rate": 0.20, "training_hours": 10, "growth_rate": 0.025},
"Senegal": {"population_2024": 18.0, "tier": "developing", "civil_servants_per_10k": 220, "vacancy_baseline": 0.18, "degree_rate": 0.32, "training_hours": 22, "growth_rate": 0.027},
"DRC": {"population_2024": 105.0, "tier": "early", "civil_servants_per_10k": 60, "vacancy_baseline": 0.40, "degree_rate": 0.15, "training_hours": 8, "growth_rate": 0.032},
"Mozambique": {"population_2024": 33.0, "tier": "early", "civil_servants_per_10k": 100, "vacancy_baseline": 0.32, "degree_rate": 0.18, "training_hours": 12, "growth_rate": 0.027},
"Botswana": {"population_2024": 2.6, "tier": "advanced", "civil_servants_per_10k": 450, "vacancy_baseline": 0.14, "degree_rate": 0.48, "training_hours": 45, "growth_rate": 0.015},
}
SECTORS = ["Health", "Education", "Finance", "Infrastructure", "Agriculture", "Security", "Justice", "Social_Services", "Administration", "ICT"]
SECTOR_WEIGHTS = [0.18, 0.22, 0.08, 0.08, 0.06, 0.12, 0.05, 0.08, 0.08, 0.05]
GRADE_LEVELS = ["Junior", "Mid_Level", "Senior", "Director", "Executive"]
GRADE_WEIGHTS = [0.45, 0.30, 0.15, 0.07, 0.03]
SCENARIOS = {
"baseline": {"description": "Current SSA civil service landscape", "vacancy_mult": 1.0, "qualification_mult": 1.0, "training_mult": 1.0},
"reform_modernized": {"description": "Civil service reform with meritocratic recruitment", "vacancy_mult": 0.7, "qualification_mult": 1.3, "training_mult": 1.5},
"underresourced": {"description": "Austerity and brain drain", "vacancy_mult": 1.5, "qualification_mult": 0.7, "training_mult": 0.5},
}
REGION_TYPES = ["capital", "urban", "rural", "remote"]
REGION_WEIGHTS = {"advanced": [0.30, 0.35, 0.25, 0.10], "developing": [0.25, 0.30, 0.30, 0.15], "early": [0.20, 0.25, 0.35, 0.20]}
REGION_VACANCY_ADJUST = {"capital": 0.8, "urban": 0.9, "rural": 1.2, "remote": 1.5}
REGION_QUALIFICATION_ADJUST = {"capital": 1.2, "urban": 1.1, "rural": 0.8, "remote": 0.6}
def generate_dataset(n=10000, scenario="baseline", seed=42):
rng = np.random.default_rng(seed)
sp = SCENARIOS[scenario]
records = []
for i in range(n):
country = rng.choice(list(COUNTRIES.keys()))
cd = COUNTRIES[country]
year = rng.integers(2018, 2026)
region = rng.choice(REGION_TYPES, p=REGION_WEIGHTS[cd["tier"]])
sector = rng.choice(SECTORS, p=SECTOR_WEIGHTS)
grade = rng.choice(GRADE_LEVELS, p=GRADE_WEIGHTS)
pop = cd["population_2024"] * (1 + cd["growth_rate"]) ** (year - 2024)
pop = max(0.5, pop)
staff_per_10k = cd["civil_servants_per_10k"] * rng.uniform(0.85, 1.15)
total_staff = int(pop * 1_000_000 * staff_per_10k / 10000)
vacancy_rate = cd["vacancy_baseline"] * REGION_VACANCY_ADJUST[region] * sp["vacancy_mult"] * rng.uniform(0.8, 1.2)
vacancy_rate = np.clip(vacancy_rate, 0.05, 0.60)
filled_posts = int(total_staff * (1 - vacancy_rate))
degree_rate = cd["degree_rate"] * REGION_QUALIFICATION_ADJUST[region] * sp["qualification_mult"] * rng.uniform(0.85, 1.15)
degree_rate = np.clip(degree_rate, 0.10, 0.80)
degree_holders = int(filled_posts * degree_rate)
training_hours = cd["training_hours"] * sp["training_mult"] * rng.uniform(0.7, 1.3)
training_hours = max(0, int(training_hours))
training_participation = 0.5 + (training_hours / 100) * 0.4
training_participation = np.clip(training_participation * rng.uniform(0.85, 1.15), 0.2, 0.95)
retention_rate = 0.85 if cd["tier"] == "advanced" else (0.75 if cd["tier"] == "developing" else 0.65)
retention_rate *= rng.uniform(0.9, 1.1)
retention_rate = np.clip(retention_rate, 0.50, 0.98)
avg_salary_usd = (500 if cd["tier"] == "advanced" else (250 if cd["tier"] == "developing" else 120)) * rng.uniform(0.7, 1.3)
if grade == "Senior": avg_salary_usd *= 1.5
elif grade == "Director": avg_salary_usd *= 2.5
elif grade == "Executive": avg_salary_usd *= 4.0
wage_bill_pct_gdp = (0.08 if cd["tier"] == "advanced" else 0.06) * rng.uniform(0.8, 1.2)
performance_eval_rate = 0.70 if cd["tier"] == "advanced" else (0.50 if cd["tier"] == "developing" else 0.30)
performance_eval_rate *= sp["qualification_mult"] * rng.uniform(0.85, 1.15)
performance_eval_rate = np.clip(performance_eval_rate, 0.15, 0.95)
digital_literacy = 0.65 if cd["tier"] == "advanced" else (0.40 if cd["tier"] == "developing" else 0.20)
digital_literacy *= rng.uniform(0.8, 1.2)
digital_literacy = np.clip(digital_literacy, 0.10, 0.90)
capacity_score = (0.25 * (1 - vacancy_rate) + 0.25 * degree_rate + 0.15 * (training_hours / 60) + 0.15 * retention_rate + 0.10 * performance_eval_rate + 0.10 * digital_literacy)
capacity_score = np.clip(capacity_score, 0, 1)
if capacity_score >= 0.65: capacity_class = "high"
elif capacity_score >= 0.50: capacity_class = "moderate"
elif capacity_score >= 0.35: capacity_class = "low"
else: capacity_class = "critical"
record = {
"record_id": i + 1, "country": country, "year": year, "region_type": region,
"sector": sector, "grade_level": grade, "population_millions": round(pop, 2),
"total_posts": total_staff, "vacancy_rate": round(vacancy_rate, 4),
"filled_posts": filled_posts, "degree_rate": round(degree_rate, 4),
"degree_holders": degree_holders, "training_hours_annual": training_hours,
"training_participation_rate": round(training_participation, 4),
"retention_rate": round(retention_rate, 4), "avg_salary_usd": round(avg_salary_usd, 2),
"wage_bill_pct_gdp": round(wage_bill_pct_gdp, 4),
"performance_eval_rate": round(performance_eval_rate, 4),
"digital_literacy_rate": round(digital_literacy, 4),
"capacity_score": round(capacity_score, 4), "capacity_class": capacity_class,
}
records.append(record)
df = pd.DataFrame(records)
print(f"\n=== Generated {scenario} scenario ({n} records) ===")
print(f"Mean vacancy rate: {df['vacancy_rate'].mean():.3f}")
print(f"Mean degree rate: {df['degree_rate'].mean():.3f}")
print(f"Mean training hours: {df['training_hours_annual'].mean():.1f}")
print(f"Mean capacity score: {df['capacity_score'].mean():.3f}")
print(f"Capacity class distribution:"); print(df['capacity_class'].value_counts().sort_index())
return df
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--scenario", type=str, default="baseline", choices=list(SCENARIOS.keys()))
parser.add_argument("--n", type=int, default=10000)
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--output", type=str, default=None)
args = parser.parse_args()
df = generate_dataset(n=args.n, scenario=args.scenario, seed=args.seed)
if args.output is None:
os.makedirs("data", exist_ok=True)
args.output = f"data/{args.scenario}.csv"
df.to_csv(args.output, index=False)
print(f"\nSaved to {args.output}")