"""Reading the daily rate feeds, and dating them honestly.""" from __future__ import annotations from datetime import UTC, date, datetime import pytest from recipe.methodology import methodology_for, registry from recipe.sources.curves import ( normalise_maturity, parse_bill_rates, parse_curve, parse_date, to_rate, ) from recipe.timing import CURVE_TIME_BASIS, curve_instants @pytest.mark.parametrize( ("label", "expected"), [ ("1 Mo", ("1M", 0.083333)), ("1.5 Month", ("1.5M", 0.125)), ("10 Yr", ("10Y", 10.0)), ("5 YR", ("5Y", 5.0)), ("4 WEEKS BANK DISCOUNT", ("4W", 0.076712)), # Not a point on a curve, and filing it as 10Y would put a second, # different number under a name already taken. ("LT COMPOSITE (>10 Yrs)", ("LT_COMPOSITE", None)), ("TREASURY 20-Yr CMT", ("20Y", 20.0)), ], ) def test_maturity_labels(label, expected): assert normalise_maturity(label) == expected def test_a_blank_cell_is_a_maturity_that_did_not_exist(): assert to_rate("") is None assert to_rate("N/A") is None assert to_rate(" ") is None # A zero yield is a real and different statement. assert to_rate("0.00") == 0.0 def test_dates_in_the_feed_format(): assert parse_date("09/11/2026") == date(2026, 9, 11) assert parse_date("nonsense") is None def test_the_current_curve_has_the_maturities_the_treasury_publishes_now(load_fixture): points = parse_curve(load_fixture("rates/nominal_2026.csv.gz")) maturities = {point.maturity for point in points} assert {"1M", "3M", "6M", "1Y", "2Y", "5Y", "10Y", "20Y", "30Y"} <= maturities assert all(point.yield_pct is not None for point in points) def test_a_year_with_fewer_maturities_yields_fewer_rows_not_nulls(load_fixture): """The 4-month bill was added in 2022; in 2010 there was no column for it.""" old = parse_curve(load_fixture("rates/nominal_2010_fewer_maturities.csv.gz")) new = parse_curve(load_fixture("rates/nominal_2026.csv.gz")) assert "4M" not in {point.maturity for point in old} assert "4M" in {point.maturity for point in new} # Nothing was interpolated to fill the gap. assert all(point.yield_pct is not None for point in old) def test_the_real_curve_is_read_separately(load_fixture): points = parse_curve(load_fixture("rates/real_2026.csv.gz")) assert {point.maturity for point in points} == {"5Y", "7Y", "10Y", "20Y", "30Y"} def test_a_bill_carries_both_of_its_quote_conventions(load_fixture): """Discount rate and coupon equivalent are the same bill measured differently.""" points = parse_bill_rates(load_fixture("rates/bills_2026.csv.gz")) assert points both = [ point for point in points if point.bank_discount_rate is not None and point.coupon_equivalent_rate is not None ] assert len(both) > len(points) * 0.9 # The coupon equivalent is always the larger of the two. assert all(point.coupon_equivalent_rate >= point.bank_discount_rate for point in both) def test_a_curve_is_knowable_at_six_and_describes_half_past_three(): observed, known, precision = curve_instants(date(2026, 9, 11)) assert precision == "modeled_conservative" # 15:30 and 18:00 Eastern in September are 19:30 and 22:00 UTC. assert observed == datetime(2026, 9, 11, 19, 30, tzinfo=UTC) assert known == datetime(2026, 9, 11, 22, 0, tzinfo=UTC) assert known > observed assert CURVE_TIME_BASIS == "treasury_usually_available_by_1800_et" def test_daylight_saving_comes_from_the_timezone_database(): _, summer, _ = curve_instants(date(2026, 7, 1)) _, winter, _ = curve_instants(date(2026, 1, 2)) assert summer.hour == 22 assert winter.hour == 23 def test_the_methodology_changed_on_the_date_the_treasury_states(): assert methodology_for(date(2021, 12, 3)) == "quasi_cubic_hermite_spline" assert methodology_for(date(2021, 12, 6)) == "monotone_convex" assert methodology_for(date(1985, 1, 1)) is None table = registry() assert table.height == 2 assert table["effective_to"].to_list()[-1] is None assert all(len(text) > 100 for text in table["description"]) def test_every_past_year_is_settled_including_the_empty_ones(tmp_path): """A past year with no rows is a year the Treasury published nothing for. The real curve begins in 2003 and the years before it return a file with nothing but a header. Holding only the years that produced rows meant refetching those empty years on every run, forever, to be told again that they are empty. The current year is never held: it gains a row a day. """ from datetime import UTC, datetime import polars as pl from recipe.pipeline import held_curve_years from recipe.schema import CURVE_SCHEMA, align from recipe.store import write_table this_year = datetime.now(UTC).year rows = [ {"curve_id": f"nominal_{year}-06-01_10Y", "curve_date": date(year, 6, 1), "curve_type": "nominal", "maturity": "10Y", "yield_pct": 4.0} for year in (this_year - 2, this_year) ] data_dir = tmp_path / "data" write_table(data_dir, "nominal_yield_curve", align(pl.DataFrame(rows, infer_schema_length=None), CURVE_SCHEMA)) held = held_curve_years(data_dir, first_year=this_year - 5) assert held["nominal"] == set(range(this_year - 5, this_year)) assert this_year not in held["nominal"] def test_two_feeds_sharing_a_table_are_tracked_apart(tmp_path): """`nominal` and `long_term_composite` both write nominal_yield_curve.""" import polars as pl from recipe.pipeline import held_curve_years from recipe.schema import CURVE_SCHEMA, align from recipe.store import write_table rows = [ {"curve_id": "nominal_2010-06-01_10Y", "curve_date": date(2010, 6, 1), "curve_type": "nominal", "maturity": "10Y", "yield_pct": 3.2}, {"curve_id": "lt_2011-06-01_LT_COMPOSITE", "curve_date": date(2011, 6, 1), "curve_type": "long_term_composite", "maturity": "LT_COMPOSITE", "yield_pct": 4.1}, ] data_dir = tmp_path / "data" write_table(data_dir, "nominal_yield_curve", align(pl.DataFrame(rows, infer_schema_length=None), CURVE_SCHEMA)) held = held_curve_years(data_dir, first_year=2010) # Both feeds have published rows, so both hold every past year. assert 2010 in held["nominal"] assert 2011 in held["long_term_composite"] # A feed with no rows at all holds nothing. assert "real" not in held def test_nothing_is_held_without_a_published_tree(tmp_path): from recipe.pipeline import held_curve_years assert held_curve_years(tmp_path / "nothing") == {}