Repository navigation
puremacro 5.2.0
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.tradewith 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.TradePolicySimulatorreturns 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.mdgives 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.pyrebuilds that table byte for byte from the OECD file;--downloadfetches the OECD's2016-2020_SML.zip(about 85 MB) once into~/.cache/puremacro/oecd_icio(or$PUREMACRO_OECD_ICIO_DIR).tools/reference_validation/validate_oecd.pytakes 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, andsynthetic_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 ofspatial_and_trade_ge,policy_simulators,STRUCTURAL_VALIDATION_STATUSandADVISORY. Notebooks 55 and 61-65 (EN/ES), the examplepuremacro/examples/distributional_trade_ge.py, the OECD/accounting/welfare/Hicksian validators intools/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,gpuincluded; consistent accounting is the only one, so the same call can give different numbers than in 4.x.accounting="legacy"raisesValueErrorsaying so; a removed keyword raisesTypeErrornaming it. - Named scenarios and the scenario registry (
SCENARIOS, the canonical-scenario constants and lookup functions,ExtendedTariffScenario,run_tariff_scenario).run_scenario,solve_keller_pacandpolicy_analyticstake aTariffScenarioor explicit tariff arrays; a string raisesTypeError("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")raisesValueErrornaming the bundled source andsynthetic_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 themEVDecompositionResult), the twoScenarioBatchResulttable renderers for the named scenarios,plot.plot_country_impactsandplot.plot_tariff_escalation_curve, the"harberger"welfare metric (it now gives the "Unknown welfare metric"ValueError), andflexible.compute_homothetic_final_demand. - In
mrioandcondensed: 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_gapsand thew_<code>alias. puremacro.trade.extensions(scenario extensions built on the named scenarios) andpuremacro.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_equilibriumandsolve_flexible_trade_equilibriumneed their country and sector named and raiseTypeErrornaming the missing argument;compute_unilateral_optimal_tariffandcompute_welfare_payoff_matrixtake their countries as required arguments, andsolve_multilateral_nash_tariffsandevaluate_national_welfareraiseTypeErrorwithout them; the Geary-Khamis numeraire is a country argument, andGearyKhamisResult's summary has no fixed-country PPP row.ICIODataneeds its country and sector codes. - Renamed without aliases: the fold helpers are
solve_fold_continuationandsolve_fold_continuation_step(they take the stiff coordinate as an argument; the info key isidx_stiff), andCondensedMeasuresResult.headline()iskey_measures(). solve_flexible_trade_equilibriumis rebuilt on the consistent accounting; at default settings its equations are those ofsolve_trade_equilibrium.method="quasi_condensed"onsolve_trade_equilibriumraisesNotImplementedErrorpointing 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_continuationhas no national-rate targets.get_country_coderaisesIndexErrorfor a position outside the table instead of inventing a code.solve_fold_continuation_stepraisesValueErrorfor anidx_stiffoutside[0, nc); it used to warn and use country 0.- The condensed path of
solve_flexible_trade_equilibriumkeepsiterationswithinmax_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 fullmax_itereach and could report more iterations thanmax_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,
iterationscounts 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 ofyear(fromtables, or the bundled 2019 table) and raisesValueErrorlisting the available years for any other year.
Fixed
CaliendoParroModelmapped the codes USA, CHN and DEU (and ROW) to positions 0-2 whatever the model's own country order, andmanuf/servto sectors 0-1. Countries and sectors are now found by their own codes (exact, then case-insensitive) or by position; an unknown code raisesKeyError.TradePolicySimulator.from_icio(year)ignoredyearand returned thenafta_chinapreset.condensed.table.infer_merchandise_maskcounted utilities (ISIC D and E) as merchandise, and accepted composite sector labels whose content is not documented; those now need an explicitmerchandise_mask.