"""Mechanical DCF discount path (BSX 1953a5992ac21d82 → 4875fbbcadf66091): two_stage_per_share discounted each year's flow at 1 / (1 + k_t)^t and the terminal value at the terminal rate for the whole horizon, so a rate path that changes year by year was not compounded and the terminal value (82% of the BSX value) never saw the early-year rates: a beta corrected from 0.44 to 0.98 moved the value by $0.17. Only the present-value discounting is tested here; the rate path, growth, payout and terminal economics are unchanged. BSX inputs are those of run 4875fbbcadf66091. """ import copy import os import sys import pytest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import valuation_engines as VE # noqa: E402 # the corrected BSX run's inputs: forecast EPS, first-stage growth, ROE, rates; beta 0.984 (0.443 before the fix) BSX = dict(flow0=3.545299637882127, g1=0.08908, g_term=0.04, N=5, rf=0.0524, erp=0.0409, beta_stable=1.0, roe_d=0.147092, roe_stable=0.147092, basis="eps", survival=1.0) BETA_NOW, BETA_BEFORE = 0.9838702203549254, 0.44347087551022324 def shortcut(state): """The same flows and rates discounted the OLD way: 1 / (1 + k_t)^t per year and the terminal value at the terminal rate for the whole horizon. Returns a state as that implementation would have stored it.""" s = copy.deepcopy(state) ke, n = s["ke_path"], len(s["ke_path"]) s["period_discount_factors"] = [1.0 / (1.0 + k) for k in ke] s["cumulative_discount_factors"] = [1.0 / (1.0 + k) ** (t + 1) for t, k in enumerate(ke)] s["pv_flows"] = [f * d for f, d in zip(s["flow_path"], s["cumulative_discount_factors"])] s["terminal_discount_factor"] = 1.0 / (1.0 + s["ke_terminal"]) ** n s["pv_terminal_value"] = s["terminal_value"] * s["terminal_discount_factor"] s["total_value"] = (sum(s["pv_flows"]) + s["pv_terminal_value"]) * s["survival"] return s def run(beta0, **over): v, meta = VE.two_stage_per_share(**dict(BSX, beta0=beta0, **over)) return v, meta["state"] def prod(xs): out = 1.0 for x in xs: out *= x return out # ── A: a constant rate path: both formulas agree ── def test_A_constant_rate_path_matches_the_power_formula(): v, s = run(1.0) # beta 1.0 throughout: every year at the stable rate assert len(set(round(k, 12) for k in s["ke_path"])) == 1 k = s["ke_path"][0] assert s["cumulative_discount_factors"] == pytest.approx([1.0 / (1.0 + k) ** t for t in range(1, 6)]) assert shortcut(s)["total_value"] == pytest.approx(v) assert VE.check_mechanical_dcf(s, v) == [] and VE.check_mechanical_dcf(shortcut(s), v) == [] # ── B: a rising path 7% → 8% → 9% ── def test_B_rising_path_compounds_the_yearly_rates(): v, s = run(0.5, rf=0.05, erp=0.04, N=3) assert s["ke_path"] == pytest.approx([0.07, 0.08, 0.09]) and s["ke_terminal"] == pytest.approx(0.09) assert s["cumulative_discount_factors"] == pytest.approx([1 / 1.07, 1 / (1.07 * 1.08), 1 / (1.07 * 1.08 * 1.09)]) assert s["terminal_discount_factor"] == pytest.approx(1 / (1.07 * 1.08 * 1.09)) assert s["pv_terminal_value"] == pytest.approx(s["terminal_value"] / (1.07 * 1.08 * 1.09)) assert v == pytest.approx(sum(s["pv_flows"]) + s["pv_terminal_value"]) and VE.check_mechanical_dcf(s, v) == [] old = shortcut(s) # terminal rate to the power n assert old["terminal_discount_factor"] == pytest.approx(1 / 1.09 ** 3) and old["total_value"] < v bad = VE.check_mechanical_dcf(old, old["total_value"]) assert bad and any("terminal discount factor" in b for b in bad) and any("cumulative" in b for b in bad) # ── C: a falling path 12% → 10% → 9% ── def test_C_falling_path_follows_the_same_rule(): period, cum = VE.discount_factors([0.12, 0.10, 0.09]) assert period == pytest.approx([1 / 1.12, 1 / 1.10, 1 / 1.09]) assert cum == pytest.approx([1 / 1.12, 1 / (1.12 * 1.10), 1 / (1.12 * 1.10 * 1.09)]) # a state on exactly that path: the cumulative rule validates, the shortcut does not flows, tv = [1.0, 1.1, 1.2], 25.0 s = {"ke_path": [0.12, 0.10, 0.09], "ke_terminal": 0.09, "flow_path": flows, "period_discount_factors": period, "cumulative_discount_factors": cum, "pv_flows": [f * d for f, d in zip(flows, cum)], "terminal_value": tv, "terminal_discount_factor": cum[-1], "pv_terminal_value": tv * cum[-1], "survival": 1.0} s["total_value"] = sum(s["pv_flows"]) + s["pv_terminal_value"] assert VE.check_mechanical_dcf(s, s["total_value"]) == [] old = shortcut(s) assert old["total_value"] > s["total_value"] # a falling path: the shortcut over-values assert VE.check_mechanical_dcf(old, old["total_value"]) v, st = run(1.75, rf=0.05, erp=0.04, N=3) # the model's own falling path (12% → 10.5% → 9%) assert st["ke_path"][0] == pytest.approx(0.12) and st["ke_path"][-1] == pytest.approx(0.09) assert st["terminal_discount_factor"] == pytest.approx(prod(1 / (1 + k) for k in st["ke_path"])) and VE.check_mechanical_dcf(st, v) == [] # ── D: the corrected BSX path 9.26% → 9.33% ── def test_D_bsx_current_path(): v, s = run(BETA_NOW) assert s["ke_path"][0] == pytest.approx(0.09264, abs=1e-5) and s["ke_path"][-1] == pytest.approx(0.0933) == pytest.approx(s["ke_terminal"]) assert 49.06 < v < 49.08 # the diagnostic's cumulative value assert shortcut(s)["total_value"] == pytest.approx(49.005, abs=0.002) # what the old implementation published assert VE.check_mechanical_dcf(s, v) == [] # exactly reproducible from the stored path state alone period, cum = VE.discount_factors(s["ke_path"]) assert (sum(f * d for f, d in zip(s["flow_path"], cum)) + s["terminal_value"] * cum[-1]) * s["survival"] == pytest.approx(v, rel=1e-12) # the rate path, growth, payout and terminal economics are what they were: only the discounting changed assert s["growth_path"] == pytest.approx([0.08908, 0.07681, 0.06454, 0.05227, 0.04]) and s["terminal_value"] == pytest.approx(63.18, abs=0.01) # ── E: the low-beta path of the run before the mapping fix (7.05% → 9.33%) ── def test_E_low_beta_path_differs_materially_and_the_shortcut_is_caught(): v, s = run(BETA_BEFORE) assert s["ke_path"][0] == pytest.approx(0.07054, abs=1e-5) and s["ke_terminal"] == pytest.approx(0.0933) old = shortcut(s) assert old["total_value"] == pytest.approx(49.173, abs=0.002) and v == pytest.approx(51.56, abs=0.01) assert v / old["total_value"] - 1 > 0.04 # the shortcut understated this path by almost 5% assert VE.check_mechanical_dcf(s, v) == [] bad = VE.check_mechanical_dcf(old, old["total_value"]) assert any("terminal discount factor" in b for b in bad) and any("stored total" in b or "pv_flows" in b for b in bad) # and a published value that is not the recomputed one fails even when the stored path is sound assert any("published mechanical value" in b for b in VE.check_mechanical_dcf(s, old["total_value"])) def test_the_forecast_stage_blocks_a_mechanical_value_from_the_shortcut(): """MECHANICAL_DCF_DISCOUNT_PATH in the pipeline: the stored path state must reproduce the published value.""" sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import pipeline_fixtures as F v, s = run(BETA_BEFORE) def staged(state, value): fv = F.fair_val() fv["methods"]["DCF"]["price"] = value fv["dcf_meta"]["dcf_state"] = state r, payload, failed = F.run_pipeline(forecast_over={"fair_val": fv}) return failed, next(c for val in r.validations for c in val.checks if c.id == "MECHANICAL_DCF_DISCOUNT_PATH") failed, chk = staged(s, v) assert failed is None and chk.status == "PASS" and "reproduces" in chk.detail old = shortcut(s) failed, chk = staged(old, old["total_value"]) assert failed == "FORECAST" and chk.status == "FAIL" and "terminal discount factor" in chk.detail # a run with no two-stage path (a direct book-value model) is not applicable, not a failure r, payload, failed = F.run_pipeline() assert failed is None and next(c for val in r.validations for c in val.checks if c.id == "MECHANICAL_DCF_DISCOUNT_PATH").status == "NOT_APPLICABLE"