Skip to content

puremacro 5.2.0

Choose a tag to compare

@jalonso1979 jalonso1979 released this 17 Oct 01:16
· 419 commits to main since this release

puremacro.trade is back. 5.0 and 5.1 shipped without it; this release brings the multi-country, multi-sector trade library back with consistent producer/purchaser accounting as its only accounting, a new bundled OECD table built by a script that ships with it, and no built-in defaults for the table: every dimension, country and sector comes from the table or from an argument. Code written for the 4.x trade API needs the changes listed under "Removed relative to 4.8.1" and "Changed relative to 4.8.1".

Added

  • puremacro.trade with the static model (calibrate_trade_model, solve_trade_equilibrium, solve_equilibrium, TariffScenario, run_scenario, run_scenario_batch, Geary-Khamis PPP), Hicksian welfare (compute_hicksian_welfare), audited policy recovery (solve_policy_equilibrium), optimal tariffs and Nash tariff games (optimal_tariffs, game), Caliendo-Parro (2015) exact hat algebra (CaliendoParroModel), the flexible CGE (solve_flexible_trade_equilibrium: nested CES technology, Stone-Geary demand, Armington sourcing, Atkeson-Burstein markups, capacity margins), policy analytics, the exact nested-CES block Newton (ces_newton), household demand systems and distributional incidence (household, distributional), audited parameter continuation (continuation), the reduced local-stability diagnostic (stability, experimental), native MRIO tables (mrio), the condensed one-factor Leontief tariff model (condensed), the perfect-foresight dynamic MRIO (dynamic, experimental), plots and the optional torch/MLX solvers (gpu). models.TradePolicySimulator returns with it.
  • A bundled table, puremacro/trade/_datafiles/icio_oecd2023_2019_isic_g20.npz: the OECD Inter-Country Input-Output tables, 2023 edition, reference year 2019, summed into the 19 G20 member countries plus a rest of the world and the 20 sections of ISIC Rev.4 (shape 403 x 460). load_icio_data() reads it by default (source="oecd_isic") and labels it with the table's own codes. puremacro/trade/_datafiles/SOURCES.md gives the OECD citation, the terms of use, the concordance and the checksums of the source and of the bundled file. If you publish results computed from it, cite the OECD.
  • tools/build_icio_isic.py rebuilds that table byte for byte from the OECD file; --download fetches the OECD's 2016-2020_SML.zip (about 85 MB) once into ~/.cache/puremacro/oecd_icio (or $PUREMACRO_OECD_ICIO_DIR). tools/reference_validation/validate_oecd.py takes the same option.
  • puremacro.trade.data.synthetic_icio_table(nc, ns, *, nfd=3, seed=0) builds a seeded synthetic table of any size in the library's layout, with nonnegative final-use shares and positive investment for every seed, and synthetic_codes(nc, ns) gives its labels (C01..., S01...). The tests run on it instead of a real table.
  • Docs (EN/ES): trade_accounting, trade_welfare, trade_policy, trade_continuation, trade_stability, trade_household, trade_distributional, trade_ces_newton, trade_condensed, trade_mrio, trade_dynamic, trade_gpu; the trade sections of spatial_and_trade_ge, policy_simulators, STRUCTURAL_VALIDATION_STATUS and ADVISORY. Notebooks 55 and 61-65 (EN/ES), the example puremacro/examples/distributional_trade_ge.py, the OECD/accounting/welfare/Hicksian validators in tools/reference_validation/ and three trade cases in the validation gallery.

Removed relative to 4.8.1

  • The legacy accounting. accounting="legacy" (the 4.x default), tariff_revenue_mode="legacy_national" and the precedence and compatibility switches that went with it are gone from every entry point, gpu included; consistent accounting is the only one, so the same call can give different numbers than in 4.x. accounting="legacy" raises ValueError saying so; a removed keyword raises TypeError naming it.
  • Named scenarios and the scenario registry (SCENARIOS, the canonical-scenario constants and lookup functions, ExtendedTariffScenario, run_tariff_scenario). run_scenario, solve_keller_pac and policy_analytics take a TariffScenario or explicit tariff arrays; a string raises TypeError("named scenarios were removed; build a TariffScenario").
  • The 4.x bundled table, the bundled reference solutions and results workbook, their loaders and constants (the reference-solution and workbook loaders, available_reference_scenarios, bundled_icio_path, bundled_reference_path, bundled_workbook_path, load_raw_45sector_icio, BUNDLED_ICIO_PROVENANCE, OECD2020_ICIO_PROVENANCE), and that table's code lists (CANONICAL_COUNTRY_CODES, CANONICAL_SECTOR_CODES, CANONICAL_SECTOR_NAMES, get_country_codes, get_sector_codes, get_sector_names). load_icio_data(source="legacy") raises ValueError naming the bundled source and synthetic_icio_table.
  • The real-world tariff benchmark (and its re-export in game), the analysis stubs and result classes built on the named scenarios (among them EVDecompositionResult), the two ScenarioBatchResult table renderers for the named scenarios, plot.plot_country_impacts and plot.plot_tariff_escalation_curve, the "harberger" welfare metric (it now gives the "Unknown welfare metric" ValueError), and flexible.compute_homothetic_final_demand.
  • In mrio and condensed: the coarse tariff rules (coarse_tariff_rates, CoarseTariffResult), the sector concordance of the 4.x table and its code and name constants, the numbered model labels, condensed.measures.aggregation_gaps and the w_<code> alias.
  • puremacro.trade.extensions (scenario extensions built on the named scenarios) and puremacro.trade.tables (LaTeX tables of the named scenarios) do not return.

Changed relative to 4.8.1

  • No paper defaults. Dimensions are read from the table (any number of countries, sectors and final-use categories); the fiscal-recycling and capacity branches of solve_trade_equilibrium and solve_flexible_trade_equilibrium need their country and sector named and raise TypeError naming the missing argument; compute_unilateral_optimal_tariff and compute_welfare_payoff_matrix take their countries as required arguments, and solve_multilateral_nash_tariffs and evaluate_national_welfare raise TypeError without them; the Geary-Khamis numeraire is a country argument, and GearyKhamisResult's summary has no fixed-country PPP row. ICIOData needs its country and sector codes.
  • Renamed without aliases: the fold helpers are solve_fold_continuation and solve_fold_continuation_step (they take the stiff coordinate as an argument; the info key is idx_stiff), and CondensedMeasuresResult.headline() is key_measures().
  • solve_flexible_trade_equilibrium is rebuilt on the consistent accounting; at default settings its equations are those of solve_trade_equilibrium. method="quasi_condensed" on solve_trade_equilibrium raises NotImplementedError pointing to the flexible solver.
  • gpu: BatchedJacobianEvaluator(calib, tau_a, taufd_a, ...) evaluates the consistent-accounting residuals and no longer takes the national tariff-rate vectors; solve_homotopy_continuation has no national-rate targets.
  • get_country_code raises IndexError for a position outside the table instead of inventing a code.
  • solve_fold_continuation_step raises ValueError for an idx_stiff outside [0, nc); it used to warn and use country 0.
  • The condensed path of solve_flexible_trade_equilibrium keeps iterations within max_iter: Newton stops when its line search finds no decrease (metadata["newton_stalled"]). Before, a solve that could not converge ran Newton up to 21 times with the full max_iter each and could report more iterations than max_iter.
  • In that condensed path, the tariff homotopy runs only on the iteration budget the direct attempt leaves unused; with fewer iterations left than homotopy steps it is not tried.
  • When that condensed solve fails, iterations counts the homotopy stages as well as the direct attempt (metadata["tariff_homotopy_steps"] is 0 when a homotopy was tried and did not converge).
  • TradePolicySimulator.from_icio(year, *, tables=None, ...) calibrates to the table of year (from tables, or the bundled 2019 table) and raises ValueError listing the available years for any other year.

Fixed

  • CaliendoParroModel mapped the codes USA, CHN and DEU (and ROW) to positions 0-2 whatever the model's own country order, and manuf/serv to sectors 0-1. Countries and sectors are now found by their own codes (exact, then case-insensitive) or by position; an unknown code raises KeyError.
  • TradePolicySimulator.from_icio(year) ignored year and returned the nafta_china preset.
  • condensed.table.infer_merchandise_mask counted utilities (ISIC D and E) as merchandise, and accepted composite sector labels whose content is not documented; those now need an explicit merchandise_mask.