# -*- coding: utf-8 -*-
"""Escaping negative genuine savings? Distribution dynamics of adjusted net savings in 160 countries, 1990–2021

GOMD arastirma makalesi 05 - makaledeki tum sayilari, tablolari ve sekilleri ureten kod (Google Colab surumu).
Colab: kodu tek bir hucreye yapistirip calistirin ya da .ipynb dosyasini Dosya > Not defteri yukle ile acin.
Kaynak: GOMD acik veri platformu (https://gomd.pirireis.edu.tr) - Piri Reis Universitesi ve
T.C. Cevre, Sehircilik ve Iklim Degisikligi Bakanligi Cevre Yonetimi Genel Mudurlugu.
"""

# # Escaping negative genuine savings? Distribution dynamics of adjusted net savings in 160 countries, 1990–2021
#
# Bu not defteri makaledeki **tüm sayıları, tabloları ve şekilleri** üreten kodun Colab sürümüdür (makalenin replikasyon paketindeki betiklerle aynı kod, hücrelere bölünmüş hâlde).
#
# **Çalıştırma:** Çalışma zamanı → Tümünü çalıştır (*Runtime → Run all*). Tahmini süre: 5–10 dakika.
#
# **Veri:** Varsayılan olarak veriler GOMD açık veri API'sinden canlı çekilir (`VERI_KAYNAGI = "api"`). Makaledeki sayıları birebir yeniden üretmek için replikasyon paketi ZIP'ini yükleyebilirsiniz (`VERI_KAYNAGI = "zip"`): API'deki seriler kaynak kurumlar tarafından güncellenirse sonuçlar küçük farklar gösterebilir. Veri çekim tarihi makalede 8 Kasım 2026'dır.
#
# Şekiller hem hücre altında görünür hem `fig/` klasörüne, sayısal sonuçlar `out/` klasörüne yazılır. Son hücre hepsini ZIP olarak indirir.

# ## 0. Kurulum

# Installs the packages used in the paper and the GOMD Python client. If Colab asks to restart the session, restart and run all cells again.
# [Colab Magic] !pip install -q scipy
# [Colab Magic] !pip install -q --upgrade gomd

VERI_KAYNAGI = "api"   # "api": live from GOMD; "zip": upload the replication package ZIP (exact reproduction)
import os
os.makedirs("/content/rp05", exist_ok=True); os.chdir("/content/rp05")
for d in ("data", "out", "fig"): os.makedirs(d, exist_ok=True)
if VERI_KAYNAGI == "zip":
    import zipfile, glob, shutil
    from google.colab import files
    up = files.upload()                      # choose *_replication_package.zip
    with zipfile.ZipFile(next(iter(up))) as z:
        for n in z.namelist():
            if n.startswith("data/"): z.extract(n, ".")
    print(sorted(os.listdir("data")))
print("Calisma klasoru:", os.getcwd())

# ## 1. Veri

# Data from the public GOMD open data API (entity lists as used in the paper). Requests run in a few parallel threads with retries.
import json, time
from concurrent.futures import ThreadPoolExecutor
import pandas as pd
from gomd import GOMDClient
client = GOMDClient("https://gomd.pirireis.edu.tr")
JOBS = [["wb_cwon_adjusted_net_savings_gni_pct", "ans_gni"], ["cwon_wb_total_natural_resources_rents_gdp_pct", "rents"], ["wb_cwon_carbon_dioxide_damage_gni_pct", "co2dmg_gni"], ["wb_cwon_natural_resources_depletion_gni_pct", "depletion_gni"]]
ENTITIES = json.loads("{\"wb_cwon_adjusted_net_savings_gni_pct\": [[\"TUR\", \"Turkiye\", \"country\"], [\"ALB\", \"Albania\", \"country\"], [\"DZA\", \"Algeria\", \"country\"], [\"DEU\", \"Germany\", \"country\"], [\"AGO\", \"Angola\", \"country\"], [\"ARG\", \"Argentina\", \"country\"], [\"ARM\", \"Armenia\", \"country\"], [\"AUS\", \"Australia\", \"country\"], [\"AUT\", \"Austria\", \"country\"], [\"AZE\", \"Azerbaijan\", \"country\"], [\"BHS\", \"Bahamas, The\", \"country\"], [\"BHR\", \"Bahrain\", \"country\"], [\"BGD\", \"Bangladesh\", \"country\"], [\"BRB\", \"Barbados\", \"country\"], [\"BLR\", \"Belarus\", \"country\"], [\"BEL\", \"Belgium\", \"country\"], [\"BLZ\", \"Belize\", \"country\"], [\"BEN\", \"Benin\", \"country\"], [\"BTN\", \"Bhutan\", \"country\"], [\"BOL\", \"Bolivia\", \"country\"], [\"BWA\", \"Botswana\", \"country\"], [\"BRA\", \"Brazil\", \"country\"], [\"BRN\", \"Brunei Darussalam\", \"country\"], [\"BGR\", \"Bulgaria\", \"country\"], [\"BFA\", \"Burkina Faso\", \"country\"], [\"BDI\", \"Burundi\", \"country\"], [\"CPV\", \"Cabo Verde\", \"country\"], [\"KHM\", \"Cambodia\", \"country\"], [\"CMR\", \"Cameroon\", \"country\"], [\"CAN\", \"Canada\", \"country\"], [\"CAF\", \"Central African Republic\", \"country\"], [\"TCD\", \"Chad\", \"country\"], [\"CHL\", \"Chile\", \"country\"], [\"CHN\", \"China\", \"country\"], [\"COL\", \"Colombia\", \"country\"], [\"COM\", \"Comoros\", \"country\"], [\"COG\", \"Congo, Rep.\", \"country\"], [\"CRI\", \"Costa Rica\", \"country\"], [\"CIV\", \"Cote d'Ivoire\", \"country\"], [\"HRV\", \"Croatia\", \"country\"], [\"CYP\", \"Greek Cypriot Administration of Southern Cyprus (GCA)\", \"country\"], [\"CZE\", \"Czechia\", \"country\"], [\"COD\", \"Democratic Republic of Congo\", \"country\"], [\"DNK\", \"Denmark\", \"country\"], [\"DJI\", \"Djibouti\", \"country\"], [\"DOM\", \"Dominican Republic\", \"country\"], [\"ECU\", \"Ecuador\", \"country\"], [\"EGY\", \"Egypt\", \"country\"], [\"SLV\", \"El Salvador\", \"country\"], [\"ERI\", \"Eritrea\", \"country\"], [\"EST\", \"Estonia\", \"country\"], [\"SWZ\", \"Eswatini\", \"country\"], [\"ETH\", \"Ethiopia\", \"country\"], [\"FJI\", \"Fiji\", \"country\"], [\"FIN\", \"Finland\", \"country\"], [\"FRA\", \"France\", \"country\"], [\"GAB\", \"Gabon\", \"country\"], [\"GMB\", \"Gambia, The\", \"country\"], [\"GEO\", \"Georgia\", \"country\"], [\"GHA\", \"Ghana\", \"country\"], [\"GTM\", \"Guatemala\", \"country\"], [\"GIN\", \"Guinea\", \"country\"], [\"GNB\", \"Guinea-Bissau\", \"country\"], [\"GUY\", \"Guyana\", \"country\"], [\"HTI\", \"Haiti\", \"country\"], [\"HND\", \"Honduras\", \"country\"], [\"HUN\", \"Hungary\", \"country\"], [\"ISL\", \"Iceland\", \"country\"], [\"IND\", \"India\", \"country\"], [\"IDN\", \"Indonesia\", \"country\"], [\"IRN\", \"Iran, Islamic Rep.\", \"country\"], [\"IRQ\", \"Iraq\", \"country\"], [\"IRL\", \"Ireland\", \"country\"], [\"ESP\", \"Spain\", \"country\"], [\"ISR\", \"Israel\", \"country\"], [\"ITA\", \"Italy\", \"country\"], [\"JAM\", \"Jamaica\", \"country\"], [\"JPN\", \"Japan\", \"country\"], [\"JOR\", \"Jordan\", \"country\"], [\"KAZ\", \"Kazakhstan\", \"country\"], [\"KEN\", \"Kenya\", \"country\"], [\"KOR\", \"Korea Rep\", \"country\"], [\"KWT\", \"Kuwait\", \"country\"], [\"KGZ\", \"Kyrgyzstan\", \"country\"], [\"LAO\", \"Laos\", \"country\"], [\"LVA\", \"Latvia\", \"country\"], [\"LBN\", \"Lebanon\", \"country\"], [\"LSO\", \"Lesotho\", \"country\"], [\"LBY\", \"Libya\", \"country\"], [\"LTU\", \"Lithuania\", \"country\"], [\"LUX\", \"Luxembourg\", \"country\"], [\"MDG\", \"Madagascar\", \"country\"], [\"MYS\", \"Malaysia\", \"country\"], [\"MDV\", \"Maldives\", \"country\"], [\"MLI\", \"Mali\", \"country\"], [\"MRT\", \"Mauritania\", \"country\"], [\"MUS\", \"Mauritius\", \"country\"], [\"MEX\", \"Mexico\", \"country\"], [\"MDA\", \"Moldova\", \"country\"], [\"MNG\", \"Mongolia\", \"country\"], [\"MAR\", \"Morocco\", \"country\"], [\"MOZ\", \"Mozambique\", \"country\"], [\"MMR\", \"Myanmar\", \"country\"], [\"NAM\", \"Namibia\", \"country\"], [\"NPL\", \"Nepal\", \"country\"], [\"NLD\", \"Netherlands\", \"country\"], [\"NZL\", \"New Zealand\", \"country\"], [\"NIC\", \"Nicaragua\", \"country\"], [\"NER\", \"Niger\", \"country\"], [\"NGA\", \"Nigeria\", \"country\"], [\"MKD\", \"North Macedonia\", \"country\"], [\"NOR\", \"Norway\", \"country\"], [\"OMN\", \"Oman\", \"country\"], [\"PAK\", \"Pakistan\", \"country\"], [\"PAN\", \"Panama\", \"country\"], [\"PNG\", \"Papua New Guinea\", \"country\"], [\"PRY\", \"Paraguay\", \"country\"], [\"PER\", \"Peru\", \"country\"], [\"PHL\", \"Philippines\", \"country\"], [\"POL\", \"Poland\", \"country\"], [\"PRT\", \"Portugal\", \"country\"], [\"QAT\", \"Qatar\", \"country\"], [\"ROU\", \"Romania\", \"country\"], [\"RUS\", \"Russian Federation\", \"country\"], [\"RWA\", \"Rwanda\", \"country\"], [\"WSM\", \"Samoa\", \"country\"], [\"SAU\", \"Saudi Arabia\", \"country\"], [\"SEN\", \"Senegal\", \"country\"], [\"SRB\", \"Serbia\", \"country\"], [\"SLE\", \"Sierra Leone\", \"country\"], [\"SGP\", \"Singapore\", \"country\"], [\"SVK\", \"Slovakia\", \"country\"], [\"SVN\", \"Slovenia\", \"country\"], [\"SLB\", \"Solomon Islands\", \"country\"], [\"ZAF\", \"South Africa\", \"country\"], [\"SSD\", \"South Sudan\", \"country\"], [\"LKA\", \"Sri Lanka\", \"country\"], [\"SDN\", \"Sudan\", \"country\"], [\"SUR\", \"Suriname\", \"country\"], [\"SWE\", \"Sweden\", \"country\"], [\"CHE\", \"Switzerland\", \"country\"], [\"SYR\", \"Syrian Arab Republic\", \"country\"], [\"TJK\", \"Tajikistan\", \"country\"], [\"TZA\", \"Tanzania\", \"country\"], [\"THA\", \"Thailand\", \"country\"], [\"TLS\", \"Timor-Leste\", \"country\"], [\"TGO\", \"Togo\", \"country\"], [\"TON\", \"Tonga\", \"country\"], [\"TUN\", \"Tunisia\", \"country\"], [\"UGA\", \"Uganda\", \"country\"], [\"GBR\", \"UK\", \"country\"], [\"UKR\", \"Ukraine\", \"country\"], [\"URY\", \"Uruguay\", \"country\"], [\"USA\", \"USA\", \"country\"], [\"UZB\", \"Uzbekistan\", \"country\"], [\"VUT\", \"Vanuatu\", \"country\"], [\"VNM\", \"Vietnam\", \"country\"], [\"GRC\", \"Greece\", \"country\"], [\"ZMB\", \"Zambia\", \"country\"], [\"ZWE\", \"Zimbabwe\", \"country\"]], \"cwon_wb_total_natural_resources_rents_gdp_pct\": [[\"TUR\", \"Turkiye\", \"country\"], [\"AFG\", \"Afghanistan\", \"country\"], [\"ALB\", \"Albania\", \"country\"], [\"DZA\", \"Algeria\", \"country\"], [\"DEU\", \"Germany\", \"country\"], [\"ASM\", \"American Samoa\", \"country\"], [\"AND\", \"Andorra\", \"country\"], [\"AGO\", \"Angola\", \"country\"], [\"ATG\", \"Antigua and Barbuda\", \"country\"], [\"ARG\", \"Argentina\", \"country\"], [\"ARM\", \"Armenia\", \"country\"], [\"ABW\", \"Aruba\", \"country\"], [\"AUS\", \"Australia\", \"country\"], [\"AUT\", \"Austria\", \"country\"], [\"EUU\", \"European Union\", \"aggregate\"], [\"ECS\", \"Europe & Central Asia\", \"aggregate\"], [\"AZE\", \"Azerbaijan\", \"country\"], [\"BHS\", \"Bahamas, The\", \"country\"], [\"BHR\", \"Bahrain\", \"country\"], [\"BGD\", \"Bangladesh\", \"country\"], [\"BRB\", \"Barbados\", \"country\"], [\"BLR\", \"Belarus\", \"country\"], [\"BEL\", \"Belgium\", \"country\"], [\"BLZ\", \"Belize\", \"country\"], [\"BEN\", \"Benin\", \"country\"], [\"BMU\", \"Bermuda\", \"country\"], [\"BTN\", \"Bhutan\", \"country\"], [\"BOL\", \"Bolivia\", \"country\"], [\"BIH\", \"Bosnia and Herzegovina\", \"country\"], [\"BWA\", \"Botswana\", \"country\"], [\"BRA\", \"Brazil\", \"country\"], [\"BRN\", \"Brunei Darussalam\", \"country\"], [\"BGR\", \"Bulgaria\", \"country\"], [\"BFA\", \"Burkina Faso\", \"country\"], [\"BDI\", \"Burundi\", \"country\"], [\"CPV\", \"Cabo Verde\", \"country\"], [\"KHM\", \"Cambodia\", \"country\"], [\"CMR\", \"Cameroon\", \"country\"], [\"CAN\", \"Canada\", \"country\"], [\"CYM\", \"Cayman Islands\", \"country\"], [\"CAF\", \"Central African Republic\", \"country\"], [\"TCD\", \"Chad\", \"country\"], [\"CHI\", \"Channel Islands\", \"country\"], [\"CHL\", \"Chile\", \"country\"], [\"CHN\", \"China\", \"country\"], [\"COL\", \"Colombia\", \"country\"], [\"COM\", \"Comoros\", \"country\"], [\"COG\", \"Congo, Rep.\", \"country\"], [\"CRI\", \"Costa Rica\", \"country\"], [\"CIV\", \"Cote d'Ivoire\", \"country\"], [\"HRV\", \"Croatia\", \"country\"], [\"CUB\", \"Cuba\", \"country\"], [\"CUW\", \"Curacao\", \"country\"], [\"CYP\", \"Greek Cypriot Administration of Southern Cyprus (GCA)\", \"country\"], [\"CZE\", \"Czechia\", \"country\"], [\"COD\", \"Democratic Republic of Congo\", \"country\"], [\"DNK\", \"Denmark\", \"country\"], [\"DJI\", \"Djibouti\", \"country\"], [\"EAS\", \"East Asia & Pacific\", \"aggregate\"], [\"DMA\", \"Dominica\", \"country\"], [\"DOM\", \"Dominican Republic\", \"country\"], [\"WLD\", \"World (Global)\", \"aggregate\"], [\"ECU\", \"Ecuador\", \"country\"], [\"EGY\", \"Egypt\", \"country\"], [\"SLV\", \"El Salvador\", \"country\"], [\"GNQ\", \"Equatorial Guinea\", \"country\"], [\"ERI\", \"Eritrea\", \"country\"], [\"EST\", \"Estonia\", \"country\"], [\"SWZ\", \"Eswatini\", \"country\"], [\"ETH\", \"Ethiopia\", \"country\"], [\"FRO\", \"Faroe Islands\", \"country\"], [\"FJI\", \"Fiji\", \"country\"], [\"FIN\", \"Finland\", \"country\"], [\"FRA\", \"France\", \"country\"], [\"PYF\", \"French Polynesia\", \"country\"], [\"GAB\", \"Gabon\", \"country\"], [\"GMB\", \"Gambia, The\", \"country\"], [\"GEO\", \"Georgia\", \"country\"], [\"GHA\", \"Ghana\", \"country\"], [\"GRL\", \"Greenland\", \"country\"], [\"GRD\", \"Grenada\", \"country\"], [\"GUM\", \"Guam\", \"country\"], [\"GTM\", \"Guatemala\", \"country\"], [\"GIN\", \"Guinea\", \"country\"], [\"GNB\", \"Guinea-Bissau\", \"country\"], [\"SAS\", \"South Asia\", \"aggregate\"], [\"GUY\", \"Guyana\", \"country\"], [\"HTI\", \"Haiti\", \"country\"], [\"HND\", \"Honduras\", \"country\"], [\"HKG\", \"Hong Kong SAR, China\", \"country\"], [\"HUN\", \"Hungary\", \"country\"], [\"ISL\", \"Iceland\", \"country\"], [\"IND\", \"India\", \"country\"], [\"IDN\", \"Indonesia\", \"country\"], [\"IRN\", \"Iran, Islamic Rep.\", \"country\"], [\"IRQ\", \"Iraq\", \"country\"], [\"IRL\", \"Ireland\", \"country\"], [\"IMN\", \"Isle of Man\", \"country\"], [\"ESP\", \"Spain\", \"country\"], [\"ISR\", \"Israel\", \"country\"], [\"ITA\", \"Italy\", \"country\"], [\"JAM\", \"Jamaica\", \"country\"], [\"JPN\", \"Japan\", \"country\"], [\"JOR\", \"Jordan\", \"country\"], [\"KAZ\", \"Kazakhstan\", \"country\"], [\"KEN\", \"Kenya\", \"country\"], [\"KIR\", \"Kiribati\", \"country\"], [\"KOR\", \"Korea Rep\", \"country\"], [\"XKX\", \"Kosovo\", \"country\"], [\"KWT\", \"Kuwait\", \"country\"], [\"NAC\", \"North America\", \"aggregate\"], [\"KGZ\", \"Kyrgyzstan\", \"country\"], [\"LAO\", \"Laos\", \"country\"], [\"LCN\", \"Latin America & Caribbean\", \"aggregate\"], [\"LVA\", \"Latvia\", \"country\"], [\"LBN\", \"Lebanon\", \"country\"], [\"LSO\", \"Lesotho\", \"country\"], [\"LBR\", \"Liberia\", \"country\"], [\"LBY\", \"Libya\", \"country\"], [\"LIE\", \"Liechtenstein\", \"country\"], [\"LTU\", \"Lithuania\", \"country\"], [\"LUX\", \"Luxembourg\", \"country\"], [\"MAC\", \"Macao SAR, China\", \"country\"], [\"MDG\", \"Madagascar\", \"country\"], [\"MWI\", \"Malawi\", \"country\"], [\"MYS\", \"Malaysia\", \"country\"], [\"MDV\", \"Maldives\", \"country\"], [\"MLI\", \"Mali\", \"country\"], [\"MLT\", \"Malta\", \"country\"], [\"MHL\", \"Marshall Islands\", \"country\"], [\"MRT\", \"Mauritania\", \"country\"], [\"MUS\", \"Mauritius\", \"country\"], [\"MEX\", \"Mexico\", \"country\"], [\"FSM\", \"Micronesia, Fed. Sts.\", \"country\"], [\"MDA\", \"Moldova\", \"country\"], [\"MCO\", \"Monaco\", \"country\"], [\"MNG\", \"Mongolia\", \"country\"], [\"MNE\", \"Montenegro\", \"country\"], [\"MAR\", \"Morocco\", \"country\"], [\"MOZ\", \"Mozambique\", \"country\"], [\"MMR\", \"Myanmar\", \"country\"], [\"NAM\", \"Namibia\", \"country\"], [\"NRU\", \"Nauru\", \"country\"], [\"NPL\", \"Nepal\", \"country\"], [\"NLD\", \"Netherlands\", \"country\"], [\"NCL\", \"New Caledonia\", \"country\"], [\"NZL\", \"New Zealand\", \"country\"], [\"NIC\", \"Nicaragua\", \"country\"], [\"NER\", \"Niger\", \"country\"], [\"NGA\", \"Nigeria\", \"country\"], [\"MKD\", \"North Macedonia\", \"country\"], [\"MNP\", \"Northern Mariana Islands\", \"country\"], [\"NOR\", \"Norway\", \"country\"], [\"OED\", \"OECD members\", \"aggregate\"], [\"OMN\", \"Oman\", \"country\"], [\"MEA\", \"Middle East, North Africa, Afghanistan & Pakistan\", \"aggregate\"], [\"PAK\", \"Pakistan\", \"country\"], [\"PLW\", \"Palau\", \"country\"], [\"PAN\", \"Panama\", \"country\"], [\"PNG\", \"Papua New Guinea\", \"country\"], [\"PRY\", \"Paraguay\", \"country\"], [\"PER\", \"Peru\", \"country\"], [\"PHL\", \"Philippines\", \"country\"], [\"POL\", \"Poland\", \"country\"], [\"PRT\", \"Portugal\", \"country\"], [\"PRI\", \"Puerto Rico\", \"country\"], [\"QAT\", \"Qatar\", \"country\"], [\"ROU\", \"Romania\", \"country\"], [\"RUS\", \"Russian Federation\", \"country\"], [\"RWA\", \"Rwanda\", \"country\"], [\"SSF\", \"Sub-Saharan Africa\", \"aggregate\"], [\"WSM\", \"Samoa\", \"country\"], [\"SMR\", \"San Marino\", \"country\"], [\"STP\", \"Sao Tome and Principe\", \"country\"], [\"SAU\", \"Saudi Arabia\", \"country\"], [\"SEN\", \"Senegal\", \"country\"], [\"SRB\", \"Serbia\", \"country\"], [\"SYC\", \"Seychelles\", \"country\"], [\"SLE\", \"Sierra Leone\", \"country\"], [\"SGP\", \"Singapore\", \"country\"], [\"SXM\", \"Sint Maarten (Dutch part)\", \"country\"], [\"SVK\", \"Slovakia\", \"country\"], [\"SVN\", \"Slovenia\", \"country\"], [\"SLB\", \"Solomon Islands\", \"country\"], [\"SOM\", \"Somalia\", \"country\"], [\"ZAF\", \"South Africa\", \"country\"], [\"SSD\", \"South Sudan\", \"country\"], [\"LKA\", \"Sri Lanka\", \"country\"], [\"KNA\", \"St. Kitts and Nevis\", \"country\"], [\"LCA\", \"St. Lucia\", \"country\"], [\"MAF\", \"St. Martin (French part)\", \"country\"], [\"VCT\", \"St. Vincent and the Grenadines\", \"country\"], [\"SDN\", \"Sudan\", \"country\"], [\"SUR\", \"Suriname\", \"country\"], [\"SWE\", \"Sweden\", \"country\"], [\"CHE\", \"Switzerland\", \"country\"], [\"SYR\", \"Syrian Arab Republic\", \"country\"], [\"TJK\", \"Tajikistan\", \"country\"], [\"TZA\", \"Tanzania\", \"country\"], [\"THA\", \"Thailand\", \"country\"], [\"TLS\", \"Timor-Leste\", \"country\"], [\"TGO\", \"Togo\", \"country\"], [\"TON\", \"Tonga\", \"country\"], [\"TTO\", \"Trinidad and Tobago\", \"country\"], [\"TUN\", \"Tunisia\", \"country\"], [\"TKM\", \"Turkmenistan\", \"country\"], [\"TCA\", \"Turks and Caicos Islands\", \"country\"], [\"TUV\", \"Tuvalu\", \"country\"], [\"UGA\", \"Uganda\", \"country\"], [\"GBR\", \"UK\", \"country\"], [\"UKR\", \"Ukraine\", \"country\"], [\"ARE\", \"United Arab Emirates\", \"country\"], [\"VIR\", \"United States Virgin Islands\", \"country\"], [\"URY\", \"Uruguay\", \"country\"], [\"USA\", \"USA\", \"country\"], [\"UZB\", \"Uzbekistan\", \"country\"], [\"VUT\", \"Vanuatu\", \"country\"], [\"VEN\", \"Venezuela, RB\", \"country\"], [\"VNM\", \"Vietnam\", \"country\"], [\"PSE\", \"West Bank and Gaza\", \"country\"], [\"YEM\", \"Yemen\", \"country\"], [\"GRC\", \"Greece\", \"country\"], [\"ZMB\", \"Zambia\", \"country\"], [\"ZWE\", \"Zimbabwe\", \"country\"]], \"wb_cwon_carbon_dioxide_damage_gni_pct\": [[\"TUR\", \"Turkiye\", \"country\"], [\"AFG\", \"Afghanistan\", \"country\"], [\"ALB\", \"Albania\", \"country\"], [\"DZA\", \"Algeria\", \"country\"], [\"DEU\", \"Germany\", \"country\"], [\"ASM\", \"American Samoa\", \"country\"], [\"AND\", \"Andorra\", \"country\"], [\"AGO\", \"Angola\", \"country\"], [\"ATG\", \"Antigua and Barbuda\", \"country\"], [\"ARG\", \"Argentina\", \"country\"], [\"ARM\", \"Armenia\", \"country\"], [\"ABW\", \"Aruba\", \"country\"], [\"AUS\", \"Australia\", \"country\"], [\"AUT\", \"Austria\", \"country\"], [\"AZE\", \"Azerbaijan\", \"country\"], [\"BHS\", \"Bahamas, The\", \"country\"], [\"BHR\", \"Bahrain\", \"country\"], [\"BGD\", \"Bangladesh\", \"country\"], [\"BRB\", \"Barbados\", \"country\"], [\"BLR\", \"Belarus\", \"country\"], [\"BEL\", \"Belgium\", \"country\"], [\"BLZ\", \"Belize\", \"country\"], [\"BEN\", \"Benin\", \"country\"], [\"BMU\", \"Bermuda\", \"country\"], [\"BTN\", \"Bhutan\", \"country\"], [\"BOL\", \"Bolivia\", \"country\"], [\"BIH\", \"Bosnia and Herzegovina\", \"country\"], [\"BWA\", \"Botswana\", \"country\"], [\"BRA\", \"Brazil\", \"country\"], [\"BRN\", \"Brunei Darussalam\", \"country\"], [\"BGR\", \"Bulgaria\", \"country\"], [\"BFA\", \"Burkina Faso\", \"country\"], [\"BDI\", \"Burundi\", \"country\"], [\"CPV\", \"Cabo Verde\", \"country\"], [\"KHM\", \"Cambodia\", \"country\"], [\"CMR\", \"Cameroon\", \"country\"], [\"CAN\", \"Canada\", \"country\"], [\"CYM\", \"Cayman Islands\", \"country\"], [\"CAF\", \"Central African Republic\", \"country\"], [\"TCD\", \"Chad\", \"country\"], [\"CHL\", \"Chile\", \"country\"], [\"CHN\", \"China\", \"country\"], [\"COL\", \"Colombia\", \"country\"], [\"COM\", \"Comoros\", \"country\"], [\"COG\", \"Congo, Rep.\", \"country\"], [\"CRI\", \"Costa Rica\", \"country\"], [\"CIV\", \"Cote d'Ivoire\", \"country\"], [\"HRV\", \"Croatia\", \"country\"], [\"CUB\", \"Cuba\", \"country\"], [\"CYP\", \"Greek Cypriot Administration of Southern Cyprus (GCA)\", \"country\"], [\"CZE\", \"Czechia\", \"country\"], [\"COD\", \"Democratic Republic of Congo\", \"country\"], [\"DNK\", \"Denmark\", \"country\"], [\"DJI\", \"Djibouti\", \"country\"], [\"DMA\", \"Dominica\", \"country\"], [\"DOM\", \"Dominican Republic\", \"country\"], [\"ECU\", \"Ecuador\", \"country\"], [\"EGY\", \"Egypt\", \"country\"], [\"SLV\", \"El Salvador\", \"country\"], [\"GNQ\", \"Equatorial Guinea\", \"country\"], [\"ERI\", \"Eritrea\", \"country\"], [\"EST\", \"Estonia\", \"country\"], [\"SWZ\", \"Eswatini\", \"country\"], [\"ETH\", \"Ethiopia\", \"country\"], [\"FRO\", \"Faroe Islands\", \"country\"], [\"FJI\", \"Fiji\", \"country\"], [\"FIN\", \"Finland\", \"country\"], [\"FRA\", \"France\", \"country\"], [\"PYF\", \"French Polynesia\", \"country\"], [\"GAB\", \"Gabon\", \"country\"], [\"GMB\", \"Gambia, The\", \"country\"], [\"GEO\", \"Georgia\", \"country\"], [\"GHA\", \"Ghana\", \"country\"], [\"GRL\", \"Greenland\", \"country\"], [\"GRD\", \"Grenada\", \"country\"], [\"GTM\", \"Guatemala\", \"country\"], [\"GIN\", \"Guinea\", \"country\"], [\"GNB\", \"Guinea-Bissau\", \"country\"], [\"GUY\", \"Guyana\", \"country\"], [\"HTI\", \"Haiti\", \"country\"], [\"HND\", \"Honduras\", \"country\"], [\"HKG\", \"Hong Kong SAR, China\", \"country\"], [\"HUN\", \"Hungary\", \"country\"], [\"ISL\", \"Iceland\", \"country\"], [\"IND\", \"India\", \"country\"], [\"IDN\", \"Indonesia\", \"country\"], [\"IRN\", \"Iran, Islamic Rep.\", \"country\"], [\"IRQ\", \"Iraq\", \"country\"], [\"IRL\", \"Ireland\", \"country\"], [\"ESP\", \"Spain\", \"country\"], [\"ISR\", \"Israel\", \"country\"], [\"ITA\", \"Italy\", \"country\"], [\"JAM\", \"Jamaica\", \"country\"], [\"JPN\", \"Japan\", \"country\"], [\"JOR\", \"Jordan\", \"country\"], [\"KAZ\", \"Kazakhstan\", \"country\"], [\"KEN\", \"Kenya\", \"country\"], [\"KIR\", \"Kiribati\", \"country\"], [\"KOR\", \"Korea Rep\", \"country\"], [\"XKX\", \"Kosovo\", \"country\"], [\"KWT\", \"Kuwait\", \"country\"], [\"KGZ\", \"Kyrgyzstan\", \"country\"], [\"LAO\", \"Laos\", \"country\"], [\"LVA\", \"Latvia\", \"country\"], [\"LBN\", \"Lebanon\", \"country\"], [\"LSO\", \"Lesotho\", \"country\"], [\"LBR\", \"Liberia\", \"country\"], [\"LBY\", \"Libya\", \"country\"], [\"LIE\", \"Liechtenstein\", \"country\"], [\"LTU\", \"Lithuania\", \"country\"], [\"LUX\", \"Luxembourg\", \"country\"], [\"MAC\", \"Macao SAR, China\", \"country\"], [\"MDG\", \"Madagascar\", \"country\"], [\"MWI\", \"Malawi\", \"country\"], [\"MYS\", \"Malaysia\", \"country\"], [\"MDV\", \"Maldives\", \"country\"], [\"MLI\", \"Mali\", \"country\"], [\"MLT\", \"Malta\", \"country\"], [\"MHL\", \"Marshall Islands\", \"country\"], [\"MRT\", \"Mauritania\", \"country\"], [\"MUS\", \"Mauritius\", \"country\"], [\"MEX\", \"Mexico\", \"country\"], [\"FSM\", \"Micronesia, Fed. Sts.\", \"country\"], [\"MDA\", \"Moldova\", \"country\"], [\"MNG\", \"Mongolia\", \"country\"], [\"MNE\", \"Montenegro\", \"country\"], [\"MAR\", \"Morocco\", \"country\"], [\"MOZ\", \"Mozambique\", \"country\"], [\"MMR\", \"Myanmar\", \"country\"], [\"NAM\", \"Namibia\", \"country\"], [\"NRU\", \"Nauru\", \"country\"], [\"NPL\", \"Nepal\", \"country\"], [\"NLD\", \"Netherlands\", \"country\"], [\"NCL\", \"New Caledonia\", \"country\"], [\"NZL\", \"New Zealand\", \"country\"], [\"NIC\", \"Nicaragua\", \"country\"], [\"NER\", \"Niger\", \"country\"], [\"NGA\", \"Nigeria\", \"country\"], [\"MKD\", \"North Macedonia\", \"country\"], [\"NOR\", \"Norway\", \"country\"], [\"OMN\", \"Oman\", \"country\"], [\"PAK\", \"Pakistan\", \"country\"], [\"PLW\", \"Palau\", \"country\"], [\"PAN\", \"Panama\", \"country\"], [\"PNG\", \"Papua New Guinea\", \"country\"], [\"PRY\", \"Paraguay\", \"country\"], [\"PER\", \"Peru\", \"country\"], [\"PHL\", \"Philippines\", \"country\"], [\"POL\", \"Poland\", \"country\"], [\"PRT\", \"Portugal\", \"country\"], [\"PRI\", \"Puerto Rico\", \"country\"], [\"QAT\", \"Qatar\", \"country\"], [\"ROU\", \"Romania\", \"country\"], [\"RUS\", \"Russian Federation\", \"country\"], [\"RWA\", \"Rwanda\", \"country\"], [\"WSM\", \"Samoa\", \"country\"], [\"STP\", \"Sao Tome and Principe\", \"country\"], [\"SAU\", \"Saudi Arabia\", \"country\"], [\"SEN\", \"Senegal\", \"country\"], [\"SRB\", \"Serbia\", \"country\"], [\"SYC\", \"Seychelles\", \"country\"], [\"SLE\", \"Sierra Leone\", \"country\"], [\"SGP\", \"Singapore\", \"country\"], [\"SVK\", \"Slovakia\", \"country\"], [\"SVN\", \"Slovenia\", \"country\"], [\"SLB\", \"Solomon Islands\", \"country\"], [\"SOM\", \"Somalia\", \"country\"], [\"ZAF\", \"South Africa\", \"country\"], [\"SSD\", \"South Sudan\", \"country\"], [\"LKA\", \"Sri Lanka\", \"country\"], [\"KNA\", \"St. Kitts and Nevis\", \"country\"], [\"LCA\", \"St. Lucia\", \"country\"], [\"VCT\", \"St. Vincent and the Grenadines\", \"country\"], [\"SDN\", \"Sudan\", \"country\"], [\"SUR\", \"Suriname\", \"country\"], [\"SWE\", \"Sweden\", \"country\"], [\"CHE\", \"Switzerland\", \"country\"], [\"SYR\", \"Syrian Arab Republic\", \"country\"], [\"TJK\", \"Tajikistan\", \"country\"], [\"TZA\", \"Tanzania\", \"country\"], [\"THA\", \"Thailand\", \"country\"], [\"TLS\", \"Timor-Leste\", \"country\"], [\"TGO\", \"Togo\", \"country\"], [\"TON\", \"Tonga\", \"country\"], [\"TTO\", \"Trinidad and Tobago\", \"country\"], [\"TUN\", \"Tunisia\", \"country\"], [\"TKM\", \"Turkmenistan\", \"country\"], [\"TCA\", \"Turks and Caicos Islands\", \"country\"], [\"TUV\", \"Tuvalu\", \"country\"], [\"UGA\", \"Uganda\", \"country\"], [\"GBR\", \"UK\", \"country\"], [\"UKR\", \"Ukraine\", \"country\"], [\"ARE\", \"United Arab Emirates\", \"country\"], [\"URY\", \"Uruguay\", \"country\"], [\"USA\", \"USA\", \"country\"], [\"UZB\", \"Uzbekistan\", \"country\"], [\"VUT\", \"Vanuatu\", \"country\"], [\"VEN\", \"Venezuela, RB\", \"country\"], [\"VNM\", \"Vietnam\", \"country\"], [\"PSE\", \"West Bank and Gaza\", \"country\"], [\"YEM\", \"Yemen\", \"country\"], [\"GRC\", \"Greece\", \"country\"], [\"ZMB\", \"Zambia\", \"country\"], [\"ZWE\", \"Zimbabwe\", \"country\"]], \"wb_cwon_natural_resources_depletion_gni_pct\": [[\"TUR\", \"Turkiye\", \"country\"], [\"AFG\", \"Afghanistan\", \"country\"], [\"ALB\", \"Albania\", \"country\"], [\"DZA\", \"Algeria\", \"country\"], [\"DEU\", \"Germany\", \"country\"], [\"AGO\", \"Angola\", \"country\"], [\"ARG\", \"Argentina\", \"country\"], [\"ARM\", \"Armenia\", \"country\"], [\"ABW\", \"Aruba\", \"country\"], [\"AUS\", \"Australia\", \"country\"], [\"AUT\", \"Austria\", \"country\"], [\"AZE\", \"Azerbaijan\", \"country\"], [\"BHS\", \"Bahamas, The\", \"country\"], [\"BHR\", \"Bahrain\", \"country\"], [\"BGD\", \"Bangladesh\", \"country\"], [\"BRB\", \"Barbados\", \"country\"], [\"BLR\", \"Belarus\", \"country\"], [\"BEL\", \"Belgium\", \"country\"], [\"BLZ\", \"Belize\", \"country\"], [\"BEN\", \"Benin\", \"country\"], [\"BTN\", \"Bhutan\", \"country\"], [\"BOL\", \"Bolivia\", \"country\"], [\"BIH\", \"Bosnia and Herzegovina\", \"country\"], [\"BWA\", \"Botswana\", \"country\"], [\"BRA\", \"Brazil\", \"country\"], [\"BRN\", \"Brunei Darussalam\", \"country\"], [\"BGR\", \"Bulgaria\", \"country\"], [\"BFA\", \"Burkina Faso\", \"country\"], [\"BDI\", \"Burundi\", \"country\"], [\"CPV\", \"Cabo Verde\", \"country\"], [\"KHM\", \"Cambodia\", \"country\"], [\"CMR\", \"Cameroon\", \"country\"], [\"CAN\", \"Canada\", \"country\"], [\"CAF\", \"Central African Republic\", \"country\"], [\"TCD\", \"Chad\", \"country\"], [\"CHL\", \"Chile\", \"country\"], [\"CHN\", \"China\", \"country\"], [\"COL\", \"Colombia\", \"country\"], [\"COM\", \"Comoros\", \"country\"], [\"COG\", \"Congo, Rep.\", \"country\"], [\"CRI\", \"Costa Rica\", \"country\"], [\"CIV\", \"Cote d'Ivoire\", \"country\"], [\"HRV\", \"Croatia\", \"country\"], [\"CUB\", \"Cuba\", \"country\"], [\"CYP\", \"Greek Cypriot Administration of Southern Cyprus (GCA)\", \"country\"], [\"CZE\", \"Czechia\", \"country\"], [\"COD\", \"Democratic Republic of Congo\", \"country\"], [\"DNK\", \"Denmark\", \"country\"], [\"DJI\", \"Djibouti\", \"country\"], [\"DMA\", \"Dominica\", \"country\"], [\"DOM\", \"Dominican Republic\", \"country\"], [\"ECU\", \"Ecuador\", \"country\"], [\"EGY\", \"Egypt\", \"country\"], [\"SLV\", \"El Salvador\", \"country\"], [\"GNQ\", \"Equatorial Guinea\", \"country\"], [\"ERI\", \"Eritrea\", \"country\"], [\"EST\", \"Estonia\", \"country\"], [\"SWZ\", \"Eswatini\", \"country\"], [\"ETH\", \"Ethiopia\", \"country\"], [\"FJI\", \"Fiji\", \"country\"], [\"FIN\", \"Finland\", \"country\"], [\"FRA\", \"France\", \"country\"], [\"PYF\", \"French Polynesia\", \"country\"], [\"GAB\", \"Gabon\", \"country\"], [\"GMB\", \"Gambia, The\", \"country\"], [\"GEO\", \"Georgia\", \"country\"], [\"GHA\", \"Ghana\", \"country\"], [\"GTM\", \"Guatemala\", \"country\"], [\"GIN\", \"Guinea\", \"country\"], [\"GNB\", \"Guinea-Bissau\", \"country\"], [\"GUY\", \"Guyana\", \"country\"], [\"HTI\", \"Haiti\", \"country\"], [\"HND\", \"Honduras\", \"country\"], [\"HKG\", \"Hong Kong SAR, China\", \"country\"], [\"HUN\", \"Hungary\", \"country\"], [\"ISL\", \"Iceland\", \"country\"], [\"IND\", \"India\", \"country\"], [\"IDN\", \"Indonesia\", \"country\"], [\"IRN\", \"Iran, Islamic Rep.\", \"country\"], [\"IRQ\", \"Iraq\", \"country\"], [\"IRL\", \"Ireland\", \"country\"], [\"ESP\", \"Spain\", \"country\"], [\"ISR\", \"Israel\", \"country\"], [\"ITA\", \"Italy\", \"country\"], [\"JAM\", \"Jamaica\", \"country\"], [\"JPN\", \"Japan\", \"country\"], [\"JOR\", \"Jordan\", \"country\"], [\"KAZ\", \"Kazakhstan\", \"country\"], [\"KEN\", \"Kenya\", \"country\"], [\"KIR\", \"Kiribati\", \"country\"], [\"KOR\", \"Korea Rep\", \"country\"], [\"KWT\", \"Kuwait\", \"country\"], [\"KGZ\", \"Kyrgyzstan\", \"country\"], [\"LAO\", \"Laos\", \"country\"], [\"LVA\", \"Latvia\", \"country\"], [\"LBN\", \"Lebanon\", \"country\"], [\"LSO\", \"Lesotho\", \"country\"], [\"LBR\", \"Liberia\", \"country\"], [\"LBY\", \"Libya\", \"country\"], [\"LTU\", \"Lithuania\", \"country\"], [\"LUX\", \"Luxembourg\", \"country\"], [\"MDG\", \"Madagascar\", \"country\"], [\"MWI\", \"Malawi\", \"country\"], [\"MYS\", \"Malaysia\", \"country\"], [\"MDV\", \"Maldives\", \"country\"], [\"MLI\", \"Mali\", \"country\"], [\"MRT\", \"Mauritania\", \"country\"], [\"MUS\", \"Mauritius\", \"country\"], [\"MEX\", \"Mexico\", \"country\"], [\"FSM\", \"Micronesia, Fed. Sts.\", \"country\"], [\"MDA\", \"Moldova\", \"country\"], [\"MNG\", \"Mongolia\", \"country\"], [\"MNE\", \"Montenegro\", \"country\"], [\"MAR\", \"Morocco\", \"country\"], [\"MOZ\", \"Mozambique\", \"country\"], [\"MMR\", \"Myanmar\", \"country\"], [\"NAM\", \"Namibia\", \"country\"], [\"NPL\", \"Nepal\", \"country\"], [\"NLD\", \"Netherlands\", \"country\"], [\"NCL\", \"New Caledonia\", \"country\"], [\"NZL\", \"New Zealand\", \"country\"], [\"NIC\", \"Nicaragua\", \"country\"], [\"NER\", \"Niger\", \"country\"], [\"NGA\", \"Nigeria\", \"country\"], [\"MKD\", \"North Macedonia\", \"country\"], [\"NOR\", \"Norway\", \"country\"], [\"OMN\", \"Oman\", \"country\"], [\"PAK\", \"Pakistan\", \"country\"], [\"PAN\", \"Panama\", \"country\"], [\"PNG\", \"Papua New Guinea\", \"country\"], [\"PRY\", \"Paraguay\", \"country\"], [\"PER\", \"Peru\", \"country\"], [\"PHL\", \"Philippines\", \"country\"], [\"POL\", \"Poland\", \"country\"], [\"PRT\", \"Portugal\", \"country\"], [\"QAT\", \"Qatar\", \"country\"], [\"ROU\", \"Romania\", \"country\"], [\"RUS\", \"Russian Federation\", \"country\"], [\"RWA\", \"Rwanda\", \"country\"], [\"WSM\", \"Samoa\", \"country\"], [\"STP\", \"Sao Tome and Principe\", \"country\"], [\"SAU\", \"Saudi Arabia\", \"country\"], [\"SEN\", \"Senegal\", \"country\"], [\"SRB\", \"Serbia\", \"country\"], [\"SYC\", \"Seychelles\", \"country\"], [\"SLE\", \"Sierra Leone\", \"country\"], [\"SGP\", \"Singapore\", \"country\"], [\"SVK\", \"Slovakia\", \"country\"], [\"SVN\", \"Slovenia\", \"country\"], [\"SLB\", \"Solomon Islands\", \"country\"], [\"SOM\", \"Somalia\", \"country\"], [\"ZAF\", \"South Africa\", \"country\"], [\"SSD\", \"South Sudan\", \"country\"], [\"LKA\", \"Sri Lanka\", \"country\"], [\"LCA\", \"St. Lucia\", \"country\"], [\"VCT\", \"St. Vincent and the Grenadines\", \"country\"], [\"SDN\", \"Sudan\", \"country\"], [\"SUR\", \"Suriname\", \"country\"], [\"SWE\", \"Sweden\", \"country\"], [\"CHE\", \"Switzerland\", \"country\"], [\"SYR\", \"Syrian Arab Republic\", \"country\"], [\"TJK\", \"Tajikistan\", \"country\"], [\"TZA\", \"Tanzania\", \"country\"], [\"THA\", \"Thailand\", \"country\"], [\"TLS\", \"Timor-Leste\", \"country\"], [\"TGO\", \"Togo\", \"country\"], [\"TON\", \"Tonga\", \"country\"], [\"TTO\", \"Trinidad and Tobago\", \"country\"], [\"TUN\", \"Tunisia\", \"country\"], [\"TKM\", \"Turkmenistan\", \"country\"], [\"TCA\", \"Turks and Caicos Islands\", \"country\"], [\"UGA\", \"Uganda\", \"country\"], [\"GBR\", \"UK\", \"country\"], [\"UKR\", \"Ukraine\", \"country\"], [\"ARE\", \"United Arab Emirates\", \"country\"], [\"URY\", \"Uruguay\", \"country\"], [\"USA\", \"USA\", \"country\"], [\"UZB\", \"Uzbekistan\", \"country\"], [\"VUT\", \"Vanuatu\", \"country\"], [\"VEN\", \"Venezuela, RB\", \"country\"], [\"VNM\", \"Vietnam\", \"country\"], [\"YEM\", \"Yemen\", \"country\"], [\"GRC\", \"Greece\", \"country\"], [\"ZMB\", \"Zambia\", \"country\"], [\"ZWE\", \"Zimbabwe\", \"country\"]]}")
CAP = {}

def fetch(indicator, entity, retries=6):
    for k in range(retries):
        try:
            rows = client.get_series(indicator=indicator, entity=entity, limit=50000, as_df=False)
            return [(str(r["period"]), float(r["value"])) for r in rows if str(r.get("entity_code") or "") == entity and r.get("value") is not None]
        except Exception as e:
            if k == retries - 1: raise
            time.sleep(2 ** k)

def fetch_panel(indicator, out_csv):
    ents = ENTITIES[indicator]
    with ThreadPoolExecutor(max_workers=4) as ex:
        res = list(ex.map(lambda e: fetch(indicator, e[0]), ents))
    recs = [(c, n, t, p, v) for (c, n, t), rows in zip(ents, res) for p, v in rows]
    df = pd.DataFrame(recs, columns=["entity", "name", "etype", "period", "value"]).drop_duplicates(["entity", "period"])
    if indicator in CAP: df = df[df.period <= CAP[indicator]]
    df.to_csv(out_csv, index=False)
    return df

if VERI_KAYNAGI == "api":
    for ind, f in JOBS:
        t = time.time(); df = fetch_panel(ind, f"data/{f}.csv")
        print(f"{f:28s} {len(df):7d} rows  {df.entity.nunique():4d} entities  {df.period.min()}-{df.period.max()}  ({time.time() - t:.0f} s)")

# ## 2. Markov zincirleri, ergodik dağılımlar, homojenlik testleri ve çekirdek

# Paper 5: distribution dynamics of adjusted net savings (% of GNI), 1990-2021. Discrete Markov chains, ergodic distributions, mobility, homogeneity tests, conditioning on resource rents, stochastic kernels.
import json, warnings
import numpy as np, pandas as pd
from scipy import stats
from scipy.stats import gaussian_kde
import matplotlib
import matplotlib.pyplot as plt
warnings.filterwarnings("ignore")
plt.rcParams.update({"font.family": "DejaVu Serif", "font.size": 9, "axes.titlesize": 10, "legend.fontsize": 8,
                     "savefig.dpi": 300, "axes.spines.top": False, "axes.spines.right": False})
R = {}
def panel(f):
    d = pd.read_csv(f"data/{f}.csv", dtype={"period": str}); d = d[d.etype == "country"]; d["y"] = d.period.str[:4].astype(int)
    return d.pivot_table(index="y", columns="entity", values="value")
ANS = panel("ans_gni").loc[1990:2021]; RENT = panel("rents"); CO2D = panel("co2dmg_gni"); DEP = panel("depletion_gni")
names = pd.read_csv("data/ans_gni.csv").drop_duplicates("entity").set_index("entity").name
R["n_countries"] = int(ANS.notna().any().sum()); R["n_obs"] = int(ANS.notna().sum().sum())
R["obs_by_year"] = {int(y): int(ANS.loc[y].notna().sum()) for y in (1990, 2000, 2010, 2019, 2020, 2021)}
EDGES = [-np.inf, -5, 0, 5, 10, 20, np.inf]
LAB = ["< −5", "−5 to 0", "0 to 5", "5 to 10", "10 to 20", "≥ 20"]
K = len(LAB)
def cls(v): return pd.cut(v, EDGES, labels=False, right=False)
C = ANS.apply(cls)
# descriptive
desc = {}
for y in (1990, 2000, 2010, 2019, 2021):
    v = ANS.loc[y].dropna(); desc[y] = {"n": int(len(v)), "mean": round(float(v.mean()), 2), "median": round(float(v.median()), 2), "sd": round(float(v.std()), 2),
                                        "p10": round(float(v.quantile(0.1)), 2), "p90": round(float(v.quantile(0.9)), 2), "share_neg": round(float((v < 0).mean()), 3),
                                        "shares": [round(float((cls(v) == k).mean()), 3) for k in range(K)]}
R["desc"] = desc
pool = ANS.stack(); R["pooled_class_counts"] = [int((cls(pool) == k).sum()) for k in range(K)]

def transitions(lag=1, years=None, members=None, cond=None):
    rows = []
    for y in ANS.index:
        if y + lag not in ANS.index: continue
        if years and not (years[0] <= y and y + lag <= years[1]): continue
        a = C.loc[y]; b = C.loc[y + lag]
        for c in ANS.columns:
            if members is not None and c not in members: continue
            if cond is not None and not cond(c, y): continue
            if pd.notna(a[c]) and pd.notna(b[c]): rows.append((int(a[c]), int(b[c])))
    N = np.zeros((K, K))
    for i, j in rows: N[i, j] += 1
    return N

def mat(N):
    with np.errstate(invalid="ignore", divide="ignore"):
        P = N / N.sum(axis=1, keepdims=True)
    return np.nan_to_num(P)

def ergodic(P):
    w, v = np.linalg.eig(P.T); k = np.argmin(abs(w - 1)); pi = np.real(v[:, k]); return pi / pi.sum()

def shorrocks(P): return float((K - np.trace(P)) / (K - 1))

def homogeneity(Ns):
    """Likelihood-ratio test that several transition count matrices share one transition matrix (Anderson-Goodman; Bickenbach-Bode)."""
    Ntot = sum(Ns); Ptot = mat(Ntot); lr = 0.0; df = 0
    for N in Ns:
        Pg = mat(N)
        for i in range(K):
            nz = (N[i] > 0)
            if N[i].sum() == 0: continue
            lr += 2 * float((N[i, nz] * np.log(Pg[i, nz] / Ptot[i, nz])).sum())
    for i in range(K):
        a_i = int((Ntot[i] > 0).sum()); groups = sum(1 for N in Ns if N[i].sum() > 0)
        df += (a_i - 1) * (groups - 1)
    return {"LR": round(lr, 2), "df": int(df), "p": float(stats.chi2.sf(lr, df))}

N1 = transitions(1); P1 = mat(N1); pi1 = ergodic(P1)
N5 = transitions(5); P5 = mat(N5); pi5 = ergodic(P5)
R["annual"] = {"n": int(N1.sum()), "row_n": N1.sum(axis=1).astype(int).tolist(), "P": np.round(P1, 3).tolist(), "ergodic": np.round(pi1, 3).tolist(),
               "shorrocks": round(shorrocks(P1), 3), "expected_stay": [round(float(1 / (1 - P1[i, i])), 1) if P1[i, i] < 1 else None for i in range(K)]}
R["five_year"] = {"n": int(N5.sum()), "row_n": N5.sum(axis=1).astype(int).tolist(), "P": np.round(P5, 3).tolist(), "ergodic": np.round(pi5, 3).tolist(), "shorrocks": round(shorrocks(P5), 3)}
P1_5 = np.linalg.matrix_power(P1, 5)
R["markov_check"] = {"P1^5": np.round(P1_5, 3).tolist(), "max_abs_diff": round(float(abs(P1_5 - P5).max()), 3), "diag_P5": np.round(np.diag(P5), 3).tolist(), "diag_P1^5": np.round(np.diag(P1_5), 3).tolist()}
# time homogeneity
Na = transitions(1, years=(1990, 2005)); Nb = transitions(1, years=(2006, 2021))
R["time_homog"] = homogeneity([Na, Nb]) | {"ergodic_1990_2005": np.round(ergodic(mat(Na)), 3).tolist(), "ergodic_2006_2021": np.round(ergodic(mat(Nb)), 3).tolist(),
                                           "n_a": int(Na.sum()), "n_b": int(Nb.sum()), "P_a": np.round(mat(Na), 3).tolist(), "P_b": np.round(mat(Nb), 3).tolist()}
# conditioning on resource rents (rents >= 10% of GDP in the starting year)
def rich(c, y):
    try: return RENT.loc[y, c] >= 10
    except Exception: return False
def poor(c, y):
    try: return RENT.loc[y, c] < 10
    except Exception: return False
Nr = transitions(1, cond=rich); Np = transitions(1, cond=poor)
R["rents"] = homogeneity([Nr, Np]) | {"n_rich": int(Nr.sum()), "n_other": int(Np.sum()), "P_rich": np.round(mat(Nr), 3).tolist(), "P_other": np.round(mat(Np), 3).tolist(),
                                      "ergodic_rich": np.round(ergodic(mat(Nr)), 3).tolist(), "ergodic_other": np.round(ergodic(mat(Np)), 3).tolist(),
                                      "shorrocks_rich": round(shorrocks(mat(Nr)), 3), "shorrocks_other": round(shorrocks(mat(Np)), 3),
                                      "row_n_rich": Nr.sum(axis=1).astype(int).tolist(), "row_n_other": Np.sum(axis=1).astype(int).tolist()}
# robustness: rents threshold 5% and 15%
rob = {}
for thr in (5, 15):
    Nr_ = transitions(1, cond=lambda c, y: (c in RENT.columns and y in RENT.index and pd.notna(RENT.loc[y, c]) and RENT.loc[y, c] >= thr))
    Np_ = transitions(1, cond=lambda c, y: (c in RENT.columns and y in RENT.index and pd.notna(RENT.loc[y, c]) and RENT.loc[y, c] < thr))
    rob[f"rents_{thr}"] = homogeneity([Nr_, Np_]) | {"ergodic_rich": np.round(ergodic(mat(Nr_)), 3).tolist(), "ergodic_other": np.round(ergodic(mat(Np_)), 3).tolist(), "n_rich": int(Nr_.sum())}
# robustness: balanced panel 1995-2021
bal = ANS.loc[1995:2021].dropna(axis=1).columns
Nbal = transitions(1, years=(1995, 2021), members=set(bal))
rob["balanced_1995_2021"] = {"N": int(len(bal)), "n": int(Nbal.sum()), "ergodic": np.round(ergodic(mat(Nbal)), 3).tolist(), "shorrocks": round(shorrocks(mat(Nbal)), 3)}
# robustness: excluding COVID transitions (2019->2020, 2020->2021)
Nnc = transitions(1, years=(1990, 2019))
rob["excl_covid"] = {"n": int(Nnc.sum()), "ergodic": np.round(ergodic(mat(Nnc)), 3).tolist()}
# robustness: quintile classes (relative distribution, Quah-style)
rel = ANS.sub(ANS.mean(axis=1), axis=0)
qs = np.nanquantile(rel.values, [0.2, 0.4, 0.6, 0.8])
Cq = rel.apply(lambda v: pd.cut(v, [-np.inf, *qs, np.inf], labels=False))
Nq = np.zeros((5, 5))
for y in ANS.index[:-1]:
    a = Cq.loc[y]; b = Cq.loc[y + 1]
    for c in ANS.columns:
        if pd.notna(a[c]) and pd.notna(b[c]): Nq[int(a[c]), int(b[c])] += 1
Pq = Nq / Nq.sum(axis=1, keepdims=True); w, v = np.linalg.eig(Pq.T); piq = np.real(v[:, np.argmin(abs(w - 1))]); piq /= piq.sum()
rob["relative_quintiles"] = {"cuts": np.round(qs, 2).tolist(), "P": np.round(Pq, 3).tolist(), "ergodic": np.round(piq, 3).tolist(), "shorrocks": round(float((5 - np.trace(Pq)) / 4), 3)}
R["robustness"] = rob
# mean depletion, CO2 damage and rents by class (pooled)
L = pd.DataFrame({"ans": ANS.stack(), "dep": DEP.loc[1990:2021].reindex(columns=ANS.columns).stack(), "co2": CO2D.loc[1990:2021].reindex(columns=ANS.columns).stack(),
                  "rent": RENT.loc[1990:2021].reindex(columns=ANS.columns).stack()}).dropna(subset=["ans"])
L["k"] = cls(L.ans)
R["profile_by_class"] = {LAB[int(k)]: {"n": int(len(g)), "depletion": round(float(g.dep.mean()), 2), "co2_damage": round(float(g.co2.mean()), 2), "rents": round(float(g.rent.mean()), 2)} for k, g in L.groupby("k")}
R["corr_ans_dep"] = round(float(L[["ans", "dep"]].dropna().corr(method="spearman").iloc[0, 1]), 3)
R["corr_ans_rent"] = round(float(L[["ans", "rent"]].dropna().corr(method="spearman").iloc[0, 1]), 3)
# persistent negative savers
neg_share = (ANS < 0).sum() / ANS.notna().sum(); nobs = ANS.notna().sum()
R["persistent_negative"] = {names.get(c, c): round(float(neg_share[c]), 2) for c in neg_share[(neg_share >= 0.5) & (nobs >= 15)].sort_values(ascending=False).index}
R["n_persistent_negative"] = int(((neg_share >= 0.5) & (nobs >= 15)).sum())
# Türkiye
R["tur"] = {int(y): round(float(v), 2) for y, v in ANS["TUR"].dropna().items()} if "TUR" in ANS else None
R["tur_class"] = {int(y): LAB[int(v)] for y, v in C["TUR"].dropna().items()} if "TUR" in C else None
# sigma
R["sd_by_year"] = {int(y): round(float(ANS.loc[y].std()), 2) for y in ANS.index}
R["median_by_year"] = {int(y): round(float(ANS.loc[y].median()), 2) for y in ANS.index}
# balanced-panel sigma (1995-2021)
R["sd_bal"] = {int(y): round(float(ANS.loc[y, bal].std()), 2) for y in (1995, 2005, 2015, 2019, 2021)}
json.dump(R, open("out/results.json", "w"), indent=1, default=str)

# ================= FIGURES =================
fig, ax = plt.subplots(1, 2, figsize=(9, 3.4))
yrs = ANS.index
ax[0].fill_between(yrs, ANS.quantile(0.1, axis=1), ANS.quantile(0.9, axis=1), color="#9ecae1", alpha=0.5, lw=0, label="10th–90th percentile")
ax[0].fill_between(yrs, ANS.quantile(0.25, axis=1), ANS.quantile(0.75, axis=1), color="#4292c6", alpha=0.5, lw=0, label="Interquartile range")
ax[0].plot(yrs, ANS.median(axis=1), color="k", lw=1.3, label="Median")
if "TUR" in ANS: ax[0].plot(yrs, ANS["TUR"], color="#d62728", ls="--", lw=1.1, label="Türkiye")
ax[0].axhline(0, color="k", lw=0.6); ax[0].set_ylabel("ANS (% of GNI)"); ax[0].set_title("(a) Cross-country distribution"); ax[0].legend(frameon=False, fontsize=7)
ax[1].plot(yrs, (ANS < 0).sum(axis=1) / ANS.notna().sum(axis=1) * 100, color="#d62728", label="Share with ANS < 0 (%)")
ax2 = ax[1].twinx(); ax2.plot(yrs, ANS.notna().sum(axis=1), color="grey", ls=":", label="Countries with data"); ax2.set_ylabel("Countries", color="grey")
ax[1].set_ylabel("Share with negative ANS (%)", color="#d62728"); ax[1].set_title("(b) Negative savers and coverage"); ax2.spines["top"].set_visible(False)
fig.tight_layout(); fig.savefig("fig/fig1_distribution.png"); plt.show()
# transition matrix heatmap + ergodic
fig, ax = plt.subplots(1, 2, figsize=(9, 3.6), gridspec_kw={"width_ratios": [1.25, 1]})
im = ax[0].imshow(P5, cmap="Blues", vmin=0, vmax=1)
for i in range(K):
    for j in range(K): ax[0].text(j, i, f"{P5[i, j]:.2f}", ha="center", va="center", fontsize=7, color="white" if P5[i, j] > 0.5 else "black")
ax[0].set_xticks(range(K)); ax[0].set_xticklabels(LAB, rotation=30, fontsize=7); ax[0].set_yticks(range(K)); ax[0].set_yticklabels(LAB, fontsize=7)
ax[0].set_xlabel("Class at t + 5"); ax[0].set_ylabel("Class at t"); ax[0].set_title("(a) Five-year transition probabilities")
x = np.arange(K); w_ = 0.27
ax[1].bar(x - w_, desc[1990]["shares"], w_, color="#c6dbef", label="1990")
ax[1].bar(x, desc[2021]["shares"], w_, color="#6baed6", label="2021")
ax[1].bar(x + w_, pi1, w_, color="#08519c", label="Ergodic (annual chain)")
ax[1].set_xticks(x); ax[1].set_xticklabels(LAB, rotation=30, fontsize=7); ax[1].set_ylabel("Share of countries"); ax[1].legend(frameon=False, fontsize=7); ax[1].set_title("(b) Initial, final and ergodic distributions")
fig.tight_layout(); fig.savefig("fig/fig2_transitions.png"); plt.show()
# stochastic kernel (5-year), contour of conditional density
pairs = [(ANS.loc[y, c], ANS.loc[y + 5, c]) for y in ANS.index if y + 5 in ANS.index for c in ANS.columns if pd.notna(ANS.loc[y, c]) and pd.notna(ANS.loc[y + 5, c])]
pr = np.array(pairs); pr = pr[(abs(pr[:, 0]) < 60) & (abs(pr[:, 1]) < 60)]
R["kernel_pairs"] = int(len(pr))
kde = gaussian_kde(pr.T); g = np.linspace(-30, 45, 120); X_, Y_ = np.meshgrid(g, g)
Z = kde(np.vstack([X_.ravel(), Y_.ravel()])).reshape(X_.shape); Zc = Z / Z.sum(axis=0, keepdims=True) / (g[1] - g[0])
fig, ax = plt.subplots(figsize=(5.2, 4.4))
cs = ax.contour(X_, Y_, Zc, levels=8, cmap="viridis", linewidths=0.9); ax.plot([-30, 45], [-30, 45], color="k", ls="--", lw=0.8)
ax.axhline(0, color="grey", lw=0.5); ax.axvline(0, color="grey", lw=0.5)
ax.set_xlabel("ANS at t (% of GNI)"); ax.set_ylabel("ANS at t + 5 (% of GNI)"); fig.tight_layout(); fig.savefig("fig/fig3_kernel.png"); plt.show()
R["kernel_note"] = "conditional density of ANS(t+5) given ANS(t), Gaussian KDE (Scott bandwidth), |ANS| < 60"
# ergodic by rents
fig, ax = plt.subplots(figsize=(6.3, 3.3))
ax.bar(x - 0.2, R["rents"]["ergodic_other"], 0.4, color="#74c476", label=f"Rents < 10% of GDP (n = {R['rents']['n_other']:,} transitions)")
ax.bar(x + 0.2, R["rents"]["ergodic_rich"], 0.4, color="#fd8d3c", label=f"Rents ≥ 10% of GDP (n = {R['rents']['n_rich']:,})")
ax.set_xticks(x); ax.set_xticklabels(LAB, fontsize=8); ax.set_xlabel("ANS class (% of GNI)"); ax.set_ylabel("Ergodic share"); ax.legend(frameon=False, fontsize=7)
fig.tight_layout(); fig.savefig("fig/fig4_rents.png"); plt.show()
json.dump(R, open("out/results.json", "w"), indent=1, default=str)
print(json.dumps({k: R[k] for k in ["n_countries", "n_obs", "obs_by_year", "desc", "pooled_class_counts", "annual", "five_year", "markov_check", "time_homog", "rents", "robustness", "profile_by_class", "corr_ans_dep", "corr_ans_rent", "n_persistent_negative", "persistent_negative", "tur", "sd_bal"]}, indent=0, default=str))

# ## 3. Kaynak rantına göre beş yıllık zincirler ve ilk geçiş süreleri

# Paper 5, supplementary computations: five-year chains by resource rents and mean first-passage times.
import json
R = json.load(open("out/results.json"))
def rich5(c, y):
    try: return RENT.loc[y, c] >= 10
    except Exception: return False
def poor5(c, y):
    try: return RENT.loc[y, c] < 10
    except Exception: return False
Nr5 = transitions(5, cond=rich5); Np5 = transitions(5, cond=poor5)
R["rents_5y"] = homogeneity([Nr5, Np5]) | {"n_rich": int(Nr5.sum()), "n_other": int(Np5.sum()), "ergodic_rich": np.round(ergodic(mat(Nr5)), 3).tolist(),
                                           "ergodic_other": np.round(ergodic(mat(Np5)), 3).tolist(), "P_rich_diag": np.round(np.diag(mat(Nr5)), 3).tolist(),
                                           "P_other_diag": np.round(np.diag(mat(Np5)), 3).tolist()}
def mfpt_to_set(P, target):
    """Mean number of steps to first reach any state in `target` from each other state."""
    S = [i for i in range(len(P)) if i not in target]; Q = P[np.ix_(S, S)]
    m = np.linalg.solve(np.eye(len(S)) - Q, np.ones(len(S)))
    return {LAB[s]: round(float(v), 2) for s, v in zip(S, m)}
P5 = mat(transitions(5)); P1 = mat(transitions(1))
R["mfpt_to_nonneg_5y_steps"] = mfpt_to_set(P5, {2, 3, 4, 5})
R["mfpt_to_nonneg_annual"] = mfpt_to_set(P1, {2, 3, 4, 5})
R["mfpt_to_nonneg_5y_rich"] = mfpt_to_set(mat(Nr5), {2, 3, 4, 5}); R["mfpt_to_nonneg_5y_other"] = mfpt_to_set(mat(Np5), {2, 3, 4, 5})
# within-group composition: which resource-rich countries sit in the top class?
top = [(names.get(c, c), round(float(ANS.loc[2010:2021, c].mean()), 1)) for c in ANS.columns if c in RENT.columns and RENT.loc[2010:2021, c].mean() >= 10 and ANS.loc[2010:2021, c].mean() >= 20]
bot = [(names.get(c, c), round(float(ANS.loc[2010:2021, c].mean()), 1)) for c in ANS.columns if c in RENT.columns and RENT.loc[2010:2021, c].mean() >= 10 and ANS.loc[2010:2021, c].mean() < -5]
R["rich_top_2010_21"] = sorted(top, key=lambda t: -t[1]); R["rich_bottom_2010_21"] = sorted(bot, key=lambda t: t[1])
json.dump(R, open("out/results.json", "w"), indent=1, default=str)
for k in ["rents_5y", "mfpt_to_nonneg_5y_steps", "mfpt_to_nonneg_annual", "mfpt_to_nonneg_5y_rich", "mfpt_to_nonneg_5y_other", "rich_top_2010_21", "rich_bottom_2010_21"]: print(k, R[k])

# ## Sonuçlar ve çıktılar

# Packs figures and numerical results for download.
import shutil
from google.colab import files
shutil.make_archive("/content/rp05_outputs", "zip", ".", ".")
files.download("/content/rp05_outputs.zip")
