Skip to content

test: cover FinancialForecastModel linear-only predict and short-history AR padding - #144

Merged
shivamlalakiya merged 2 commits into
PhilanthroPy-Project:mainfrom
slegarraga:fix/issue-59-forecast-branches
Aug 24, 2026
Merged

test: cover FinancialForecastModel linear-only predict and short-history AR padding#144
shivamlalakiya merged 2 commits into
PhilanthroPy-Project:mainfrom
slegarraga:fix/issue-59-forecast-branches

Conversation

@slegarraga

Copy link
Copy Markdown
Contributor

Closes #59. Test-only, no source change.

What

Two named tests in tests/test_forecast_model.py, exactly as specced:

  1. test_predict_without_nonlinear_model_uses_linear_only: fits on a single training sample, which leaves the residual network unfitted (nonlinear_model_ is None), then asserts predict(X) matches linear_model_.predict on the same NaN-free input exactly. The probe is NaN-free on purpose, so imputation is the identity and the comparison against the linear sub-model is direct.
  2. test_predict_revenue_forecast_pads_short_history_with_training_mean: fits with ar_order=5, calls predict_revenue_forecast with 2 context rows (fewer than p), asserts a finite (horizon,) array, and then reproduces the documented roll-forward by hand from the frozen fitted statistics (predict history reversed most-recent-first, padded to p with y_mean_), so the test pins the pad value rather than just finiteness.

Evidence

Coverage of philanthropy/models/_forecast.py from this file, before and after (branch coverage):

before: 90 stmts, Miss 1, BrPart 2, 97%, Missing: 288->290, 341
after:  90 stmts, Miss 0, BrPart 0, 100%

341 is the window = list(history[::-1]) + [self.y_mean_] * (p - history.size) padding line and 288->290 the if self.nonlinear_model_ is not None guard, the two branches named in the issue. Unrelated partial branches elsewhere in the module: none left in this file, nothing else touched.

Local verification

  • python -m pytest tests/ -q: 1910 passed, 25 skipped.
  • python -m pytest tests/test_forecast_model.py -q: 78 passed, 1 skipped.
  • flake8 tests/test_forecast_model.py: clean.

Test-only PR, so no CHANGELOG entry per the precedent noted in #139.

…ory AR padding

Closes PhilanthroPy-Project#59. Two named tests: single-sample fit leaves nonlinear_model_ as
None so predict must return the bare linear prediction; a context shorter
than ar_order must pad the seed window with y_mean_ instead of crashing,
pinned by reproducing the roll-forward from the frozen fitted statistics.
_forecast.py goes 97% (Missing: 288->290, 341) to 100% line and branch.
@shivamlalakiya
shivamlalakiya merged commit 42622ea into PhilanthroPy-Project:main Aug 24, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: FinancialForecastModel has two untested branches — linear-only predict and short-history AR padding

2 participants