Skip to content

ENH: KPSS - detailed error message when lags > nobs - #5707

Merged
bashtage merged 1 commit into
statsmodels:masterfrom
Matthew-Amos:master
May 13, 2019
Merged

bashtage merged 1 commit into
statsmodels:masterfrom
Matthew-Amos:master

Conversation

@Matthew-Amos

Copy link
Copy Markdown
Contributor

I don't believe there are open issues for this, but the enhancement is related to #5432 & #3330, only for the KPSS test. Adds a clear error message when the lags parameter exceeds the number of observations.

  • closes #xxxx
  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    NumPy's guide.

@bashtage bashtage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few details. Also, could you squash to one since this is a single issue

Comment thread statsmodels/tsa/stattools.py Outdated
lags = int(lags)

if lags > nobs:
raise ValueError("lags (%d) must be <= number of observations (%d)" % (lags, nobs))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use modern formatting

Comment thread statsmodels/tsa/tests/test_stattools.py Outdated

def test_kpss_fails_on_nobs_check(self):
# Test that if lags exceeds nobs KPSS raises a clear error
with pytest.raises(ValueError):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use match to check the error message

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this suggestion. Also can you add “GH#5707” to the comment above?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, thank you.

jbrockmendel, to clarify, do you mean to add GH#5707 in the test comment (line 539), or to the PR checklist?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To the comment on line 539. It makes it slightly easier for a curious reader to look up the motivation for the test.

else:
lags = int(lags)

if lags > nobs:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the number of obs and the included deterministic terms? This is unlikely to bind but could create an error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow, could you please explain this a little more? My understanding is that the error that occurs when lags exceeds # obs stems from _sigma_est_kpss, particularly:

resids_prod = np.dot(resids[i:], resids[:nobs - i])

Example error with # obs = 3, lags set to 4:

ValueError: shapes (0,) and (2,) not aligned: 0 (dim 0) != 2 (dim 0)

@codecov

codecov Bot commented May 13, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #5707 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5707      +/-   ##
==========================================
+ Coverage   82.22%   82.22%   +<.01%     
==========================================
  Files         593      593              
  Lines       93711    93716       +5     
  Branches    10352    10353       +1     
==========================================
+ Hits        77053    77058       +5     
  Misses      14257    14257              
  Partials     2401     2401
Impacted Files Coverage Δ
statsmodels/tsa/tests/test_stattools.py 98.92% <100%> (ø) ⬆️
statsmodels/tsa/stattools.py 88.01% <100%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 70e9685...6fe8d4e. Read the comment docs.

1 similar comment
@codecov

codecov Bot commented May 13, 2019

Copy link
Copy Markdown

Codecov Report

Merging #5707 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5707      +/-   ##
==========================================
+ Coverage   82.22%   82.22%   +<.01%     
==========================================
  Files         593      593              
  Lines       93711    93716       +5     
  Branches    10352    10353       +1     
==========================================
+ Hits        77053    77058       +5     
  Misses      14257    14257              
  Partials     2401     2401
Impacted Files Coverage Δ
statsmodels/tsa/tests/test_stattools.py 98.92% <100%> (ø) ⬆️
statsmodels/tsa/stattools.py 88.01% <100%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 70e9685...6fe8d4e. Read the comment docs.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) to 84.796% when pulling 6fe8d4e on equinaut:master into 70e9685 on statsmodels:master.

@bashtage

Copy link
Copy Markdown
Member

Thanks. Other point isn't essential. Wondering if it crashes if run on a very short series, e.g., 1 observation.

@bashtage
bashtage merged commit e5d170e into statsmodels:master May 13, 2019
jbrockmendel added a commit to jbrockmendel/statsmodels that referenced this pull request May 13, 2019
bashtage added a commit that referenced this pull request May 13, 2019
CLN: fix invalid escapes in test_stattools introduced in #5707
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants