| |
| """Generate synthetic African crop insurance payout dataset. |
| |
| Parameters derived from IBLI (Kenya/Ethiopia), WFP R4, ACRE Africa |
| programs and published literature on index-based insurance in SSA. |
| """ |
|
|
| import csv |
| import json |
| import os |
| import random |
| from datetime import datetime |
|
|
| import numpy as np |
|
|
| |
| COUNTRIES = { |
| "Kenya": { |
| "regions": 8, |
| "base_enrollment_rate": 0.12, |
| "premium_rate": 0.055, |
| "avg_premium_usd": 12, |
| "drought_prob": 0.25, |
| "ndvi_baseline": 0.48, |
| "rainfall_baseline_mm": 650, |
| }, |
| "Ethiopia": { |
| "regions": 10, |
| "base_enrollment_rate": 0.08, |
| "premium_rate": 0.045, |
| "avg_premium_usd": 8, |
| "drought_prob": 0.30, |
| "ndvi_baseline": 0.42, |
| "rainfall_baseline_mm": 580, |
| }, |
| "Tanzania": { |
| "regions": 7, |
| "base_enrollment_rate": 0.06, |
| "premium_rate": 0.05, |
| "avg_premium_usd": 10, |
| "drought_prob": 0.20, |
| "ndvi_baseline": 0.50, |
| "rainfall_baseline_mm": 720, |
| }, |
| "Malawi": { |
| "regions": 5, |
| "base_enrollment_rate": 0.09, |
| "premium_rate": 0.06, |
| "avg_premium_usd": 7, |
| "drought_prob": 0.22, |
| "ndvi_baseline": 0.46, |
| "rainfall_baseline_mm": 900, |
| }, |
| "Zambia": { |
| "regions": 6, |
| "base_enrollment_rate": 0.07, |
| "premium_rate": 0.05, |
| "avg_premium_usd": 9, |
| "drought_prob": 0.18, |
| "ndvi_baseline": 0.52, |
| "rainfall_baseline_mm": 850, |
| }, |
| "Zimbabwe": { |
| "regions": 5, |
| "base_enrollment_rate": 0.05, |
| "premium_rate": 0.06, |
| "avg_premium_usd": 8, |
| "drought_prob": 0.28, |
| "ndvi_baseline": 0.44, |
| "rainfall_baseline_mm": 600, |
| }, |
| "Mozambique": { |
| "regions": 5, |
| "base_enrollment_rate": 0.04, |
| "premium_rate": 0.055, |
| "avg_premium_usd": 6, |
| "drought_prob": 0.22, |
| "ndvi_baseline": 0.53, |
| "rainfall_baseline_mm": 950, |
| }, |
| "Senegal": { |
| "regions": 4, |
| "base_enrollment_rate": 0.06, |
| "premium_rate": 0.05, |
| "avg_premium_usd": 11, |
| "drought_prob": 0.35, |
| "ndvi_baseline": 0.35, |
| "rainfall_baseline_mm": 450, |
| }, |
| "Ghana": { |
| "regions": 5, |
| "base_enrollment_rate": 0.05, |
| "premium_rate": 0.045, |
| "avg_premium_usd": 10, |
| "drought_prob": 0.15, |
| "ndvi_baseline": 0.55, |
| "rainfall_baseline_mm": 1100, |
| }, |
| "Nigeria": { |
| "regions": 8, |
| "base_enrollment_rate": 0.03, |
| "premium_rate": 0.05, |
| "avg_premium_usd": 9, |
| "drought_prob": 0.20, |
| "ndvi_baseline": 0.50, |
| "rainfall_baseline_mm": 1050, |
| }, |
| "Rwanda": { |
| "regions": 4, |
| "base_enrollment_rate": 0.10, |
| "premium_rate": 0.04, |
| "avg_premium_usd": 7, |
| "drought_prob": 0.12, |
| "ndvi_baseline": 0.56, |
| "rainfall_baseline_mm": 1200, |
| }, |
| "Uganda": { |
| "regions": 5, |
| "base_enrollment_rate": 0.07, |
| "premium_rate": 0.05, |
| "avg_premium_usd": 8, |
| "drought_prob": 0.18, |
| "ndvi_baseline": 0.52, |
| "rainfall_baseline_mm": 1150, |
| }, |
| } |
|
|
| CROP_TYPES = ["maize", "wheat", "sorghum", "rice", "cassava"] |
| CROP_WEIGHTS = [0.35, 0.15, 0.20, 0.15, 0.15] |
|
|
| INSURANCE_TYPES = ["index_based", "area_yield", "weather_derivative"] |
| INSURANCE_WEIGHTS = [0.50, 0.30, 0.20] |
|
|
| SEASONS = ["long_rains", "short_rains"] |
|
|
| SCENARIOS = { |
| "baseline": { |
| "enrollment_mult": 1.0, |
| "payout_mult": 1.0, |
| "drought_shift": 0.0, |
| "description": "Current-state parameters based on existing IBLI/R4/ACRE programs", |
| }, |
| "expanded_index_insurance": { |
| "enrollment_mult": 2.5, |
| "payout_mult": 1.1, |
| "drought_shift": 0.0, |
| "description": "Scaled enrollment via subsidy and mobile distribution (ACRE model)", |
| }, |
| "climate_shock": { |
| "enrollment_mult": 1.1, |
| "payout_mult": 1.0, |
| "drought_shift": 0.15, |
| "description": "Increased drought frequency reflecting IPCC AR6 projections for SSA", |
| }, |
| } |
|
|
| |
| |
| BASIS_RISK_PARAMS = { |
| "index_based": {"mean": 0.31, "std": 0.12}, |
| "area_yield": {"mean": 0.22, "std": 0.10}, |
| "weather_derivative": {"mean": 0.18, "std": 0.09}, |
| } |
|
|
| YEARS = list(range(2015, 2025)) |
|
|
| |
| CROP_YIELD = { |
| "maize": {"mean": 1.8, "std": 0.6}, |
| "wheat": {"mean": 1.5, "std": 0.5}, |
| "sorghum": {"mean": 0.9, "std": 0.4}, |
| "rice": {"mean": 2.2, "std": 0.7}, |
| "cassava": {"mean": 8.0, "std": 3.0}, |
| } |
|
|
|
|
| def generate_record(record_id, country, params, scenario_name, scenario, year, season): |
| """Generate a single insurance record.""" |
| rng = np.random.default_rng(seed=(record_id * 31 + hash(country) + year) % (2**31)) |
|
|
| |
| drought_prob = min(params["drought_prob"] + scenario["drought_shift"], 0.75) |
| is_drought = rng.random() < drought_prob |
|
|
| |
| |
| if is_drought: |
| ndvi_anomaly = round(rng.normal(-1.2, 0.5), 3) |
| ndvi_anomaly = max(ndvi_anomaly, -3.0) |
| else: |
| ndvi_anomaly = round(rng.normal(0.1, 0.4), 3) |
| ndvi_anomaly = max(min(ndvi_anomaly, 2.0), -0.5) |
|
|
| |
| if is_drought: |
| rainfall_deficit = round(rng.uniform(25, 65), 1) |
| else: |
| rainfall_deficit = round(max(0, rng.normal(5, 15)), 1) |
|
|
| |
| base_yield_loss = max(0, -ndvi_anomaly * 15 + rainfall_deficit * 0.3 + rng.normal(0, 5)) |
| yield_loss_pct = round(min(base_yield_loss, 90), 1) |
|
|
| |
| insurance_type = rng.choice(INSURANCE_TYPES, p=INSURANCE_WEIGHTS) |
|
|
| |
| crop_type = rng.choice(CROP_TYPES, p=CROP_WEIGHTS) |
|
|
| |
| enrolled_hectares = round(rng.lognormal(mean=1.2, sigma=0.7), 2) |
| enrolled_hectares = max(0.2, min(enrolled_hectares, 20.0)) |
|
|
| |
| base_premium = params["avg_premium_usd"] |
| premium_paid = round(base_premium * enrolled_hectares * rng.uniform(0.8, 1.3), 2) |
|
|
| |
| |
| |
| |
| if insurance_type == "index_based": |
| payout_triggered = ndvi_anomaly < -1.0 |
| elif insurance_type == "area_yield": |
| payout_triggered = yield_loss_pct > 20.0 |
| else: |
| payout_triggered = rainfall_deficit > 30.0 |
|
|
| |
| scenario_boost = 0.1 * (scenario["payout_mult"] - 1.0) |
| if not payout_triggered and rng.random() < scenario_boost: |
| payout_triggered = True |
|
|
| |
| |
| |
| if payout_triggered: |
| severity = max(min(yield_loss_pct / 50.0, 1.0), 0.15) |
| payout_ratio = round(max(rng.uniform(0.4, 0.9) * severity, 0.05), 3) |
| insured_value = enrolled_hectares * CROP_YIELD[crop_type]["mean"] * 350 |
| payout_amount = round(max(insured_value * payout_ratio, 1.0), 2) |
| else: |
| payout_ratio = 0.0 |
| payout_amount = 0.0 |
|
|
| |
| farmer_count = int(rng.lognormal(mean=4.5, sigma=1.0)) |
| farmer_count = max(5, min(farmer_count, 500)) |
|
|
| |
| base_rate = params["base_enrollment_rate"] |
| enrollment_rate = round( |
| min(base_rate * scenario["enrollment_mult"] * rng.uniform(0.7, 1.3), 0.85), 4 |
| ) |
|
|
| |
| if payout_triggered: |
| lapse_rate = round(rng.uniform(0.05, 0.15), 4) |
| else: |
| lapse_rate = round(rng.uniform(0.15, 0.30), 4) |
|
|
| |
| |
| br = BASIS_RISK_PARAMS[insurance_type] |
| basis_risk_score = round(np.clip(rng.normal(br["mean"], br["std"]), 0.0, 1.0), 3) |
|
|
| return { |
| "record_id": record_id, |
| "country": country, |
| "year": year, |
| "season": season, |
| "crop_type": crop_type, |
| "insurance_type": insurance_type, |
| "scenario": scenario_name, |
| "enrolled_hectares": enrolled_hectares, |
| "premium_paid_usd": premium_paid, |
| "ndvi_anomaly": ndvi_anomaly, |
| "rainfall_deficit_pct": rainfall_deficit, |
| "yield_loss_pct": yield_loss_pct, |
| "payout_triggered": payout_triggered, |
| "payout_amount_usd": payout_amount, |
| "payout_ratio": payout_ratio, |
| "farmer_count": farmer_count, |
| "enrollment_rate": enrollment_rate, |
| "lapse_rate": lapse_rate, |
| "basis_risk_score": basis_risk_score, |
| } |
|
|
|
|
| def generate_dataset(): |
| """Generate full dataset: 12 countries × 3 scenarios × 10000 records.""" |
| all_records = [] |
| record_id = 1 |
|
|
| for scenario_name, scenario in SCENARIOS.items(): |
| records_per_country = 10000 // len(COUNTRIES) |
| remainder = 10000 % len(COUNTRIES) |
|
|
| for i, (country, params) in enumerate(COUNTRIES.items()): |
| n = records_per_country + (1 if i < remainder else 0) |
|
|
| for _ in range(n): |
| year = random.choice(YEARS) |
| season = random.choice(SEASONS) |
| rec = generate_record( |
| record_id, country, params, scenario_name, scenario, year, season |
| ) |
| all_records.append(rec) |
| record_id += 1 |
|
|
| return all_records |
|
|
|
|
| def write_csv(records, output_path): |
| """Write records to CSV.""" |
| fieldnames = list(records[0].keys()) |
| with open(output_path, "w", newline="") as f: |
| writer = csv.DictWriter(f, fieldnames=fieldnames) |
| writer.writeheader() |
| writer.writerows(records) |
|
|
|
|
| def write_metadata(output_dir): |
| """Write dataset metadata JSON.""" |
| metadata = { |
| "name": "african-crop-insurance-payouts", |
| "description": ( |
| "Synthetic dataset of agricultural index-insurance payouts across " |
| "12 Sub-Saharan African countries. Parameters calibrated from IBLI " |
| "(Kenya/Ethiopia), WFP R4, and ACRE Africa programs." |
| ), |
| "version": "1.0.0", |
| "created": datetime.utcnow().isoformat() + "Z", |
| "license": "CC-BY-4.0", |
| "scenarios": { |
| name: s["description"] for name, s in SCENARIOS.items() |
| }, |
| "countries": list(COUNTRIES.keys()), |
| "crop_types": CROP_TYPES, |
| "insurance_types": INSURANCE_TYPES, |
| "seasons": SEASONS, |
| "sources": [ |
| "Jensen & Barrett (2016) AJAE - IBLI basis risk analysis", |
| "WFP R4 Rural Resilience Initiative reports (2018-2023)", |
| "ACRE Africa scaling data (GIIF 2016, IFC 2014)", |
| "Ntukamazina et al. (2017) J. Agr. Rural Develop. Trop. Subtrop.", |
| "IPCC AR6 WGII Chapter 9 - Africa climate projections", |
| ], |
| "variables": { |
| "record_id": "Unique integer identifier", |
| "country": "Country name", |
| "year": "Calendar year (2015-2024)", |
| "season": "Growing season (long_rains / short_rains)", |
| "crop_type": "Insured crop (maize/wheat/sorghum/rice/cassava)", |
| "insurance_type": "Product type (index_based/area_yield/weather_derivative)", |
| "scenario": "Policy scenario (baseline/expanded_index_insurance/climate_shock)", |
| "enrolled_hectares": "Hectares covered by the policy", |
| "premium_paid_usd": "Premium amount in USD", |
| "ndvi_anomaly": "NDVI z-score anomaly (negative = vegetation deficit)", |
| "rainfall_deficit_pct": "Percentage below normal rainfall", |
| "yield_loss_pct": "Estimated yield loss percentage", |
| "payout_triggered": "Whether index triggered a payout (True/False)", |
| "payout_amount_usd": "Payout disbursement in USD (0 if not triggered)", |
| "payout_ratio": "Payout as fraction of insured value", |
| "farmer_count": "Number of farmers in the micro-cohort", |
| "enrollment_rate": "Regional enrollment rate (0-1)", |
| "lapse_rate": "Policy non-renewal rate (0-1)", |
| "basis_risk_score": "Residual unhedged risk (0=perfect, 1=none)", |
| }, |
| "parameter_sources": { |
| "premium_rates": "IBLI: 5.5%/3.25% of insured value; R4: 10-15% farmer contribution", |
| "payout_trigger": "IBLI: NDVI < 15th percentile; ACRE: yield < 80% of average", |
| "basis_risk": "Jensen et al. 2016: IBLI reduces covariate risk by 63%, residual 69%", |
| "enrollment": "R4: ~180K farmers (2020); ACRE: ~400K farmers (2015)", |
| "sum_insured": "R4: ~$98/farmer; ACRE: $650/acre for bundled seed products", |
| }, |
| } |
|
|
| with open(os.path.join(output_dir, "metadata.json"), "w") as f: |
| json.dump(metadata, f, indent=2) |
|
|
|
|
| if __name__ == "__main__": |
| output_dir = os.path.join(os.path.dirname(__file__), "data") |
| os.makedirs(output_dir, exist_ok=True) |
|
|
| print("Generating 30,000 records (12 countries × 3 scenarios × ~10,000)...") |
| records = generate_dataset() |
|
|
| csv_path = os.path.join(output_dir, "african_crop_insurance_payouts.csv") |
| write_csv(records, csv_path) |
| print(f"Wrote {len(records)} records to {csv_path}") |
|
|
| write_metadata(os.path.dirname(__file__)) |
| print("Wrote metadata.json") |
|
|
| |
| payouts = [r for r in records if r["payout_triggered"]] |
| print(f"\nSummary:") |
| print(f" Total records: {len(records)}") |
| print(f" Payouts triggered: {len(payouts)} ({100*len(payouts)/len(records):.1f}%)") |
| print(f" Countries: {len(COUNTRIES)}") |
| print(f" Scenarios: {list(SCENARIOS.keys())}") |
| if payouts: |
| print(f" Avg payout: ${np.mean([r['payout_amount_usd'] for r in payouts]):.2f}") |
| print(f" Avg basis risk: {np.mean([r['basis_risk_score'] for r in records]):.3f}") |
|
|