Repository navigation
WIP: Automatic forecasting GSoC-2018-Project - #4621
abhijeetpanda12 wants to merge 49 commits into
Conversation
|
Thanks for setting up this PR! I don't see a function definition yet, though, so maybe you didn't commit it before pushing? Below is a copy of what I e-mailed to you yesterday, for reference:
|
|
When you get a chance, I think it would be great if you could send an update to the mailing list giving with the link to this PR and a status update of what you're planning for this week (i.e. the first week of your proposal). |
|
Hopefully the basic non-stepwise version of this function is straightforward and can be pushed pretty today or tomorrow, since we should get ARMA p and q selection done this week. But if you're having trouble figuring out how to get started, that's no problem at all. A couple of good ways to get help if you're stuck:
|
|
I have added the function definition now. |
|
This pull request introduces 1 alert when merging adfce60 into f790b88 - view on lgtm.com new alerts:
Comment posted by lgtm.com |
Codecov Report
@@ Coverage Diff @@
## master #4621 +/- ##
==========================================
- Coverage 81.75% 81.09% -0.66%
==========================================
Files 569 592 +23
Lines 89851 90470 +619
Branches 10022 10120 +98
==========================================
- Hits 73455 73367 -88
- Misses 14126 14799 +673
- Partials 2270 2304 +34
Continue to review full report at Codecov.
|
|
@ChadFulton I have made a GitHub gist for the non-stepwise model selection for SARIMAX at |
|
Great! As I mentioned on the gist, please go ahead and add that code as commit on this PR. In the meantime, here are some comments: First, rather than putting any datasets in the Then, with the test separated from the code, you should modify the function to use whatever Finally, you haven't made use of the
So you would want to do e.g.: mod = sm.tsa.statespace.SARIMAX(endog, order=(p, d, q), **spec)(we may expand |
For examples of these tests, see e.g. the tests in To run your test, from the command line: cd /path/to/statsmodels/tsa/automatic
# To run all tests:
pytest tests
# To run only a certain file:
pytest tests/test_sarimax.py
# To run only a certain test:
pytest tests/test_sarimax.py::test_non_stepwise |
|
@ChadFulton thanks for your comments and reviews. I have made a few necessary changes like separating the test file from the original sarimax.py file but I haven't done the unit test for it, which I'll be completing today and committing it. |
Yes, you can use any of the variables in the macrodata dataset. For example, I used the following in one of my test files: current_path = os.path.dirname(os.path.abspath(__file__))
macrodata = datasets.macrodata.load_pandas().data
macrodata.index = pd.PeriodIndex(start='1959Q1', end='2009Q3', freq='Q')For tests in which |
Please carry on with the stepwise version. We can always come back and clean things up here later, but it's more important at this stage to keep things moving. By the way, you should in general keep moving on the project as you see fit / as the timeline suggests - you don't need to check in with me first. Since we are on essentially opposite time zones, you'll need to be able to keep working on things even if you don't hear back from me for several hours. |
| data = pd.read_stata(BytesIO(wpi1)) | ||
| data.index = data.t | ||
|
|
||
| def auto_order(endog, criteria='aic', d=0, max_order=(3, 3), spec=None): |
There was a problem hiding this comment.
You don't need to (and shouldn't) replicate the function to be tested in the unit test. Instead, you should import the function you want to test, e.g.:
from statsmodels.tsa.automatic import sarimaxand then in your test_non_stepwise function you should do something like:
p, q = sarimax.auto_order(...) # where you fill in the appropriate parameters for your test
desired_p = ... # where you fill in whatever you think the returned `p` should be equal to
desired_q = ... # similarly for what the returned `q` should be
assert_equal(p, desired_p)
assert_equal(q, desired_q)In general, you'll want to test out various different types of parameterizations (e.g. different max_order, different spec, etc).
| aic_matrix = np.zeros(p, q) | ||
| for p in range(max_order[0]): | ||
| for q in range(max_order[1]): | ||
| if p == 0 and q == 0: |
There was a problem hiding this comment.
You don't need to skip this case, since if exog is set in spec, then linear regression is the special case p=0, q=0.
There was a problem hiding this comment.
Yaa, I agree but if we skip this case, then the SARIMAX model fails as one of the states should be positive.
I think we should put a check for this
There was a problem hiding this comment.
If this is true, can you file a bug report with code to replicate? When I try, it seems to work:
import pandas as pd
import statsmodels.api as sm
dta = sm.datasets.macrodata.load_pandas().data
dta.index = pd.PeriodIndex(start='1959Q1', end='2009Q3', freq='Q')
mod = sm.tsa.SARIMAX(dta['infl'], order=(0, 0, 0))
res = mod.fit()
print(res.summary()) Statespace Model Results
==============================================================================
Dep. Variable: infl No. Observations: 203
Model: SARIMAX Log Likelihood -619.610
Date: Thu, 17 May 2018 AIC 1241.220
Time: 07:51:27 BIC 1244.533
Sample: 03-31-1959 HQIC 1242.560
- 09-30-2009
Covariance Type: opg
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
sigma2 26.2234 2.466 10.632 0.000 21.389 31.058
===================================================================================
Ljung-Box (Q): 712.11 Jarque-Bera (JB): 60.66
Prob(Q): 0.00 Prob(JB): 0.00
Heteroskedasticity (H): 0.45 Skew: 0.74
Prob(H) (two-sided): 0.00 Kurtosis: 5.23
===================================================================================
There was a problem hiding this comment.
In that case, I'll go check this code again. I'll take this into account and edit the code for necessary changes.
There was a problem hiding this comment.
It throws a ValueError for the number of states to be positive which I have pasted in
https://gist.github.com/abhijeetpanda12/e2d2c14744c84b368910842a4ecfe0f7
There was a problem hiding this comment.
Can you do:
import statsmodels.api as sm.
print(sm.version.version)You seem to be using an old version, whereas you should be developing against master.
There was a problem hiding this comment.
I was using version 0.8. I have now rebased my local repository to the current master running version 0.10
Thanks for helping me out.
b4a20f7 to
201f14c
Compare
|
@ChadFulton I have written the stepwise algorithm now without taking the seasonal components into account. Please review the code and provide me some feedback. |
|
This pull request introduces 1 alert when merging 201f14c into 6611e87 - view on lgtm.com new alerts:
Comment posted by lgtm.com |
| model = int(np.where(aic_vals == min_aic)[0]) | ||
| p, q = order_init[model][0], order_init[model][1] | ||
| # only p varies by +1 | ||
| if(p+1 <= max_order[0]): |
There was a problem hiding this comment.
In Statsmodels, we put spaced between mathematical operators other than exponentiation, so this should be p + 1.
Also, no need for the parentheses, so maybe:
if p + 1 <= max_order[0]:(and similar in the rest of the function).
There was a problem hiding this comment.
I have the made the changes.
|
|
||
| def auto_order( | ||
| endog, criteria='aic', d=0, max_order=(3, 3), | ||
| stepwise=False, **spec): |
There was a problem hiding this comment.
At some point we need to add a docstring here, giving at least the Parameters and the Return sections. It would also be good to have a Notes section describing the basics of the stepwise approach and giving the citation.
There was a problem hiding this comment.
I have added a simple docstring with the citation of Hyndman's paper in the Notes section. I'll add more details to it after completing the function.
| def auto_order(endog, criteria='aic', d=0, max_order=(3, 3), **spec=None): | ||
|
|
||
| def auto_order( | ||
| endog, criteria='aic', d=0, max_order=(3, 3), |
There was a problem hiding this comment.
The first line arguments should be on the same line as the def part.
There was a problem hiding this comment.
I followed the E127 convention along with the pycodestyle suggestions, so had to take it to the new line.
I'll make the desired changes.
| p += 1 | ||
| min_aic = res.aic | ||
| # only p varies by -1 | ||
| if(p-1 >= 0): |
There was a problem hiding this comment.
If the previous case (p+1) improved the aic, then this will test the original p value again.
E.g. if the order_init was minimized with p=1, q=0 and then p+1=2 was better than p = 1, this section will test p = 1 again.
There was a problem hiding this comment.
Yes, I understand that. It should be checking for p=0. I'll add a new variable that'll keep track of the changes in p and finally assign it in the end.
| q += 1 | ||
| min_aic = res.aic | ||
| # only q varies by -1 | ||
| if(q-1 >= 0): |
There was a problem hiding this comment.
Similarly to the p-1 case, if q+1 improved the aic, this will test the original q again.
| q += 1 | ||
| min_aic = res.aic | ||
| # both p and q vary by -1 | ||
| if(q-1 >= 0) and (q-1 >= 0): |
There was a problem hiding this comment.
See my notes on the p-1 and q-1 cases above, similar issue here.
| desired_q = 2 | ||
| assert_equal(p, desired_p) | ||
| assert_equal(q, desired_q) | ||
| assert p == desired_p |
There was a problem hiding this comment.
We use the numpy testing functions (regular assert calls can disappear at compile time), so something like:
from numpy.testing import assert_equaland then later:
assert_equal(p, desired_p)There was a problem hiding this comment.
Sure, I'll use the numpy testing functions.
Looks like a great start, I've added a few comments. |
I'll write some more thoughts on this in response to your question on the mailing list, and maybe we can get others' input as well. |
201f14c to
bca1cec
Compare
|
@ChadFulton I have made all the changes that you have reviewed. Please let me know if some other changes are also required. |
a852ff8 to
a007dd7
Compare
|
@abhijeetpanda12 - it would be great to get some of your hard work merged in to Statsmodels. Do you think you'll have some time to work on this in the coming month? |
|
@ChadFulton I would definitely like to see this PR merged and I have recently started working on it. I will be able to take out some time and complete it by the end next month. |
|
@abhijeetpanda12 how are things? Is there anything you're stuck on, or that we can help with? |
|
I am having an issue with the Result classes. I want to know which all values to post in the result summary. I am taking reference from your PR #5151 . Can you brief me about what things I should consider for it? |
|
There is no specific list of attributes that In this case, I think that |
@ChadFulton I have only pushed the function definition. We can discuss the improvements/ additions over here.