Skip to content

BUG: Fix the impacts and details tables of NewsResults - #11080

Merged
bashtage merged 9 commits into
statsmodels:mainfrom
bashtage:wip-news-summary-tables
Nov 25, 2026
Merged

bashtage merged 9 commits into
statsmodels:mainfrom
bashtage:wip-news-summary-tables

Conversation

@bashtage

Copy link
Copy Markdown
Member

Stacked on #11078, merge that first (its commits are the first ones here). The five commits after them fix one bug each (B1, B5, B4, B2 and B8 of the C11 audit), each with its own tests; the last one changes results of valid input (Behavior changes). The other C11 follow-up PRs (the NewsResults table fixes and the get_impacts fix) also append to test_news.py: keep both appended blocks. No conflict with #11054, #11057, #11058 or #11029.

TST: Cover the tables and summaries of NewsResults

Add tests of the paths of statespace/news.py that the TSA test suite did
not reach, each checked against an independent value:

  • closed forms for an AR(1) model with a revision and an update: the
    weights, revisions, news and impacts of revision_details_by_impact
    (with all revisions detailed) and details_by_impact, and the estimates
    of the impacts table; with two updates, the details table of each
    update date;
  • the details tables summed over the revisions or updates give the
    revision and update impacts, and these add up to the change in the
    estimates, with detailed and with grouped revisions;
  • impacted_variable (str, list, array and slice, given to news or set on
    the results) and tolerance (set on the results) select the rows of
    every details table and of the impacts table;
  • summary_impacts, summary_details, summary_news and summary_revisions:
    every cell of the tables against the DataFrames they show (the AR(1)
    closed forms for the univariate details), with and without sparsifying,
    with each groupby (and its underscore spelling), the impact and update
    date and variable selections, multiple tables, the univariate defaults,
    show_revisions_columns, the weights shown as 1.0 and 0, equal values of
    different variables (not blanked), and the errors for an invalid
    groupby;
  • summary: the header (samples, revisions, new datapoints, included
    states) and its tables against the individual summary methods with
    every argument (including the revision date and revised variable of
    the details of revisions), for univariate and multivariate models;
  • state_index: in a VAR(1) the impacts of the news and the detailed
    impacts of the revisions through the first state are those of the
    first variable;
  • get_details and get_impacts with each include flag (summing to the
    matching impacts), and get_impacts with a groupby of the updated
    variables.

Two bugs of NewsResults, not fixed here, shape the tests: the univariate
summaries are tested with a one-letter series name (k_endog is the length
of the name of the series of a univariate model), and
test_get_impacts_groupby ignores the two FutureWarnings that pandas < 3
raises in get_impacts with a groupby (stack without future_stack).

Coverage of news.py by the TSA tests goes from 85.6% to 97.0% of the
lines and from 63.9% to 98.3% of the branches.

DOC: Correct the docstrings of NewsResults

Fix the docstrings of statespace/news.py where they disagree with the
code. No code changes.

  • get_impacts: groupby groups the updated (or revised) variables
    separately for each update date, for example with a dict or a function
    of the variable names; it is not applied to the combined details table
    and cannot group the update dates, which the docstring gave as its
    example (that raises a KeyError). The index of the result has the
    levels update date and updated variable (or the groups).
  • get_details also renames the index levels of the revision details, and
    both get_details and get_impacts return an empty table if both include
    flags are False.
  • summary_news has a news column; the columns of data_updates,
    data_revisions, details_by_impact and revision_details_by_impact and
    the index levels of details_by_update and revision_details_by_update
    are listed in their actual order; the details tables and impacts only
    include the rows above tolerance and of the selected
    impacted_variable.
  • impacted_variable and tolerance (settable properties) get docstrings.
  • summary_details: updated_variable are the variables that generated
    the news, not those affected by it, and with source="revisions",
    update_date, updated_variable and groupby refer to the revision
    dates and revised variables.
  • summary: the news table is listed, the revisions tables are shown only
    with include_revisions_tables=True (default False) and only if the
    data were revised, and revision_date and revised_variable apply to
    the details tables of revisions.
  • NewsResults takes the results of SmootherResults.news (there is no
    KalmanSmoother.news).

BUG: Show the impact dates of NewsResults.summary_impacts as text

summary_impacts (and the impacts table of summary) formats the impact
dates and impacted variables as strings and writes them back with a
block assignment (impacts.iloc[:, :2] = ...). pandas converts the strings
back to the dtype of the column, so for a PeriodIndex or a
DatetimeIndex the dates stay dates, and the sparsified (blanked) repeated
dates were shown as "NaT" in every multivariate table grouped by impact
date. For integer dates (data given as a list or an ndarray), pandas 3
raised "TypeError: Invalid value '...' for dtype 'int64'" (a FutureWarning
on pandas 2.x), also for univariate models, so summary of such news raised.

Changes

  • The formatted columns are assigned column by column, which replaces
    each column. Series.map exists in every supported pandas version, so the
    pandas < 2.1 fallback of the multivariate block (which made the same
    block assignment with applymap) is not needed any more.

Not changed

  • The text of every table that did not show NaT; the first column of the
    univariate table is a column of str instead of dates, with the same
    text. The unreachable fallback around Series.map of the univariate block
    is left for a separate pull request.

Tests

  • For univariate and multivariate models with a PeriodIndex, a
    DatetimeIndex and integer dates, with and without sparsifying, every
    cell of summary_impacts is that of the impacts DataFrame (the repeated
    dates blank), and summary shows the same table. On the base, the
    multivariate sparsified cases with dates fail, and the integer cases
    fail on pandas 3 (warn on pandas 2.3).

BUG: Allow a single impact date and variable in NewsResults.summary_impacts

summary_impacts selects the rows of the impacts table with
impacts.loc[(impact_date, impacted_variable), :]. If both are single
labels, this is a complete key of the MultiIndex, and pandas returns the
row as a Series, so the table code raised "AttributeError: 'Index' object
has no attribute 'remove_unused_levels'", for example for
news.summary_impacts(impact_date="2009Q3", impacted_variable="unemp") or
news.summary(impact_date="2009Q3", impacted_variable="unemp"). Univariate
models whose series has a one-letter name select their variable by
default, so any single impact date raised for them, also a partial date
string such as "2009".

Changes

  • The rows are selected by their positions from MultiIndex.get_locs,
    which is what .loc uses for every key that is not a complete key, so a
    single impact date and variable give a table with one row (or the rows
    of a partial date string).

Not changed

  • Every other selection selects the same rows, and a label that is not in
    the table still raises a KeyError (with the label alone as the message
    instead of the tuple).

Tests

  • For a single impact date given as a string, as a Period and as a
    partial date string, with a single impacted variable of a multivariate
    model, and with and without the variable of a univariate model, the
    table shows the rows of the impacts DataFrame, summary shows the same
    table, and a date without impacts raises a KeyError. All cases fail on
    the base.

BUG: Sparsify NewsResults.summary_impacts for every groupby spelling

summary_impacts accepts groupby="impact_date" and "impacted_variable" as
spellings of "impact date" and "impacted variable" (as summary_details
does), but it sparsifies the first column only if groupby is the name of
a column, so with the underscore spellings the repeated impact dates (or
impacted variables) were not blanked. The title joins a single impacted
variable or impact date that was moved out of the table with "on" if
groupby == "date", which no valid groupby is, so it was always "for".

Changes

  • groupby is normalised as in summary_details (lower case, underscores
    replaced by spaces) before it is used, so every spelling gives the same
    table.
  • The title says "Impacts on [impacted variable = ...]" when grouping by
    impact date (the default, so for example in the impacts table of every
    univariate model) and "Impacts for [impact date = ...]" when grouping by
    impacted variable.

Not changed

  • The tables for groupby="impact date" and "impacted variable" apart from
    the title, and the error for an invalid groupby.

Tests

  • For each spelling of both groupings, every cell of the table is that of
    the impacts DataFrame with the first column sparsified, and the title of
    a table with a single impacted variable or impact date. The underscore
    spellings and the title with "on" fail on the base.

BUG: Allow multiple NewsResults details tables by impact date or variable

summary_details(multiple_tables=True) makes one table for each value of
the groupby key, which it looks up in a dict of the column names that
only has the update (revision) date and variable. With
groupby="impact date" or "impacted variable" it raised a KeyError, and so
did summary(details_groupby="impact date") (or "impacted variable") for
every multivariate model, also with the default include_details_tables
(summary makes the details tables even if it does not show them), and for
univariate models whose series name is longer than one character.

Changes

  • The dicts of column names of the news and revisions details include the
    impact date and the impacted variable, so there is one table for each
    impact date or impacted variable.

Not changed

  • The tables of the other groupings and of a single table.

Tests

  • For the news and the revisions, grouped by impact date and by impacted
    variable, there is a table for each key, whose cells are those of the
    details DataFrame for that key, and summary shows these tables. All
    cases fail on the base.

BUG: Apply the univariate defaults of NewsResults to every univariate model

NewsResults set k_endog = len(self.endog_names), and endog_names of a
univariate model is the name of the series (a str), so k_endog was the
length of the name. The documented univariate defaults applied only to
series with a one-letter name. For a series named "infl" (k_endog = 4),
summary showed no details tables by default, include_details_tables=True
gave one details table per update date, summary_details with
multiple_tables=True kept the variable columns, and
summary(details_groupby="impact date") raised a KeyError.

Where they applied, the defaults selected the rows of the tables by the
name of the variable, so for one-letter names summary raised a KeyError
when there were no new observations or no impacts above the tolerance (an
empty table), and with include_revisions_tables=True when all revisions
were grouped (the default revisions_details_start=False); with some
revisions grouped, the "all prior revisions" rows were silently dropped
from the details of the revisions. Fixing k_endog alone would have made
these errors happen for every univariate model.

Changes

  • k_endog is the number of variables of the model.
  • The single variable of a univariate model no longer selects the rows of
    the tables: summary_impacts used it only to select the rows (a no-op
    otherwise), and summary_details treats it as selected, so that it is
    moved to the title of multiple tables.
  • summary leaves out a single details table without rows, as it does for
    multiple tables, so the summary of a univariate model without news (or
    without impacts above the tolerance) gains no empty table.
  • versionchanged in the docstring of summary.

Results change for valid input of univariate models whose series name is
longer than one character: summary shows the details tables by default,
as documented, and a single details table for any details_groupby;
summary_details(multiple_tables=True) shows the variable in the title;
k_endog is 1. For one-letter names, the details of the revisions include
the grouped revisions.

Not changed

  • Multivariate models, and the tables of univariate models apart from the
    above.

Tests

  • For a series named "infl" and "y", k_endog is 1, summary shows a single
    details table of the news and of the revisions by default for each
    grouping (also a table with a single row), and multiple tables show the
    variable in the title.

  • For both names, with revisions only (all grouped), with some revisions
    grouped, and with no impact above the tolerance, summary shows the
    expected tables (no empty details tables), and the details of the
    revisions include the grouped revisions and sum to the impacts of the
    revisions. These cases fail on the base for "y" (and with k_endog fixed
    alone, also for "infl").

  • A multivariate model with news for a single variable keeps the updated
    variable in a column of multiple tables unless it is selected.

  • The univariate tests of TST: Cover NewsResults and the state space news entry points #11078 use the name of the series ("infl")
    instead of "y", which they used because of this bug, and
    test_summary_univariate runs with both names.

  • tests added / passed.

  • code/documentation is well formatted.

  • properly formatted commit message. See
    NumPy's guide.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code (Claude Opus 5.5).
    Used for: finding the bug in a contract audit, writing the fix and its tests, and checking them (tests failing on main, mutation check).
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

🤖 Generated with Claude Code

Add tests of the paths of statespace/news.py that the TSA test suite did
not reach, each checked against an independent value:

- closed forms for an AR(1) model with a revision and an update: the
  weights, revisions, news and impacts of revision_details_by_impact
  (with all revisions detailed) and details_by_impact, and the estimates
  of the impacts table; with two updates, the details table of each
  update date;
- the details tables summed over the revisions or updates give the
  revision and update impacts, and these add up to the change in the
  estimates, with detailed and with grouped revisions;
- impacted_variable (str, list, array and slice, given to news or set on
  the results) and tolerance (set on the results) select the rows of
  every details table and of the impacts table;
- summary_impacts, summary_details, summary_news and summary_revisions:
  every cell of the tables against the DataFrames they show (the AR(1)
  closed forms for the univariate details), with and without sparsifying,
  with each groupby (and its underscore spelling), the impact and update
  date and variable selections, multiple tables, the univariate defaults,
  show_revisions_columns, the weights shown as 1.0 and 0, equal values of
  different variables (not blanked), and the errors for an invalid
  groupby;
- summary: the header (samples, revisions, new datapoints, included
  states) and its tables against the individual summary methods with
  every argument (including the revision date and revised variable of
  the details of revisions), for univariate and multivariate models;
- state_index: in a VAR(1) the impacts of the news and the detailed
  impacts of the revisions through the first state are those of the
  first variable;
- get_details and get_impacts with each include flag (summing to the
  matching impacts), and get_impacts with a groupby of the updated
  variables.

Two bugs of NewsResults, not fixed here, shape the tests: the univariate
summaries are tested with a one-letter series name (k_endog is the length
of the name of the series of a univariate model), and
test_get_impacts_groupby ignores the two FutureWarnings that pandas < 3
raises in get_impacts with a groupby (stack without future_stack).

Coverage of news.py by the TSA tests goes from 85.6% to 97.0% of the
lines and from 63.9% to 98.3% of the branches.
Fix the docstrings of statespace/news.py where they disagree with the
code. No code changes.

- get_impacts: `groupby` groups the updated (or revised) variables
  separately for each update date, for example with a dict or a function
  of the variable names; it is not applied to the combined details table
  and cannot group the update dates, which the docstring gave as its
  example (that raises a KeyError). The index of the result has the
  levels `update date` and `updated variable` (or the groups).
- get_details also renames the index levels of the revision details, and
  both get_details and get_impacts return an empty table if both include
  flags are False.
- summary_news has a `news` column; the columns of data_updates,
  data_revisions, details_by_impact and revision_details_by_impact and
  the index levels of details_by_update and revision_details_by_update
  are listed in their actual order; the details tables and impacts only
  include the rows above `tolerance` and of the selected
  `impacted_variable`.
- impacted_variable and tolerance (settable properties) get docstrings.
- summary_details: `updated_variable` are the variables that generated
  the news, not those affected by it, and with `source="revisions"`,
  `update_date`, `updated_variable` and `groupby` refer to the revision
  dates and revised variables.
- summary: the news table is listed, the revisions tables are shown only
  with `include_revisions_tables=True` (default False) and only if the
  data were revised, and `revision_date` and `revised_variable` apply to
  the details tables of revisions.
- NewsResults takes the results of SmootherResults.news (there is no
  KalmanSmoother.news).
summary_impacts (and the impacts table of summary) formats the impact
dates and impacted variables as strings and writes them back with a
block assignment (impacts.iloc[:, :2] = ...). pandas converts the strings
back to the dtype of the column, so for a PeriodIndex or a
DatetimeIndex the dates stay dates, and the sparsified (blanked) repeated
dates were shown as "NaT" in every multivariate table grouped by impact
date. For integer dates (data given as a list or an ndarray), pandas 3
raised "TypeError: Invalid value '...' for dtype 'int64'" (a FutureWarning
on pandas 2.x), also for univariate models, so summary of such news raised.

Changes
- The formatted columns are assigned column by column, which replaces
  each column. Series.map exists in every supported pandas version, so the
  pandas < 2.1 fallback of the multivariate block (which made the same
  block assignment with applymap) is not needed any more.

Not changed
- The text of every table that did not show NaT; the first column of the
  univariate table is a column of str instead of dates, with the same
  text. The unreachable fallback around Series.map of the univariate block
  is left for a separate pull request.

Tests
- For univariate and multivariate models with a PeriodIndex, a
  DatetimeIndex and integer dates, with and without sparsifying, every
  cell of summary_impacts is that of the impacts DataFrame (the repeated
  dates blank), and summary shows the same table. On the base, the
  multivariate sparsified cases with dates fail, and the integer cases
  fail on pandas 3 (warn on pandas 2.3).
…mpacts

summary_impacts selects the rows of the impacts table with
impacts.loc[(impact_date, impacted_variable), :]. If both are single
labels, this is a complete key of the MultiIndex, and pandas returns the
row as a Series, so the table code raised "AttributeError: 'Index' object
has no attribute 'remove_unused_levels'", for example for
news.summary_impacts(impact_date="2009Q3", impacted_variable="unemp") or
news.summary(impact_date="2009Q3", impacted_variable="unemp"). Univariate
models whose series has a one-letter name select their variable by
default, so any single impact date raised for them, also a partial date
string such as "2009".

Changes
- The rows are selected by their positions from MultiIndex.get_locs,
  which is what .loc uses for every key that is not a complete key, so a
  single impact date and variable give a table with one row (or the rows
  of a partial date string).

Not changed
- Every other selection selects the same rows, and a label that is not in
  the table still raises a KeyError (with the label alone as the message
  instead of the tuple).

Tests
- For a single impact date given as a string, as a Period and as a
  partial date string, with a single impacted variable of a multivariate
  model, and with and without the variable of a univariate model, the
  table shows the rows of the impacts DataFrame, summary shows the same
  table, and a date without impacts raises a KeyError. All cases fail on
  the base.
summary_impacts accepts groupby="impact_date" and "impacted_variable" as
spellings of "impact date" and "impacted variable" (as summary_details
does), but it sparsifies the first column only if groupby is the name of
a column, so with the underscore spellings the repeated impact dates (or
impacted variables) were not blanked. The title joins a single impacted
variable or impact date that was moved out of the table with "on" if
groupby == "date", which no valid groupby is, so it was always "for".

Changes
- groupby is normalised as in summary_details (lower case, underscores
  replaced by spaces) before it is used, so every spelling gives the same
  table.
- The title says "Impacts on [impacted variable = ...]" when grouping by
  impact date (the default, so for example in the impacts table of every
  univariate model) and "Impacts for [impact date = ...]" when grouping by
  impacted variable.

Not changed
- The tables for groupby="impact date" and "impacted variable" apart from
  the title, and the error for an invalid groupby.

Tests
- For each spelling of both groupings, every cell of the table is that of
  the impacts DataFrame with the first column sparsified, and the title of
  a table with a single impacted variable or impact date. The underscore
  spellings and the title with "on" fail on the base.
…able

summary_details(multiple_tables=True) makes one table for each value of
the groupby key, which it looks up in a dict of the column names that
only has the update (revision) date and variable. With
groupby="impact date" or "impacted variable" it raised a KeyError, and so
did summary(details_groupby="impact date") (or "impacted variable") for
every multivariate model, also with the default include_details_tables
(summary makes the details tables even if it does not show them), and for
univariate models whose series name is longer than one character.

Changes
- The dicts of column names of the news and revisions details include the
  impact date and the impacted variable, so there is one table for each
  impact date or impacted variable.

Not changed
- The tables of the other groupings and of a single table.

Tests
- For the news and the revisions, grouped by impact date and by impacted
  variable, there is a table for each key, whose cells are those of the
  details DataFrame for that key, and summary shows these tables. All
  cases fail on the base.
… model

NewsResults set k_endog = len(self.endog_names), and endog_names of a
univariate model is the name of the series (a str), so k_endog was the
length of the name. The documented univariate defaults applied only to
series with a one-letter name. For a series named "infl" (k_endog = 4),
summary showed no details tables by default, include_details_tables=True
gave one details table per update date, summary_details with
multiple_tables=True kept the variable columns, and
summary(details_groupby="impact date") raised a KeyError.

Where they applied, the defaults selected the rows of the tables by the
name of the variable, so for one-letter names summary raised a KeyError
when there were no new observations or no impacts above the tolerance (an
empty table), and with include_revisions_tables=True when all revisions
were grouped (the default revisions_details_start=False); with some
revisions grouped, the "all prior revisions" rows were silently dropped
from the details of the revisions. Fixing k_endog alone would have made
these errors happen for every univariate model.

Changes
- k_endog is the number of variables of the model.
- The single variable of a univariate model no longer selects the rows of
  the tables: summary_impacts used it only to select the rows (a no-op
  otherwise), and summary_details treats it as selected, so that it is
  moved to the title of multiple tables.
- summary leaves out a single details table without rows, as it does for
  multiple tables, so the summary of a univariate model without news (or
  without impacts above the tolerance) gains no empty table.
- versionchanged in the docstring of summary.

Results change for valid input of univariate models whose series name is
longer than one character: summary shows the details tables by default,
as documented, and a single details table for any details_groupby;
summary_details(multiple_tables=True) shows the variable in the title;
k_endog is 1. For one-letter names, the details of the revisions include
the grouped revisions.

Not changed
- Multivariate models, and the tables of univariate models apart from the
  above.

Tests
- For a series named "infl" and "y", k_endog is 1, summary shows a single
  details table of the news and of the revisions by default for each
  grouping (also a table with a single row), and multiple tables show the
  variable in the title.
- For both names, with revisions only (all grouped), with some revisions
  grouped, and with no impact above the tolerance, summary shows the
  expected tables (no empty details tables), and the details of the
  revisions include the grouped revisions and sum to the impacts of the
  revisions. These cases fail on the base for "y" (and with k_endog fixed
  alone, also for "infl").
- A multivariate model with news for a single variable keeps the updated
  variable in a column of multiple tables unless it is selected.
- The univariate tests of statsmodels#11078 use the name of the series ("infl")
  instead of "y", which they used because of this bug, and
  test_summary_univariate runs with both names.
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.

1 participant