Skip to content

feat(trade): extend Trade FBS/overlay to 2018–2024; export 1:m uses Detail_Supply T007 - #730

Merged
WesIngwersen merged 4 commits into
nowcastfrom
by_trade_time_series
Aug 26, 2026
Merged

feat(trade): extend Trade FBS/overlay to 2018–2024; export 1:m uses Detail_Supply T007#730
WesIngwersen merged 4 commits into
nowcastfrom
by_trade_time_series

Conversation

@bl-young

@bl-young bl-young commented Aug 26, 2026

Copy link
Copy Markdown
Member

cc:
Closes: #727
Related: #729

What changed? Why?

Nowcast years 2018-2024 use the same Trade FBS + overlay path as 2017. Extract YAMLs already declare Census_USATrade / BEA_IEA through 2024; methods, MDTY/MADJ helpers, and nowcast gates were 2017-only.

Export 1:m Crosswalk splits use same-year domestic commodity output T007: each Trade_Exports_<year> caches Detail_Supply_<year> and runs trade.attribution.collapse_detail_supply_to_t007 before proportional attribution (#729). Imports stay on frozen 2017 Supply MCIF.

Methods

  • Trade_Exports_common.yaml / Trade_Imports_common.yaml hold shared bodies; thin Trade_{Exports,Imports}_{2017..2024}.yaml stubs set year.
  • Export stubs cache Detail_Supply_<year> with clean_fbs: collapse_detail_supply_to_t007 and use that cache key as attribution_source.
  • Census and IEA sources inherit the method year. Exports and imports remain separate methods (different FlowName, IEA Crosswalk, attribution column).

Helpers + nowcast wire

  • mdty_detail_usd / madj_detail_usd work for every overlay year (no year != 2017 guard).
  • MADJ level: 2017 ->’ published Supply MADJ; later years ->’ that year's mapped GEN_CHA_YR magnitude with the published Supply MADJ sign. Destination shares stay frozen 2017 Supply MADJ.
  • TRADE_OVERLAY_YEARS = range(2017, 2025) in eeio/nowcast.py for F04000 / MCIF / MDTY / MADJ. Inventories stay 2017-only.
  • S00900 / F04000 identity keeps frozen 2017 Supply T016[S00900] for every trade-overlay year.
  • Overlay stays unscaled mapped Trade mass (Step 1B: revisit wholesale ITA G+S scale vs raw Census + BEA IEA #647 Option B).

bedrock/transform/trade/README.md documents the multi-year layout and attribution rule. score_2017_trade_detail_baseline.csv is advanced for T007 export-attribution leaf moves (49 commodities; no Crosswalk change).

GCS extract inputs (staged; not in git)

Raw Census / IEA dumps for 2018-2024 are on gs://cornerstone-default/extract/input-data/:

  • Census_USATrade/{2018..2024}/Census_USATrade_<year>_{exports,imports}.csv
  • BEA_IEA/{2018..2024}/BEA_IEA_<year>_{Exports,Imports}.csv

Detail_Supply_{2018..2024} FBS parquets live under gs://cornerstone-default/transform/output_data/ (not flowsa/FlowBySector/ -> download locally before generate). With extract_data_from_raw_sources: False, FBA generation loads Census/IEA via gcs_fxn without API keys.

Outstanding: Census NAICS 2022+ -> Detail Crosswalk

Sector_Crosswalk_Census_USATrade is still a 2017-vintage Activity list. Smoke vs Census_USATrade FBAs:

Year Export mass join Import mass join Notes
2017-2021 ~100% / ~97% ~100% / ~97% Import gap almost entirely intentional 980000
2022 ~99.7% ~97% Small dig-6 orphans + residual rename 11211X->1121XX; OK to ship
2023-2024 ~92% ~82% NAICS-2022 leaves (e.g. 336110) drop; FBS Detail row count 305->290

This PR does not extend the Crosswalk. Follow-on under #670 / goods-allocation: inventory orphan Activities by year -> concordance to BEA 2017 Detail -> append Crosswalk rows (keep 980000 omitted) -> regen Trade_*_{2022..2024}. Cheap first cut: alias 1121XX to the same Detail targets as 11211X (~0.5 B exports).

Out of scope

Testing

Regenerated Trade_Exports/Imports_{2017..2024} locally (Census/IEA FBAs local; Detail_Supply_{2018..2024} pulled from GCS transform/output_data/).

$env:Path = "$env:USERPROFILE\.local\bin;$env:Path"
foreach ($y in 2017..2024) {
  uv run python -c "from bedrock.transform.flowbysector import FlowBySector as F; e=F.generateFlowBySector(f'Trade_Exports_$y', download_sources_ok=True); i=F.generateFlowBySector(f'Trade_Imports_$y', download_sources_ok=True); print($y, e['FlowAmount'].sum()/1e6, i['FlowAmount'].sum()/1e6)"
}
uv run python -c "from bedrock.transform.eeio.nowcast import derive_initial_Y_pur, derive_initial_supply_bridge; y=derive_initial_Y_pur(2022); b=derive_initial_supply_bridge(2022); print(y['F04000'].sum()/1e6, b['MCIF'].sum()/1e6, b['MDTY'].sum()/1e6, b['MADJ'].sum()/1e6)"
uv run python -m bedrock.analysis.nowcasting.trade_data.score_2017_trade_detail

Results (local):

⚠️ Vintage note. These totals were generated before Step 4a rewired
Detail_Supply and commodity output for 2022–2024. Export 1:m splits use same-year
T007 as proportional attribution weights, which redistribute within a 1:m group
and preserve its total — so the totals below still hold, but the per-commodity
composition for 2022–2024 exports moves once Trade_Exports_{2022..2024} is
regenerated against the updated commodity output. Imports are unaffected: they stay
on frozen 2017 Supply MCIF.

Year Exports M Imports M
2017 2,132,637 2,679,578
2018 2,271,469 2,870,643
2019 2,278,811 2,835,013
2020 2,037,942 2,665,519
2021 2,443,060 3,209,061
2022 2,810,816 3,723,030
2023 2,643,809 3,092,093
2024 2,763,107 3,309,118

2017 scorecard (after T007; baseline updated): F040 national +12.20%, MCIF +1.14%; Pearson non-special 0.92 / 0.85; no regressions vs prior baseline (49 leaf moves). Re-run without --update-baseline should report no changes.

@bl-young bl-young linked an issue Aug 26, 2026 that may be closed by this pull request
@bl-young bl-young changed the title Step 1B: extend Trade FBS and overlay to 2018–2024 feat(trade): extend Trade FBS/overlay to 2018–2024; export 1:m uses Detail_Supply T007 Aug 26, 2026
@bl-young
bl-young marked this pull request as ready for review August 26, 2026 21:21
@WesIngwersen
WesIngwersen self-requested a review August 26, 2026 21:25
Base automatically changed from trade_updates to nowcast August 26, 2026 23:12
Add common + per-year Trade_Exports/Imports methods, lift MDTY/MADJ
year guards, and wire F040/MCIF/MDTY/MADJ for TRADE_OVERLAY_YEARS.
Attribution stays on frozen 2017 F040/MCIF.
Cache Detail_Supply_<year> in Trade_Exports stubs and collapse to commodity
T007 before proportional attribution (#729). Imports stay on frozen 2017 MCIF.
@WesIngwersen
WesIngwersen merged commit 1b97307 into nowcast Aug 26, 2026
4 checks passed
@WesIngwersen
WesIngwersen deleted the by_trade_time_series branch August 26, 2026 23:23
WesIngwersen added a commit that referenced this pull request Aug 27, 2026
Docs only. The plan and progress report predated #733/#734 and were describing
a Supply bridge that no longer exists.

Supply column table, corrected against a live coverage run rather than from
memory: eight of the twelve columns now run 2017-2024, and only TRADE, TRANS
and therefore T014/T016 stop at 2023. MCIF, MADJ and MDTY were still listed as
"2017 candidate" and have been annual since #730. TRANS was "2017-2022" and now
reaches 2023 from AIES.

TOP's residual is no longer frozen, which the plan asserted in two places. The
purchaser-price base did not have to wait for Step 5 as the plan predicted - it
is assembled from the bridge's own components, and is not circular because TOP
sits in T015 and is excluded from its own base. Recorded with what it buys
(S00402's 174,312 base, the 0.939-1.008 rescale) and what it costs: TOP now
depends on margin coverage, so 2024 holds 2023's shares.

Step 1: F04000 was described as all-zero for 2018-2024 and has been annual since
#730 (2,211bn to 3,139bn). F03000 really is still 2017-only, so the two are now
stated separately instead of together.

Numbers re-measured rather than edited by hand. Steps 1 and 4 re-run, and the
embedded images regenerated with them: supply bridge coverage 99.0% to 99.2%,
accuracy 61.8% to 62.1%, grand total error 0.092% to 0.048%. That improvement is
not Step 4a - it is the Trade FBSs rebuilt on the NAICS-2022 goods Crosswalk,
which moved MCIF (misses 27 to 22) and carried through T013 and T016. Per-column
table updated with it. Step 3 is unchanged and was not re-run.

⚠️ The 2017 trade scorecard moved in two directions at once and the README now
says so rather than reporting the half that flatters it: the export national
error roughly halved, +12.20% to +6.16%, while Pearson fell 0.96 to 0.92,
top-20 Jaccard 0.67 to 0.60 and n_miss rose 39 to 43. The extra mass lands
closer to the published total but spread over more Detail codes. The prior row
is kept for comparison.

⚠️ The stored per-commodity trade baseline was deliberately NOT refreshed.
score_2017_trade_detail flags a handful of expected status changes against it;
--update-baseline is a judgement call and has not been made.

Step 2's plan entries are not touched here - they belong to #735.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Step 1B: extend Trade FBS and overlay columns to 2018–2024

2 participants