Download tests/test_pit.py from ZipLime/fomc-events: direct link, hf CLI and curl.
- Browser
- Download file 4.54 kB
-
https://huggingface.co/datasets/ZipLime/fomc-events/resolve/main/tests/test_pit.py
- Command line
-
hf download hf://datasets/ZipLime/fomc-events/tests/test_pit.py
-
curl -L -o test_pit.py https://huggingface.co/datasets/ZipLime/fomc-events/resolve/main/tests/test_pit.py
4.54 kB
| """The point-in-time table, and the arithmetic it is allowed to do.""" | |
| from __future__ import annotations | |
| from datetime import UTC, date, datetime | |
| import polars as pl | |
| from recipe.pipeline import with_rate_changes | |
| from recipe.pit import build_pit, horizon_label, weighted_median | |
| from recipe.schema import EVENT_SCHEMA, align | |
| def test_horizon_is_relative_to_the_meeting_that_projected_it(): | |
| assert horizon_label("2026", 2026) == "Y0" | |
| assert horizon_label("2028", 2026) == "Y2" | |
| assert horizon_label("longer_run", 2026) == "LR" | |
| # A projection for a year already past is not a horizon; it is a misread | |
| # column, and it is dropped rather than filed under a negative offset. | |
| assert horizon_label("2019", 2026) is None | |
| def test_the_median_dot_counts_each_participant_once(): | |
| assert weighted_median([3.0, 3.5, 4.0], [1, 1, 1]) == 3.5 | |
| assert weighted_median([3.0, 4.0], [2, 2]) == 3.5 | |
| assert weighted_median([3.0, 3.5, 4.0], [5, 1, 1]) == 3.0 | |
| assert weighted_median([], []) is None | |
| def _events() -> pl.DataFrame: | |
| rows = [ | |
| { | |
| "event_id": "US_FOMC_2026-01-28_STATEMENT", "meeting_id": "US_FOMC_2026-01-28", | |
| "event_type": "STATEMENT", | |
| "knowledge_at": datetime(2026, 1, 28, 19, 0, tzinfo=UTC), | |
| "target_rate_lower": 3.75, "target_rate_upper": 4.0, "target_rate_mid": 3.875, | |
| "decision": "hold", "vote_for": 12, "vote_against": 0, | |
| "knowledge_time_precision": "exact", "source_url": "https://example.invalid/a", | |
| "parser_version": "1.0.0", | |
| }, | |
| { | |
| "event_id": "US_FOMC_2026-03-18_STATEMENT", "meeting_id": "US_FOMC_2026-03-18", | |
| "event_type": "STATEMENT", | |
| "knowledge_at": datetime(2026, 3, 18, 18, 0, tzinfo=UTC), | |
| "target_rate_lower": 3.5, "target_rate_upper": 3.75, "target_rate_mid": 3.625, | |
| "decision": "cut", "vote_for": 11, "vote_against": 1, | |
| "knowledge_time_precision": "exact", "source_url": "https://example.invalid/b", | |
| "parser_version": "1.0.0", | |
| }, | |
| ] | |
| return align(pl.DataFrame(rows, infer_schema_length=None), EVENT_SCHEMA) | |
| def _meetings() -> pl.DataFrame: | |
| return pl.DataFrame( | |
| { | |
| "meeting_id": ["US_FOMC_2026-01-28", "US_FOMC_2026-03-18"], | |
| "meeting_end_date": [date(2026, 1, 28), date(2026, 3, 18)], | |
| } | |
| ) | |
| def test_rate_change_is_measured_against_the_previous_statement(): | |
| events = with_rate_changes(_events()) | |
| changes = dict(zip(events["event_id"], events["rate_change_bp"], strict=True)) | |
| assert changes["US_FOMC_2026-01-28_STATEMENT"] is None | |
| assert changes["US_FOMC_2026-03-18_STATEMENT"] == -25.0 | |
| def test_pit_rows_carry_the_instant_the_event_did(): | |
| frames = { | |
| "events": _events(), | |
| "meetings": _meetings(), | |
| "sep": pl.DataFrame(schema={"meeting_id": pl.String, "median": pl.Float64}), | |
| "dot_plot": pl.DataFrame( | |
| schema={ | |
| "meeting_id": pl.String, "projection_year": pl.String, | |
| "rate_level": pl.Float64, "dot_count": pl.Int32, | |
| "knowledge_at": pl.Datetime(time_unit="us", time_zone="UTC"), | |
| "source_url": pl.String, | |
| } | |
| ), | |
| } | |
| pit = build_pit(frames) | |
| assert set(pit["entity_id"].unique()) == { | |
| "US_FOMC_TARGET_RATE_LOWER", "US_FOMC_TARGET_RATE_UPPER", "US_FOMC_TARGET_RATE_MID", | |
| } | |
| mid = pit.filter(pl.col("entity_id") == "US_FOMC_TARGET_RATE_MID").sort("knowledge_date") | |
| assert mid["actual"].to_list() == [3.875, 3.625] | |
| assert mid["previous"].to_list() == [None, 3.875] | |
| assert mid["change"].to_list() == [None, -0.25] | |
| # Nothing is ever knowable before it was published. | |
| assert (mid["knowledge_date"] >= mid["event_date"].cast(pl.Datetime("us", "UTC"))).all() | |
| def test_forecast_is_always_null(): | |
| """Consensus estimates are licensed data; this dataset carries none.""" | |
| frames = { | |
| "events": _events(), "meetings": _meetings(), | |
| "sep": pl.DataFrame(schema={"meeting_id": pl.String, "median": pl.Float64}), | |
| "dot_plot": pl.DataFrame( | |
| schema={ | |
| "meeting_id": pl.String, "projection_year": pl.String, | |
| "rate_level": pl.Float64, "dot_count": pl.Int32, | |
| "knowledge_at": pl.Datetime(time_unit="us", time_zone="UTC"), | |
| "source_url": pl.String, | |
| } | |
| ), | |
| } | |
| assert build_pit(frames)["forecast"].null_count() == build_pit(frames).height | |