us-treasury-events / tests /test_pit.py
ZipLime's picture
Update the FOMC events dataset
8863726 verified
Raw History Blame
4.89 kB
"""The point-in-time table, and the two ways its rows are dated."""
from __future__ import annotations
from datetime import UTC, date, datetime
import polars as pl
from recipe.pit import build_pit, entity_for, term_code
from recipe.schema import BILL_RATE_SCHEMA, CURVE_SCHEMA, RESULT_SCHEMA, align
AUCTION_AT = datetime(2026, 9, 10, 17, 2, tzinfo=UTC) # 13:02 Eastern, stated
CURVE_AT = datetime(2026, 9, 10, 22, 0, tzinfo=UTC) # 18:00 Eastern, modelled
def test_auction_series_are_keyed_by_term_not_by_cusip():
"""A series keyed on one CUSIP stops when that bond is issued."""
assert term_code("10-Year", "note") == "10Y"
assert term_code("10-Year", "tips") == "10Y_TIPS"
assert term_code("4-Week", "bill") == "4W"
assert term_code("42-Day", "cmb") == "42D_CMB"
assert term_code(None, "bill") is None
def test_a_prefix_is_not_repeated():
assert entity_for("UST_CMT_", "10Y") == "UST_CMT_10Y"
assert entity_for("UST_LT_", "LT_COMPOSITE") == "UST_LT_COMPOSITE"
assert entity_for("UST_REAL_LT_", "LT_REAL_AVERAGE") == "UST_REAL_LT_REAL_AVERAGE"
def _frames() -> dict[str, pl.DataFrame]:
result = {
"result_id": "UST_2026-09-10_912810UW6_RES",
"auction_id": "UST_2026-09-10_912810UW6",
"cusip": "912810UW6",
"security_type": "bond",
"security_term": "29-Year 11-Month",
"auction_date": date(2026, 9, 10),
"knowledge_at": AUCTION_AT,
"knowledge_date": AUCTION_AT,
"knowledge_time_precision": "minute",
"high_yield": 5.308,
"bid_to_cover": 2.61,
"indirect_accepted_pct": 79.330688,
"source_url": "https://example.invalid/artifact",
"parser_version": "1.0.0",
}
curve = [
{
"curve_id": f"nominal_2026-09-{day:02d}_10Y",
"curve_date": date(2026, 9, day),
"curve_type": "nominal",
"maturity": "10Y",
"maturity_years": 10.0,
"yield_pct": value,
"knowledge_at": datetime(2026, 9, day, 22, 0, tzinfo=UTC),
"knowledge_date": datetime(2026, 9, day, 22, 0, tzinfo=UTC),
"knowledge_time_precision": "modeled_conservative",
"source_url": "https://example.invalid/rates",
"parser_version": "1.0.0",
}
for day, value in ((9, 4.93), (10, 4.95))
]
bills = {
"bill_rate_id": "bill_2026-09-10_13W",
"rate_date": date(2026, 9, 10),
"maturity": "13W",
"maturity_weeks": 13.0,
"bank_discount_rate": 3.93,
"coupon_equivalent_rate": 4.03,
"knowledge_at": CURVE_AT,
"knowledge_date": CURVE_AT,
"knowledge_time_precision": "modeled_conservative",
"source_url": "https://example.invalid/rates",
"parser_version": "1.0.0",
}
return {
"auction_results": align(pl.DataFrame([result], infer_schema_length=None), RESULT_SCHEMA),
"nominal_yield_curve": align(pl.DataFrame(curve, infer_schema_length=None), CURVE_SCHEMA),
"real_yield_curve": pl.DataFrame(schema=dict(CURVE_SCHEMA)),
"bill_rates": align(pl.DataFrame([bills], infer_schema_length=None), BILL_RATE_SCHEMA),
}
def test_an_auction_and_a_curve_from_the_same_day_are_hours_apart():
"""The whole reason both belong in one table."""
pit = build_pit(_frames())
auction = pit.filter(pl.col("subset") == "auction_results")["knowledge_date"].max()
curve = pit.filter(pl.col("subset") == "nominal_yield_curve")["knowledge_date"].max()
assert auction < curve
assert (curve - auction).total_seconds() >= 4 * 3600
def test_a_bill_becomes_two_series_because_it_is_quoted_two_ways():
pit = build_pit(_frames())
bills = set(pit.filter(pl.col("subset") == "bill_rates")["entity_id"])
assert bills == {"UST_BILL_13W_BD", "UST_BILL_13W_CE"}
def test_derived_shares_are_labelled_derived():
pit = build_pit(_frames())
methods = dict(zip(pit["entity_id"], pit["value_method"], strict=True))
assert methods["UST_AUCTION_29Y_11M_HIGH_YIELD"] == "reported"
assert methods["UST_AUCTION_29Y_11M_INDIRECT_PCT"] == "derived"
def test_change_is_against_the_previous_publication_of_the_same_series():
pit = build_pit(_frames()).filter(pl.col("entity_id") == "UST_CMT_10Y").sort("knowledge_date")
assert pit["actual"].to_list() == [4.93, 4.95]
assert pit["previous"].to_list() == [None, 4.93]
assert pit["change"].to_list()[1] == 0.02
def test_forecast_is_always_null():
"""Consensus estimates are licensed data; this dataset carries none."""
pit = build_pit(_frames())
assert pit["forecast"].null_count() == pit.height
def test_nothing_is_knowable_before_the_day_it_describes():
pit = build_pit(_frames())
early = pit.filter(pl.col("knowledge_date").dt.date() < pl.col("event_date"))
assert early.height == 0