Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions bedrock/analysis/nowcasting/services_transport_expense_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,18 +1168,43 @@ def _chain_across_aies() -> bool:
return get_usa_config().chain_services_seed_across_aies


#: The electricity commodity of the Use rows the seed moves.
ELECTRICITY_COMMODITY = '221100'

#: Industries whose electricity cell moves on another industry's survey index
#: under ``hold_services_seed_at_last_sas_year`` (#1304). Trucking's electricity
#: is its terminals and depots, buildings like warehousing's: its fleet isn't
#: electrified (Wes, 2026-11-13). Its own survey item steps x1.5 from AIES 2023
#: to 2024, beyond any SAS-era year. CBECS gives warehouses 13,107 kWh per worker
#: in 2018, against BEA's 2017 implied 15,283 for warehousing and 17,394 for
#: trucking.
ELECTRICITY_PROXY_INDUSTRIES = {'484000': '493000'}


def _hold_at_last_sas_year() -> bool:
from bedrock.utils.config.usa_config import get_usa_config # noqa: PLC0415

return get_usa_config().hold_services_seed_at_last_sas_year


def _chained_index(naics: str, year: int, base_year: int) -> pd.Series:
""":data:`LAST_SAS_YEAR`'s SAS index, moved on AIES's own ratios to *year*.

``index(2022 vs base)`` on the SAS panel, times ``index(year vs 2023)`` on
the AIES panel. A commodity only one side reaches keeps that side's index
(the other contributes 1.0, no movement).

⚠️ With ``hold_services_seed_at_last_sas_year``, every AIES year takes the SAS
index alone (#1304). From 2023 to 2024 the AIES moves purchased electricity
beyond any SAS-era year in 20 of 48 survey industries, and many industries'
other items grow 40-80% in that one year, so the relative index divides by a
bill that changes for reasons other than growth.
"""
sas = relative_index(
naics, LAST_SAS_YEAR, base_year=base_year, panel=_panel_for(LAST_SAS_YEAR)
)
first = AIES_OBSERVED_YEARS[0]
if year == first:
if year == first or _hold_at_last_sas_year():
return sas
aies = relative_index(naics, year, base_year=first, panel=_panel_for(year))
codes = sas.index.union(aies.index)
Expand Down Expand Up @@ -1245,12 +1270,20 @@ def services_transport_seed(
chained = year in AIES_OBSERVED_YEARS and _chain_across_aies()
panel = _panel_for(LAST_SAS_YEAR if chained else year)

def survey_index(naics: str) -> pd.Series:
if chained:
return _chained_index(naics, year, base_year)
return relative_index(naics, year, base_year=base_year, panel=panel)

for industry in columns:
naics = _bea_to_survey_industry()[industry]
if chained:
index = _chained_index(naics, year, base_year)
else:
index = relative_index(naics, year, base_year=base_year, panel=panel)
index = survey_index(naics)
proxy = ELECTRICITY_PROXY_INDUSTRIES.get(industry)
if proxy is not None and _hold_at_last_sas_year():
borrowed = survey_index(_bea_to_survey_industry()[proxy])
if ELECTRICITY_COMMODITY in borrowed.index:
index = index.copy()
index[ELECTRICITY_COMMODITY] = borrowed[ELECTRICITY_COMMODITY]
if index.empty:
continue
touched = [code for code in index.index if code in seed.index]
Expand Down
36 changes: 36 additions & 0 deletions bedrock/transform/iot/__tests__/test_nowcast_intermediate.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,42 @@ def test_services_seed_chains_across_the_survey_change(
assert float((indexed_2023 - chained_2022).abs().to_numpy().max()) > 0.0


@needs_census
def test_the_held_seed_takes_no_aies_movement_and_trucking_follows_warehousing(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""With the hold, 2024 is 2023, and trucking's electricity follows warehousing's.

#1304: AIES 2024 against AIES 2023 isn't used. Trucking's electricity cell
takes warehousing's electricity index, so in 2022 the two cells move by about
the same factor against their own 2017 shares.
"""
from bedrock.analysis.nowcasting import ( # noqa: PLC0415
services_transport_expense_seed as st,
)

monkeypatch.setattr(st, '_hold_at_last_sas_year', lambda: True)
pd.testing.assert_frame_equal(
st.services_transport_seed(2024), st.services_transport_seed(2023)
)

use = st._use_2017_detail()
held = st.services_transport_seed(2022)
elec = st.ELECTRICITY_COMMODITY

def share_change(seed: pd.DataFrame, industry: str) -> float:
column = pd.Series(seed[industry], dtype=float)
base = pd.Series(use[industry], dtype=float)
return (column[elec] / column.sum()) / (base[elec] / base.sum())

monkeypatch.setattr(st, '_hold_at_last_sas_year', lambda: False)
own = st.services_transport_seed(2022)
assert share_change(own, '484000') != pytest.approx(share_change(held, '484000'))
assert share_change(held, '484000') == pytest.approx(
share_change(held, '493000'), rel=0.05
)


# --- electric power's refined petroleum on price (#922) ------------------------


Expand Down
6 changes: 6 additions & 0 deletions bedrock/utils/config/usa_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ class USAConfig(BaseModel):
# cut every buyer ~16% to close it (trade -24%). See
# bedrock.analysis.nowcasting.services_transport_expense_seed.
chain_services_seed_across_aies: bool = True # DRI: WesIngwersen
# With the chain on, hold every AIES year on 2022's SAS relative index
# instead of moving 2024 on AIES 2024/2023. That step isn't safe inside
# the AIES either: purchased electricity moves beyond any SAS-era year in
# 20 of 48 survey industries (warehousing x3.7, legal x0.53), and other
# items grow 40-80% in one year in many (#1304).
hold_services_seed_at_last_sas_year: bool = False # DRI: WesIngwersen
# Chain the truck and water transport margins across the SAS -> AIES
# change: 2023 keeps 2022's margin as a share of the mode's gross output,
# and 2024 moves on AIES's own 2024/2023 revenue. AIES's Table 8 covers
Expand Down
Loading