From f5046e6bad7f788a1ebb841be31720f32f0f2b30 Mon Sep 17 00:00:00 2001 From: warriord Date: Sun, 29 Nov 2026 23:48:07 +0100 Subject: [PATCH] feat(ladder): fuf's FORECAST REPORT in the .out, as the C writes it After its own forecast block the C writes fuf's table series by series (escalera.c, write_fuf_report): the last H+1 observations with their period and annual variation and their exact residual (ERR), then the H forecasts with their standard deviations. The port did not write it. Line for line the C's (atsw-gui main e964abd) on the three price indices, from the last datum and from an inner origin, and on an annual pair. With -estwin N -O -1 the ERR column carries the one-step errors of the observations that arrived after the window. Suite: 417 passed, 75 skipped, 2 xfailed. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01LHFMF3teQWFeXfDJVGj2p5 --- CHANGELOG.md | 9 ++++ src/drvarma/ladder.py | 62 +++++++++++++++++++++- tests/data/ladder/fuf_report/README.md | 12 +++++ tests/data/ladder/fuf_report/ann.txt | 40 ++++++++++++++ tests/data/ladder/fuf_report/end.txt | 72 ++++++++++++++++++++++++++ tests/data/ladder/fuf_report/g240.txt | 72 ++++++++++++++++++++++++++ tests/test_fuf_report.py | 69 ++++++++++++++++++++++++ 7 files changed, 335 insertions(+), 1 deletion(-) create mode 100644 tests/data/ladder/fuf_report/README.md create mode 100644 tests/data/ladder/fuf_report/ann.txt create mode 100644 tests/data/ladder/fuf_report/end.txt create mode 100644 tests/data/ladder/fuf_report/g240.txt create mode 100644 tests/test_fuf_report.py diff --git a/CHANGELOG.md b/CHANGELOG.md index aa7090e..facd53e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,15 @@ compiled C engine. ## Unreleased +**fuf's FORECAST REPORT in the ladder's `.out`, as the C writes it.** After +its own forecast block the C writes fuf's table series by series: the last +H+1 observations with their period and annual variation and their exact +residual (ERR), then the H forecasts with their standard deviations. The port +did not. It does now, line for line the C's on the three price indices (from +the last datum and from an inner origin) and on an annual pair +(`tests/test_fuf_report.py`). With `-estwin N -O -1` its ERR column carries the +one-step errors of the observations that arrived after the window. + **`-O g`: the origin of the forecast report, as in the C (atsw-gui #231).** The ladder forecast with fixed parameters from every origin (`Ladder.recursive`), but the report always left from the end of the diff --git a/src/drvarma/ladder.py b/src/drvarma/ladder.py index 5ab72f4..0d4560a 100644 --- a/src/drvarma/ladder.py +++ b/src/drvarma/ladder.py @@ -2230,7 +2230,10 @@ def lead_sd(l, d): "level": lvl, "low95": lo, "high95": hi, "sd": sd, "sd_period": np.array([lead_sd(l, 1) for l in range(1, L + 1)]), "sd_annual": np.array([lead_sd(l, s.freq) for l in range(1, L + 1)]), - "ystar": ystar}) + "ystar": ystar, + # the exact residuals of this series (rows 1..n_stat of + # the stationary sample), for fuf's ERR column + "resid": np.asarray(a)[:, c].copy()}) return out def recursive(self, H): @@ -2508,6 +2511,7 @@ def write_report(L, path, forecasts=None, recursive=None): if forecasts: out += ["", "=" * 61, " FORECASTS (each series back to its level with its model)", "=" * 61] out += _forecast_lines(forecasts, L) + out += _fuf_report_lines(forecasts, L) if recursive: _rows, summary = recursive out += ["", "=" * 61, " RECURSIVE FORECAST EVALUATION (out of sample)", "=" * 61, @@ -2523,6 +2527,62 @@ def write_report(L, path, forecasts=None, recursive=None): fh.write("\n".join(out) + "\n") +def _fuf_report_lines(forecasts, L): + """THE FORECAST REPORT OF fuf, series by series, as the C writes it in the + .out after the block above (escalera.c, write_fuf_report; fuf's + forecast_table_acii): the last H+1 observations with their period and + annual variation and their exact residual (ERR), then the H forecasts with + their standard deviations. In % if the model is in logarithms; if not, in + the units of the series (/refactor), as drtran. The same table in fue, + drtran, drvec and here.""" + bar = " +" + "-" * 76 + "+" + out = [] + for fc in forecasts: + s = next(z for z in L.series if z.name == fc["series"]) + n, ys, freq = fc["nobs"], fc["ystar"], s.freq + H = len(fc["level"]) + vs = (100.0 if abs(s.model.boxlam) < 1e-8 else 1.0) / s.model.refactor + res = fc["resid"] + n_stat = len(res) + + def date(obs): + y, p = s.date_of(obs) + return f" {p:2d}/{y:4d}" if freq > 1 else f" {y:4d}" + + y0, p0 = s.date_of(n) + out += ["", "FORECAST REPORT: ", f"VARIABLE NAME: {s.name}", + (f"FORECAST ORIGIN : {p0:2d}/{y0:4d}" if freq > 1 + else f"FORECAST ORIGIN : {y0:4d}"), + f"LEAD TIME FOR FORECASTING: {H}", "", bar, + " | | LEVEL | VARIATION | |", + " | DATE +--------------------+-------------------------------------+ ERR |", + " | | VALUE | STD | TRIMEST | STD | ANUAL | STD | |", + " | | | | /MONT | | | | |", + bar] + dash9, dash8 = f" {'-':>9}", f" {'-':>8}" + for obs in range(n - H, n + 1): + if obs < 1: + continue + line = date(obs) + f" {float(s.ts.data[obs - 1]):11.4f}" + dash8 + line += (f" {vs * (ys[obs - 1] - ys[obs - 2]):9.4f}" if obs - 1 >= 1 + else dash9) + dash8 + line += (f" {vs * (ys[obs - 1] - ys[obs - 1 - freq]):9.4f}" + if freq > 1 and obs - freq >= 1 else dash9) + dash8 + tt = n_stat - H + (obs - (n - H)) + line += f" {vs * res[tt - 1]:9.4f}" if 1 <= tt <= n_stat else dash9 + out.append(line) + for l in range(1, H + 1): + f2 = ys[n + l - 1] - ys[n + l - 2] + f3 = ys[n + l - 1] - ys[n + l - 1 - freq] if n + l - freq >= 1 else 0.0 + line = date(n + l) + f" {fc['level'][l-1]:11.4f} {vs * fc['sd'][l-1]:8.4f}" + line += f" {vs * f2:9.4f} {vs * fc['sd_period'][l-1]:8.4f}" + line += (f" {vs * f3:9.4f} {vs * fc['sd_annual'][l-1]:8.4f}" if freq > 1 + else dash9 + dash8) + out.append(line + dash9) + out.append(bar) + return out + + def _forecast_lines(forecasts, L): out = [] for fc in forecasts: diff --git a/tests/data/ladder/fuf_report/README.md b/tests/data/ladder/fuf_report/README.md new file mode 100644 index 0000000..8cab922 --- /dev/null +++ b/tests/data/ladder/fuf_report/README.md @@ -0,0 +1,12 @@ +# fuf's FORECAST REPORT as the C writes it in the ladder's `.out` + +The blocks from `FORECAST REPORT:` up to the evaluation, trailing blanks +removed, of the C of record (atsw-gui `engines/drvarma`, main e964abd): + +``` +end.txt IPC_ES_ext IPC_FR_ext IPC_DE_ext 1 0 -forecast 6 -estwin 216 -O -1 +g240.txt IPC_ES_ext IPC_FR_ext IPC_DE_ext 1 0 -forecast 6 -estwin 216 -O 240 +ann.txt MUSKRAT_m03 MINK_m02 1 0 -forecast 4 +``` + +`tests/test_fuf_report.py` compares the port's `.out` with them, line by line. diff --git a/tests/data/ladder/fuf_report/ann.txt b/tests/data/ladder/fuf_report/ann.txt new file mode 100644 index 0000000..c0b1849 --- /dev/null +++ b/tests/data/ladder/fuf_report/ann.txt @@ -0,0 +1,40 @@ +FORECAST REPORT: +VARIABLE NAME: MUSKRAT +FORECAST ORIGIN : 1911 +LEAD TIME FOR FORECASTING: 4 + +----------------------------------------------------------------------------+ + | | LEVEL | VARIATION | | + | DATE +--------------------+-------------------------------------+ ERR | + | | VALUE | STD | TRIMEST | STD | ANUAL | STD | | + | | | | /MONT | | | | | + +----------------------------------------------------------------------------+ + 1907 407472.0000 - -53.4040 - - - -25.8237 + 1908 172418.0000 - -86.0050 - - - -108.4419 + 1909 302195.0000 - 56.1151 - - - 10.7589 + 1910 749142.0000 - 90.7856 - - - 4.5133 + 1911 963597.0000 - 25.1745 - - - -11.1418 + 1912 1108503.0710 25.8290 14.0093 25.8290 - - - + 1913 1042307.8089 38.3236 -6.1573 28.7516 - - - + 1914 705117.0815 43.5209 -39.0829 33.3545 - - - + 1915 410177.5219 47.6668 -54.1774 33.9040 - - - + +----------------------------------------------------------------------------+ +FORECAST REPORT: +VARIABLE NAME: MINK +FORECAST ORIGIN : 1911 +LEAD TIME FOR FORECASTING: 4 + +----------------------------------------------------------------------------+ + | | LEVEL | VARIATION | | + | DATE +--------------------+-------------------------------------+ ERR | + | | VALUE | STD | TRIMEST | STD | ANUAL | STD | | + | | | | /MONT | | | | | + +----------------------------------------------------------------------------+ + 1907 39169.0000 - -42.7342 - - - -7.5978 + 1908 21534.0000 - -59.8253 - - - -45.3975 + 1909 17857.0000 - -18.7237 - - - -0.5405 + 1910 21788.0000 - 19.8964 - - - -17.8064 + 1911 33008.0000 - 41.5391 - - - -21.7818 + 1912 54357.7302 23.3507 49.8837 23.3507 - - - + 1913 83311.5347 31.9771 42.7000 25.9713 - - - + 1914 100139.8863 35.3231 18.3981 26.9270 - - - + 1915 87144.7178 35.8348 -13.8998 28.4468 - - - + +----------------------------------------------------------------------------+ diff --git a/tests/data/ladder/fuf_report/end.txt b/tests/data/ladder/fuf_report/end.txt new file mode 100644 index 0000000..da62b2e --- /dev/null +++ b/tests/data/ladder/fuf_report/end.txt @@ -0,0 +1,72 @@ +FORECAST REPORT: +VARIABLE NAME: IPC_ES +FORECAST ORIGIN : 11/2023 +LEAD TIME FOR FORECASTING: 6 + +----------------------------------------------------------------------------+ + | | LEVEL | VARIATION | | + | DATE +--------------------+-------------------------------------+ ERR | + | | VALUE | STD | TRIMEST | STD | ANUAL | STD | | + | | | | /MONT | | | | | + +----------------------------------------------------------------------------+ + 5/2023 111.1152 - -0.1504 - 2.6013 - -0.0918 + 6/2023 111.7486 - 0.5684 - 1.3347 - 0.6007 + 7/2023 112.8447 - 0.9761 - 2.5660 - 1.4794 + 8/2023 113.4005 - 0.4913 - 2.8039 - -0.2222 + 9/2023 113.6024 - 0.1779 - 3.4556 - -0.1817 + 10/2023 113.2085 - -0.3473 - 3.4091 - -1.1349 + 11/2023 112.6692 - -0.4775 - 3.1285 - -0.3742 + 12/2023 112.4205 0.2481 -0.2210 0.2481 2.6537 0.2481 - + 1/2024 111.1386 0.4316 -1.1468 0.2715 0.5698 0.4316 - + 2/2024 111.2198 0.5839 0.0730 0.2747 -0.4002 0.5839 - + 3/2024 111.8910 0.7139 0.6016 0.2752 0.2324 0.7139 - + 4/2024 113.1371 0.8260 1.1076 0.2753 1.6530 0.8260 - + 5/2024 113.4795 0.9256 0.3022 0.2753 2.1055 0.9256 - + +----------------------------------------------------------------------------+ +FORECAST REPORT: +VARIABLE NAME: IPC_FR +FORECAST ORIGIN : 11/2023 +LEAD TIME FOR FORECASTING: 6 + +----------------------------------------------------------------------------+ + | | LEVEL | VARIATION | | + | DATE +--------------------+-------------------------------------+ ERR | + | | VALUE | STD | TRIMEST | STD | ANUAL | STD | | + | | | | /MONT | | | | | + +----------------------------------------------------------------------------+ + 5/2023 117.4400 - -0.0511 - 4.9932 - -0.1140 + 6/2023 117.6500 - 0.1787 - 4.4317 - 0.0049 + 7/2023 117.7100 - 0.0510 - 4.1987 - 0.0678 + 8/2023 118.8900 - 0.9975 - 4.7454 - 0.2846 + 9/2023 118.2600 - -0.5313 - 4.7801 - -0.3767 + 10/2023 118.4300 - 0.1436 - 3.9001 - -0.2201 + 11/2023 118.1700 - -0.2198 - 3.3648 - -0.0853 + 12/2023 118.1496 0.1781 -0.0173 0.1781 3.4350 0.1781 - + 1/2024 117.9021 0.2803 -0.2096 0.1870 2.8407 0.2803 - + 2/2024 118.5244 0.3667 0.5264 0.1884 2.3427 0.3667 - + 3/2024 119.3342 0.4409 0.6809 0.1886 2.1550 0.4409 - + 4/2024 119.7238 0.5055 0.3260 0.1886 1.8749 0.5055 - + 5/2024 119.8761 0.5632 0.1271 0.1886 2.0531 0.5632 - + +----------------------------------------------------------------------------+ +FORECAST REPORT: +VARIABLE NAME: IPC_DE +FORECAST ORIGIN : 11/2023 +LEAD TIME FOR FORECASTING: 6 + +----------------------------------------------------------------------------+ + | | LEVEL | VARIATION | | + | DATE +--------------------+-------------------------------------+ ERR | + | | VALUE | STD | TRIMEST | STD | ANUAL | STD | | + | | | | /MONT | | | | | + +----------------------------------------------------------------------------+ + 5/2023 116.5000 - -0.0858 - 5.9231 - -0.5085 + 6/2023 116.8000 - 0.2572 - 6.1803 - 0.1470 + 7/2023 117.1000 - 0.2565 - 5.9824 - -0.0513 + 8/2023 117.5000 - 0.3410 - 5.9614 - 0.1000 + 9/2023 117.8000 - 0.2550 - 4.4259 - -0.3457 + 10/2023 117.8000 - 0.0000 - 3.7185 - -0.1673 + 11/2023 117.3000 - -0.4254 - 3.1171 - -0.3246 + 12/2023 117.5524 0.2314 0.2149 0.2314 3.7728 0.2314 - + 1/2024 117.4610 0.3305 -0.0778 0.2333 2.7280 0.3305 - + 2/2024 118.2059 0.4194 0.6321 0.2345 2.5758 0.4194 - + 3/2024 118.8052 0.4825 0.5058 0.2354 2.3033 0.4825 - + 4/2024 119.0661 0.5401 0.2194 0.2355 2.0930 0.5401 - + 5/2024 119.1902 0.5908 0.1041 0.2355 2.2829 0.5908 - + +----------------------------------------------------------------------------+ diff --git a/tests/data/ladder/fuf_report/g240.txt b/tests/data/ladder/fuf_report/g240.txt new file mode 100644 index 0000000..076b021 --- /dev/null +++ b/tests/data/ladder/fuf_report/g240.txt @@ -0,0 +1,72 @@ +FORECAST REPORT: +VARIABLE NAME: IPC_ES +FORECAST ORIGIN : 12/2021 +LEAD TIME FOR FORECASTING: 6 + +----------------------------------------------------------------------------+ + | | LEVEL | VARIATION | | + | DATE +--------------------+-------------------------------------+ ERR | + | | VALUE | STD | TRIMEST | STD | ANUAL | STD | | + | | | | /MONT | | | | | + +----------------------------------------------------------------------------+ + 6/2021 100.0460 - 0.4749 - 2.6958 - 0.2375 + 7/2021 99.2920 - -0.7565 - 2.8408 - -0.2249 + 8/2021 99.7430 - 0.4532 - 3.2484 - 0.2230 + 9/2021 100.5750 - 0.8307 - 3.9290 - 0.5636 + 10/2021 102.4250 - 1.8227 - 5.2278 - 0.7994 + 11/2021 102.7380 - 0.3051 - 5.3695 - -0.3412 + 12/2021 103.9650 - 1.1872 - 6.3443 - 1.0162 + 1/2022 103.2346 0.2481 -0.7051 0.2481 5.6300 0.2481 - + 2/2022 103.4893 0.4316 0.2465 0.2715 6.4675 0.4316 - + 3/2022 104.1291 0.5839 0.6163 0.2747 6.1184 0.5839 - + 4/2022 105.2719 0.7139 1.0915 0.2752 6.0366 0.7139 - + 5/2022 105.5589 0.8260 0.2723 0.2753 5.8388 0.8260 - + 6/2022 105.7305 0.9256 0.1625 0.2753 5.5264 0.9256 - + +----------------------------------------------------------------------------+ +FORECAST REPORT: +VARIABLE NAME: IPC_FR +FORECAST ORIGIN : 12/2021 +LEAD TIME FOR FORECASTING: 6 + +----------------------------------------------------------------------------+ + | | LEVEL | VARIATION | | + | DATE +--------------------+-------------------------------------+ ERR | + | | VALUE | STD | TRIMEST | STD | ANUAL | STD | | + | | | | /MONT | | | | | + +----------------------------------------------------------------------------+ + 6/2021 106.3400 - 0.1317 - 1.4683 - 0.0079 + 7/2021 106.4000 - 0.0564 - 1.1437 - 0.0623 + 8/2021 107.0500 - 0.6090 - 1.8479 - 0.3811 + 9/2021 106.8100 - -0.2244 - 2.1386 - -0.0633 + 10/2021 107.2500 - 0.4111 - 2.5880 - 0.2172 + 11/2021 107.6400 - 0.3630 - 2.7407 - 0.0714 + 12/2021 107.8500 - 0.1949 - 2.7162 - -0.0636 + 1/2022 107.8520 0.1781 0.0018 0.1781 2.5657 0.1781 - + 2/2022 108.2083 0.2803 0.3299 0.1870 2.8956 0.2803 - + 3/2022 108.8955 0.3667 0.6330 0.1884 2.9311 0.3667 - + 4/2022 109.0928 0.4409 0.1810 0.1886 3.0081 0.4409 - + 5/2022 109.3398 0.5055 0.2262 0.1886 2.9136 0.5055 - + 6/2022 109.4476 0.5632 0.0986 0.1886 2.8804 0.5632 - + +----------------------------------------------------------------------------+ +FORECAST REPORT: +VARIABLE NAME: IPC_DE +FORECAST ORIGIN : 12/2021 +LEAD TIME FOR FORECASTING: 6 + +----------------------------------------------------------------------------+ + | | LEVEL | VARIATION | | + | DATE +--------------------+-------------------------------------+ ERR | + | | VALUE | STD | TRIMEST | STD | ANUAL | STD | | + | | | | /MONT | | | | | + +----------------------------------------------------------------------------+ + 6/2021 102.9000 - 0.2920 - 2.3600 - 0.1416 + 7/2021 103.4000 - 0.4847 - 3.6439 - 0.5281 + 8/2021 103.5000 - 0.0967 - 3.7406 - 0.0699 + 9/2021 103.8000 - 0.2894 - 4.0300 - 0.2528 + 10/2021 104.3000 - 0.4805 - 4.3102 - 0.5124 + 11/2021 104.5000 - 0.1916 - 4.7021 - 0.3487 + 12/2021 104.7000 - 0.1912 - 4.7931 - -0.2297 + 1/2022 104.7394 0.2314 0.0376 0.2314 3.6354 0.2314 - + 2/2022 105.3012 0.3305 0.5349 0.2333 3.5781 0.3305 - + 3/2022 105.7278 0.4194 0.4043 0.2345 3.4915 0.4194 - + 4/2022 105.9007 0.4825 0.1634 0.2354 3.3615 0.4825 - + 5/2022 106.0722 0.5401 0.1618 0.2355 3.3282 0.5401 - + 6/2022 106.2848 0.5908 0.2002 0.2355 3.2364 0.5908 - + +----------------------------------------------------------------------------+ diff --git a/tests/test_fuf_report.py b/tests/test_fuf_report.py new file mode 100644 index 0000000..678c80d --- /dev/null +++ b/tests/test_fuf_report.py @@ -0,0 +1,69 @@ +"""fuf's FORECAST REPORT in the ladder's .out: the table the C writes after +its own forecast block (escalera.c, write_fuf_report), the same in fue, +drtran, drvec and here. Its ERR column is where the one-step errors of the +observations that arrive after the window are read (tracking).""" +import os + +import pytest + +from drvarma.ladder import main + +D = os.path.join(os.path.dirname(__file__), "data", "ladder") +EXT = [os.path.join(D, f"IPC_{c}_ext.pre") for c in ("ES", "FR", "DE")] +ANN = [os.path.join(D, "MUSKRAT_m03.pre"), os.path.join(D, "MINK_m02.pre")] +CASES = {"end": EXT + "1 0 -forecast 6 -estwin 216 -O -1".split(), + "g240": EXT + "1 0 -forecast 6 -estwin 216 -O 240".split(), + "ann": ANN + "1 0 -forecast 4".split()} + + +def _blocks(text): + out, on = [], False + for line in text.splitlines(): + if line.startswith("FORECAST REPORT: "): + on = True + if "RECURSIVE FORECAST EVALUATION" in line: + on = False + line = line.rstrip() + if on and line.strip("=") != "": + out.append(line) + return out + + +@pytest.mark.parametrize("label", sorted(CASES)) +def test_the_table_is_the_cs(tmp_path, label): + name = str(tmp_path / "X") + assert main(CASES[label] + ["-o", name]) == 0 + mine = _blocks(open(name + ".out").read()) + theirs = open(os.path.join(D, "fuf_report", label + ".txt")).read().splitlines() + assert len(mine) == len(theirs) + for a, b in zip(mine, theirs): + if a == b: + continue + # off the C's platform the last digit of a number may move + ta, tb = a.split(), b.split() + assert len(ta) == len(tb), (a, b) + for x, y in zip(ta, tb): + if x != y: + assert float(x) == pytest.approx(float(y), abs=2e-4), (a, b) + + +def test_the_err_column_carries_the_new_observations(tmp_path): + """With the window at 216 and the origin at the last datum, the rows of + the last observations carry their one-step error with the window's + parameters: none of them is a dash.""" + name = str(tmp_path / "X") + assert main(CASES["end"] + ["-o", name]) == 0 + b = _blocks(open(name + ".out").read()) + i = b.index("VARIABLE NAME: IPC_ES") + assert b[i + 1] == "FORECAST ORIGIN : 11/2023" + obs = [l for l in b[i:i + 20] if l.strip().startswith(("5/2023", "11/2023"))] + assert len(obs) == 2 and all(l.split()[-1] != "-" for l in obs) + fc = [l for l in b[i:i + 20] if l.strip().startswith("12/2023")] + assert fc and fc[0].split()[-1] == "-" + + +def test_the_forecast_file_does_not_carry_it(tmp_path): + """As in the C: the table goes to the .out only.""" + name = str(tmp_path / "X") + assert main(CASES["ann"] + ["-o", name]) == 0 + assert "FORECAST REPORT" not in open(name + ".forecast").read()