feat(notebooks): market series panel in the event-study notebook (#797)
All checks were successful
CI / lint (push) Successful in 49s
CI / notebooks-smoke (push) Successful in 2m8s
Deploy / changes (push) Successful in 19s
Infra CI / notebooks (push) Successful in 1m4s
Infra CI / zotero (push) Successful in 17s
Infra CI / docs (push) Successful in 20s
CI / test (push) Successful in 5m9s
Infra CI / api (push) Successful in 20s
Infra CI / bcda (push) Successful in 18s
Infra CI / llm (push) Successful in 18s
Infra CI / mc (push) Successful in 23s
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Deploy / api (push) Has been skipped
Deploy / bcda (push) Has been skipped
Deploy / llm (push) Has been skipped
Deploy / mc (push) Has been skipped
Deploy / notebooks (push) Successful in 5m3s
Deploy / report (push) Successful in 15s
All checks were successful
CI / lint (push) Successful in 49s
CI / notebooks-smoke (push) Successful in 2m8s
Deploy / changes (push) Successful in 19s
Infra CI / notebooks (push) Successful in 1m4s
Infra CI / zotero (push) Successful in 17s
Infra CI / docs (push) Successful in 20s
CI / test (push) Successful in 5m9s
Infra CI / api (push) Successful in 20s
Infra CI / bcda (push) Successful in 18s
Infra CI / llm (push) Successful in 18s
Infra CI / mc (push) Successful in 23s
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Deploy / api (push) Has been skipped
Deploy / bcda (push) Has been skipped
Deploy / llm (push) Has been skipped
Deploy / mc (push) Has been skipped
Deploy / notebooks (push) Successful in 5m3s
Deploy / report (push) Successful in 15s
This commit is contained in:
@@ -427,9 +427,135 @@ def _(alt, code, con, kind, mo, not_built, pl, year):
|
||||
return
|
||||
|
||||
|
||||
@app.cell(hide_code=True)
|
||||
def _(code, con, kind, mo, not_built, year):
|
||||
# ── 4. Market series (#797) ──
|
||||
from market.enrich import annual_tri as _annual_tri
|
||||
from market.enrich import joint_annual as _joint
|
||||
from market.tables import read_cars as _read_cars
|
||||
from market.tables import read_series as _read_series
|
||||
from pfs.codetables import read_exposures as _read_exp
|
||||
from pfs.event_study import pre_post as _pre_post
|
||||
|
||||
_head = (
|
||||
f"## 4. Market series (#797) around {code} {kind} ({year})\n\n"
|
||||
"These series are national indexes and the exposed family's equal-weight "
|
||||
"portfolio, not code-specific, so there is no control set: the table reports "
|
||||
"the pre- and post-window means and the log change for each, which is context "
|
||||
"for the exposure above, not an estimate of the exposure's effect. It is not "
|
||||
"a causal estimate."
|
||||
)
|
||||
|
||||
def _num(value):
|
||||
return "—" if value is None else f"{value:.6g}"
|
||||
|
||||
def _md(headers, rows):
|
||||
line = "| " + " | ".join(headers) + " |"
|
||||
rule = "| " + " | ".join("---" for _ in headers) + " |"
|
||||
body = ["| " + " | ".join(str(cell) for cell in row) + " |" for row in rows]
|
||||
return "\n".join([line, rule, *body])
|
||||
|
||||
_tris = {}
|
||||
_bhar = {}
|
||||
_cars = []
|
||||
_keys = []
|
||||
_family = ""
|
||||
if con is not None:
|
||||
try:
|
||||
for _row in _read_exp(con, code=code):
|
||||
if _row.year == year and _row.kind == kind:
|
||||
_family = _row.family or ""
|
||||
break
|
||||
except Exception: # noqa: BLE001
|
||||
_family = ""
|
||||
try:
|
||||
for _sym in ("XLV", "IHF", "SPY"):
|
||||
_got = _annual_tri(con, _sym)
|
||||
if _got:
|
||||
_tris[_sym] = _got
|
||||
except Exception: # noqa: BLE001
|
||||
_tris = {}
|
||||
if _family:
|
||||
try:
|
||||
_bkey = f"market:family:{_family}:bhar"
|
||||
for _jy in _joint(
|
||||
con, family=_family, years=range(int(year) - 2, int(year) + 2)
|
||||
):
|
||||
_val = _jy.values.get(_bkey)
|
||||
if _val is not None:
|
||||
_bhar[int(_jy.year)] = float(_val)
|
||||
except Exception: # noqa: BLE001
|
||||
_bhar = {}
|
||||
try:
|
||||
_cars = _read_cars(con, "family", _family)
|
||||
except Exception: # noqa: BLE001
|
||||
_cars = []
|
||||
try:
|
||||
for _row in _read_series(con, ["XLV", "IHF", "SPY"]):
|
||||
if (
|
||||
str(_row.series_id).endswith(":tri")
|
||||
and _row.item_key
|
||||
and _row.item_key not in _keys
|
||||
):
|
||||
_keys.append(_row.item_key)
|
||||
except Exception: # noqa: BLE001
|
||||
_keys = []
|
||||
if con is None or not (_tris or _bhar or _cars or _keys):
|
||||
_view = mo.md(_head + "\n\n" + not_built("stack market prices --write"))
|
||||
else:
|
||||
_prows = []
|
||||
for _sym, _label in (
|
||||
("XLV", "XLV total return"),
|
||||
("IHF", "IHF total return"),
|
||||
("SPY", "SPY total return"),
|
||||
):
|
||||
if _sym not in _tris:
|
||||
continue
|
||||
_p = _pre_post(_tris[_sym], int(year))
|
||||
_prows.append(
|
||||
(
|
||||
_label,
|
||||
", ".join(map(str, _p.pre_years)),
|
||||
_num(_p.pre_mean),
|
||||
", ".join(map(str, _p.post_years)),
|
||||
_num(_p.post_mean),
|
||||
_num(_p.change),
|
||||
)
|
||||
)
|
||||
if _bhar:
|
||||
_p = _pre_post(_bhar, int(year))
|
||||
_prows.append(
|
||||
(
|
||||
f"{_family} family portfolio BHAR",
|
||||
", ".join(map(str, _p.pre_years)),
|
||||
_num(_p.pre_mean),
|
||||
", ".join(map(str, _p.post_years)),
|
||||
_num(_p.post_mean),
|
||||
_num(_p.change),
|
||||
)
|
||||
)
|
||||
_crows = [
|
||||
(_r.event_id, f"{_r.win_lo}, {_r.win_hi}", _num(_r.car), _r.caveats)
|
||||
for _r in _cars
|
||||
]
|
||||
_view = mo.md(
|
||||
_head
|
||||
+ "\n\n"
|
||||
+ _md(
|
||||
["series", "pre", "pre mean", "post", "post mean", "log change"],
|
||||
_prows,
|
||||
)
|
||||
+ "\n\n"
|
||||
+ _md(["event", "window", "CAR", "caveat"], _crows)
|
||||
+ f"\n\nProvenance: {', '.join(_keys)}"
|
||||
)
|
||||
_view
|
||||
return
|
||||
|
||||
|
||||
@app.cell(hide_code=True)
|
||||
def _(FIXTURES, con, fr_md, mo, not_built, pl):
|
||||
# ── 4. All fixture exposures ──
|
||||
# ── 5. All fixture exposures ──
|
||||
from pfs.event_study import utilization_panel as _panel_all
|
||||
|
||||
_rows = []
|
||||
@@ -473,7 +599,7 @@ def _(FIXTURES, con, fr_md, mo, not_built, pl):
|
||||
}
|
||||
)
|
||||
_head = (
|
||||
"## 4. All fixture exposures\n\n"
|
||||
"## 5. All fixture exposures\n\n"
|
||||
"The same contrast for every fixture event, with the FR anchor that dates it. A new "
|
||||
"code has no pre-period by construction — its first years are the outcome, not a "
|
||||
"change — and a 2025 exposure has no post-period until CMS publishes the 2025 file; "
|
||||
@@ -501,7 +627,7 @@ def _(FIXTURES, con, fr_md, mo, not_built, pl):
|
||||
|
||||
@app.cell(hide_code=True)
|
||||
def _(NOTES, REPLICA_PATH, con, mo, pl):
|
||||
# ── 5. Provenance ──
|
||||
# ── 6. Provenance ──
|
||||
import datetime as _dt
|
||||
|
||||
if REPLICA_PATH.exists():
|
||||
@@ -535,13 +661,12 @@ def _(NOTES, REPLICA_PATH, con, mo, pl):
|
||||
mo.vstack(
|
||||
[
|
||||
mo.md(
|
||||
"## 5. Provenance\n\n"
|
||||
"## 6. Provenance\n\n"
|
||||
f"Replica: {_line}. Every outcome row carries the bib key of the dataset "
|
||||
"release it came from (`item_key`), and each release is a Source item "
|
||||
"tagged with its table; `cms.ingest_log` records the ingest with the source "
|
||||
"file's sha256. Market and industry series (sector ETFs, telehealth and "
|
||||
"care-management tickers) are not on file: no keyless quote source is "
|
||||
"reachable from the rack, so that panel waits on a data source (#696 follow-up)."
|
||||
"file's sha256. Section 4 reads the stored total-return indexes "
|
||||
"(XLV, IHF and SPY) and the family's portfolio; raw prices stay inside the stack."
|
||||
+ (f"\n\n{_notes}" if _notes else "")
|
||||
),
|
||||
mo.ui.table(_counts, label="row counts")
|
||||
|
||||
@@ -32,7 +32,7 @@ def test_cells_are_anonymous_and_banners_present():
|
||||
src = NB.read_text()
|
||||
names = [n.name for n in ast.parse(src).body if isinstance(n, ast.FunctionDef)]
|
||||
assert names and set(names) == {"_"}
|
||||
assert re.findall(r"# ── (\S+)\. ", src) == ["0", "1", "2", "3", "4", "5"]
|
||||
assert re.findall(r"# ── (\S+)\. ", src) == ["0", "1", "2", "3", "4", "5", "6"]
|
||||
|
||||
|
||||
def test_no_causal_language():
|
||||
@@ -60,6 +60,131 @@ def test_headless_run_degrades_without_data(monkeypatch):
|
||||
assert "Not built yet" in rendered and "Traceback" not in rendered
|
||||
|
||||
|
||||
def _rendered(outputs) -> str:
|
||||
return "\n".join(o._repr_html_() for o in outputs if hasattr(o, "_repr_html_"))
|
||||
|
||||
|
||||
def test_market_panel_headless_not_built(monkeypatch):
|
||||
monkeypatch.setenv("STACK_DUCKDB_REPLICA", "1")
|
||||
mod = _load()
|
||||
import conf.connect as cc
|
||||
|
||||
monkeypatch.setattr(
|
||||
cc,
|
||||
"duckdb",
|
||||
lambda *a, **k: (_ for _ in ()).throw(FileNotFoundError("no replica")),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
cc, "bib", lambda *a, **k: (_ for _ in ()).throw(FileNotFoundError("no bib"))
|
||||
)
|
||||
outputs, _ = mod.app.run()
|
||||
rendered = _rendered(outputs)
|
||||
assert "stack market prices --write" in rendered
|
||||
assert "Traceback" not in rendered
|
||||
|
||||
|
||||
def test_market_panel_renders_from_memory_tables(monkeypatch):
|
||||
from datetime import date
|
||||
|
||||
import duckdb
|
||||
|
||||
from market.tables import (
|
||||
CarRow,
|
||||
MonthlyRow,
|
||||
ensure_tables,
|
||||
write_cars,
|
||||
write_series,
|
||||
)
|
||||
from pfs.codetables import ensure_tables as ensure_pfs
|
||||
|
||||
con = duckdb.connect(":memory:")
|
||||
ensure_tables(con)
|
||||
ensure_pfs(con)
|
||||
con.execute(
|
||||
"INSERT INTO pfs.code_exposure VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
|
||||
[
|
||||
"99490",
|
||||
"CCM",
|
||||
date(2015, 1, 1),
|
||||
2015,
|
||||
"becomes-payable",
|
||||
"",
|
||||
"",
|
||||
0,
|
||||
0.0,
|
||||
0.0,
|
||||
"exp-key",
|
||||
1,
|
||||
0,
|
||||
"fr",
|
||||
False,
|
||||
"",
|
||||
],
|
||||
)
|
||||
write_series(
|
||||
con,
|
||||
"tiingo:XLV:tri",
|
||||
[
|
||||
MonthlyRow("tiingo:XLV:tri", "XLV", "tiingo", y, 12, v, "xlv-bib")
|
||||
for y, v in ((2013, 100.0), (2014, 110.0), (2015, 120.0), (2016, 130.0))
|
||||
],
|
||||
)
|
||||
write_cars(
|
||||
con,
|
||||
"family",
|
||||
"CCM",
|
||||
"market",
|
||||
[
|
||||
CarRow(
|
||||
"rule-final:2015",
|
||||
"family",
|
||||
"CCM",
|
||||
"portfolio:family:CCM",
|
||||
"",
|
||||
"market",
|
||||
-1,
|
||||
1,
|
||||
0.02,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
10,
|
||||
3,
|
||||
0,
|
||||
'["association, not a causal estimate"]',
|
||||
date(2015, 12, 31),
|
||||
)
|
||||
],
|
||||
)
|
||||
import conf.connect as cc
|
||||
|
||||
monkeypatch.setattr(cc, "duckdb", lambda *a, **k: con)
|
||||
mod = _load()
|
||||
outputs, _ = mod.app.run()
|
||||
con.close()
|
||||
rendered = _rendered(outputs)
|
||||
assert "Traceback" not in rendered
|
||||
assert "XLV" in rendered
|
||||
assert "caveat" in rendered
|
||||
assert "xlv-bib" in rendered
|
||||
|
||||
|
||||
@_live
|
||||
def test_market_panel_live_ccm_2015_apcm_2025():
|
||||
mod = _load()
|
||||
outputs, _ = mod.app.run()
|
||||
rendered = _rendered(outputs)
|
||||
assert "Traceback" not in rendered
|
||||
assert "Market series (#797)" in rendered
|
||||
assert "XLV" in rendered
|
||||
assert "no control set" in rendered
|
||||
assert "Provenance:" in rendered
|
||||
assert "caveat" in rendered
|
||||
assert "stack market prices --write" not in rendered
|
||||
assert "99490 becomes-payable 2015" in rendered and "DE2VH9PD" in rendered
|
||||
assert "G0556 becomes-payable 2025" in rendered and "JJ6AM5HJ" in rendered
|
||||
|
||||
|
||||
@_live
|
||||
def test_ccm_2015_and_apcm_2025_panels_render_with_anchors():
|
||||
mod = _load()
|
||||
|
||||
Reference in New Issue
Block a user