Skip to content

BUG: Start the annual cycle of UnobservedComponents at a three-year period - #11089

Draft
bashtage wants to merge 4 commits into
statsmodels:mainfrom
bashtage:wip-uc-annual-cycle-start
Draft

bashtage wants to merge 4 commits into
statsmodels:mainfrom
bashtage:wip-uc-annual-cycle-start

Conversation

@bashtage

@bashtage bashtage commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Stacked on #11087, merge that first (this PR adds one commit). The fixed-slope PR also appends tests to test_structural.py: keep both appended blocks. No conflict in structural.py or with #10940.

BUG: Drop missing data from the start params of UnobservedComponents

UnobservedComponents.start_params drops missing observations before it
computes the starting parameters, but a model without a level used the
full series from the state space representation, NaN included, for the
remaining components. With missing data:

  • UnobservedComponents(y, "irregular", seasonal=4) (or a frequency-domain
    seasonal) had start_params [nan, nan], so fit() returned NaN parameters
    and a NaN log-likelihood with only a ConvergenceWarning;
  • "ntrend" with autoregressive=1, and a damped stochastic cycle, raised
    "LinAlgError: SVD did not converge";
  • an exog (estimated by maximum likelihood) raised "ValueError: shapes
    (1,38) and (40,) not aligned", because exog had the missing rows dropped
    and the series did not.

Changes

  • Without a level, the residual used for the other components is the
    series with the missing observations dropped, as with a level. The
    starting parameters are those of the model of the data without the
    missing observations, so fit() starts from finite values.
  • start_params has a docstring, with a versionchanged note.

Not changed

  • The starting parameters of models without missing data (identical, bit
    for bit, for int and float data, ndarray, list and Series, with seasonal,
    frequency-domain seasonal, autoregressive, cycle and exog components) and
    of models with a level.

Tests

  • Without a level, with three missing observations, for a seasonal, a
    frequency-domain seasonal, an AR(1), a damped stochastic cycle, an exog,
    and an exog with an AR(1) without irregular: the starting parameters
    equal those of the same model of the data without the missing
    observations; for the two seasonal models, every variance starts at the
    variance of the observed data and fit() gives finite parameters and
    log-likelihood. All six fail on main.

BUG: Start the annual cycle of UnobservedComponents at a three-year period

For annual data, the default bounds of the cycle period of
UnobservedComponents are 1.5 to 12 years, and the frequency of the cycle
should start at that of a three-year cycle (2 pi / 3), as it does at
twelve quarters and 36 months for quarterly and monthly data. The bounds
check the first letter of the frequency string for "A" or "Y", but the
start checked only "A", the annual frequency strings before pandas 2.2.
From pandas 2.2 they start with "Y" ("YS-JAN", "YE-DEC", "Y-DEC" for a
PeriodIndex), so the frequency started in the middle of the bounds,
2.356 instead of 2.094 for a local level with a stochastic cycle on
annual data, which changes the fit as well. The bounds were updated for the
new strings in 929a99a; the start was missed.

Changes

  • The cycle frequency starts at 2 pi / 3 for annual data with either
    first letter, as the bounds do, so starting parameters and fits of models
    with a cycle on annual data change on pandas 2.2 and later.
  • A versionchanged note in the docstring of start_params.

Not changed

  • Results on pandas before 2.2, and for quarterly, monthly and other data.
  • The bounds of the cycle frequency.

Tests

  • For annual data (year end, year start in July, year end in June, and an
    annual PeriodIndex, built with offsets so that the test runs on every
    supported pandas), the bounds are 2 pi / 12 to 2 pi / 1.5 and the start
    is 2 pi / 3. All four fail on main with pandas 3.0.5 and 2.3.3.

  • test_cycle_frequency_bounds checks the annual start too; it skipped it
    because the start depended on the pandas version.

  • 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 coverage audit of the state space models, 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

UnobservedComponents.start_params drops missing observations before it
computes the starting parameters, but a model without a level used the
full series from the state space representation, NaN included, for the
remaining components. With missing data:
- UnobservedComponents(y, "irregular", seasonal=4) (or a frequency-domain
  seasonal) had start_params [nan, nan], so fit() returned NaN parameters
  and a NaN log-likelihood with only a ConvergenceWarning;
- "ntrend" with autoregressive=1, and a damped stochastic cycle, raised
  "LinAlgError: SVD did not converge";
- an exog (estimated by maximum likelihood) raised "ValueError: shapes
  (1,38) and (40,) not aligned", because exog had the missing rows dropped
  and the series did not.

Changes
- Without a level, the residual used for the other components is the
  series with the missing observations dropped, as with a level. The
  starting parameters are those of the model of the data without the
  missing observations, so fit() starts from finite values.
- start_params has a docstring, with a versionchanged note.

Not changed
- The starting parameters of models without missing data (identical, bit
  for bit, for int and float data, ndarray, list and Series, with seasonal,
  frequency-domain seasonal, autoregressive, cycle and exog components) and
  of models with a level.

Tests
- Without a level, with three missing observations, for a seasonal, a
  frequency-domain seasonal, an AR(1), a damped stochastic cycle, an exog,
  and an exog with an AR(1) without irregular: the starting parameters
  equal those of the same model of the data without the missing
  observations; for the two seasonal models, every variance starts at the
  variance of the observed data and fit() gives finite parameters and
  log-likelihood. All six fail on main.
…eriod

For annual data, the default bounds of the cycle period of
UnobservedComponents are 1.5 to 12 years, and the frequency of the cycle
should start at that of a three-year cycle (2 pi / 3), as it does at
twelve quarters and 36 months for quarterly and monthly data. The bounds
check the first letter of the frequency string for "A" or "Y", but the
start checked only "A", the annual frequency strings before pandas 2.2.
From pandas 2.2 they start with "Y" ("YS-JAN", "YE-DEC", "Y-DEC" for a
PeriodIndex), so the frequency started in the middle of the bounds,
2.356 instead of 2.094 for a local level with a stochastic cycle on
annual data, which changes the fit as well. The bounds were updated for the
new strings in 929a99a; the start was missed.

Changes
- The cycle frequency starts at 2 pi / 3 for annual data with either
  first letter, as the bounds do, so starting parameters and fits of models
  with a cycle on annual data change on pandas 2.2 and later.
- A versionchanged note in the docstring of start_params.

Not changed
- Results on pandas before 2.2, and for quarterly, monthly and other data.
- The bounds of the cycle frequency.

Tests
- For annual data (year end, year start in July, year end in June, and an
  annual PeriodIndex, built with offsets so that the test runs on every
  supported pandas), the bounds are 2 pi / 12 to 2 pi / 1.5 and the start
  is 2 pi / 3. All four fail on main with pandas 3.0.5 and 2.3.3.
- test_cycle_frequency_bounds checks the annual start too; it skipped it
  because the start depended on the pandas version.
@bashtage
bashtage force-pushed the wip-uc-annual-cycle-start branch from 8da3bed to b137b85 Compare November 28, 2026 21:36
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