Repository navigation
BUG/REF: fixing a few VAR problems - #4538
Conversation
…models#4537 (old test_whiteness is now plot_acorr)
Codecov Report
@@ Coverage Diff @@
## master #4538 +/- ##
==========================================
+ Coverage 80.15% 80.22% +0.06%
==========================================
Files 563 563
Lines 85238 85412 +174
Branches 9656 9672 +16
==========================================
+ Hits 68326 68525 +199
+ Misses 14685 14658 -27
- Partials 2227 2229 +2
Continue to review full report at Codecov.
|
|
This includes now part of a solution for exog, based on notes in #4535 fix and unit test for varsim/simulate_var and VARProcess.mean (maybe rename to |
|
The irf plots in |
|
ci tests are all red |
|
conflicting definitions between VARProcess and VARResults. (double assignment of attributes in both classes makes this fragile) VARResults defined k_trend as k_exog_all, i.e. changed in last commit by replacing k_trend with k_exog (meaning trend + exog_user). |
|
this should be fine now, except for updating the docstrings and maybe some cleanup, as far as it is unit tested. FEVD works, but I have no intuition to interpret plot or guess whether it's ok. There are only smoke tests for it (as pointed out by @jbrockmendel in some (?) issue) IRF has unit test, in test_var, test_var_jmulti and test_vecm. SVAR has only some basic tests for the estimation (that I had added against Stata) but not test for postestimation extras. |
+1 for addressing this. I'll give this a review later this afternoon. |
| assert_equal(res0.k_ar, 2) | ||
|
|
||
| # SMOKE test | ||
| if have_matplotlib: |
|
|
||
| obtained = results_sm[ds][dt].test_whiteness_new(nlags=lags, | ||
| obtained = results_sm[ds][dt].test_whiteness(nlags=lags, | ||
| adjusted=True) |
| coefs = results_sm[ds][dt].var_rep | ||
| intercept = np.zeros(len(sigma_u)) | ||
| # Note: _params_info k_trend, k_exog, ... is inferred with defaults | ||
| var = VARProcess(coefs, intercept, sigma_u) |
There was a problem hiding this comment.
is intercept right here? I thought it now wants exog (which is problematic for other reasons)
There was a problem hiding this comment.
It's fine
it wants now coefs_exog in this PR (not exog data as after VECM),
intercept params are a special case of coefs_exog.
| if not len(intercept) == len(ugen): | ||
| raise ValueError('1-D intercept needs to have length `steps`') | ||
| # if np.ndim(intercept) == 1: | ||
| # intercept = intercept[:, None] |
There was a problem hiding this comment.
Please delete instead of commenting-out.
| **Attributes**: | ||
| """ | ||
| def __init__(self, coefs, exog, sigma_u, names=None): | ||
| def __init__(self, coefs, coefs_exog, sigma_u, names=None, _params_info=None): |
There was a problem hiding this comment.
is _params_info the best name for an argument? private arg names seem kinda weird
There was a problem hiding this comment.
VARProcess is mostly an internal class.
private _params_info is weird but the best I managed to come up with for this not so fast quickfix.
(It might become public, official but only after seeing how it works for a while. I still don't like several parts in the separation and duplication between process and results class.)
| .. math:: \mu = (I - A_1 - \dots - A_p)^{-1} \alpha | ||
| """ | ||
| return np.linalg.solve(self._char_mat, self.exog) | ||
| return np.linalg.solve(self._char_mat, self.intercept) |
There was a problem hiding this comment.
Possibly a warning that this method is only correct in the no-exog case?
There was a problem hiding this comment.
I will change the docstring to long run intercept.
I'm not sure yet about deprecating this method.
|
|
||
| # print(coefs.round(3)) | ||
| super(VARResults, self).__init__(coefs, exog, sigma_u, names=names) | ||
| #self.k_trend = self.coefs_exog.shape[1] |
| self.k_exog = self.coefs_exog.shape[1] | ||
| self.k_exog_user = k_exog_user | ||
|
|
||
| # maybe change to params class, distinguish exog_all versus exog_user |
There was a problem hiding this comment.
Can you add "TODO: " to the beginning of this comment?
There was a problem hiding this comment.
BTW VARParams class here isn't quite what I think you're referring to (which I think is more analogous to MixedLMParams), but may still be useful.
There was a problem hiding this comment.
similar but more focused on just holding the parameters and parameter info.
i.e. the meaning and characteristics in terms of the stochastic process, like stationarity, stay in the Process class.
The parameterization should be defined by the model class, so we don't have to fiddle with the values for k_exog, k_trend, const_idx, k_endog, k_ar, ... in the results and process classes.
| test="inst", method="wald") | ||
|
|
||
| def test_whiteness_new(self, nlags=10, signif=0.05, adjusted=False): | ||
| def test_whiteness(self, nlags=10, signif=0.05, adjusted=False): |
|
all green, finally @jbrockmendel |
|
I just remembered: |
|
I think simulate_var has a bug if exog is not None. It uses self.exog which does not (yet) include trend. I included a unit test for offset but not for exog already in VAR. |
| data = np.diff(np.log(data), axis=0) * 400 | ||
| cls.res0 = sm.tsa.VAR(data).fit(maxlags=2) | ||
|
|
||
| def test_process(self): |
There was a problem hiding this comment.
This should be split into method-specific tests.
| def test_process(self): | ||
| res0 = self.res0 | ||
| k_ar = res0.k_ar | ||
| fc20 = res0.forecast(res0.endog[-k_ar:], 20) |
There was a problem hiding this comment.
IIRC this should return a DataFrame. Assert that?
There was a problem hiding this comment.
The unit tests that I added use only numpy arrays. So, in this test class there are no pandas objects.
There was a problem hiding this comment.
Yep, my bad. I missed the data = mdata.values line in setup_class. Carry on.
| self.params = params | ||
| self.exog = exog | ||
| # print(params.shape) | ||
| # print(params.round(3)) |
| observations to start the autoregressive process. | ||
| If offset is not None, then exog of the model are used if they were | ||
| provided in the model | ||
| offset : None or ndarray (steps, neqs) |
There was a problem hiding this comment.
I'm not sure I understand what this represents? It looks a little bit like optional realizations for shocks(?)
There was a problem hiding this comment.
Not for shocks directly, those are still added with random draws.
The term offset is in analogy to GLM.
There was a problem hiding this comment.
OK. Consider fleshing out the docstring. I'm more-than-averagely familiar with this code and still don't find this arg obvious.
| y = self.simulate_var(steps=steps, offset=offset, seed=seed) | ||
| return plotting.plot_mts(y) | ||
|
|
||
| def intercept_longrun(self): |
There was a problem hiding this comment.
Points for explicitness.
| r"""Long run intercept of stable VAR process | ||
|
|
||
| Warning: trend and exog except for intercept are ignored for this. | ||
| This might change in future versions. |
There was a problem hiding this comment.
Not sure how I feel about this, will give it some thought...
There seem to be a lot of methods/attributes that are conceptually (and implementation-ally) cleaner when restricting to no-trend no-exog cases. Part of me wants to separate that (common) nice case from the VARX model/process this is turning into.
There was a problem hiding this comment.
Splitting out any of the constant-only cases will make it more difficult to add more trend/exog support later.
E.g. IRF only depends on the estimated lag polynomial, but simulated confidence intervals might require support for exog,
currently irf_errband_mc is only for intercept case (trend in ['nc', 'c']), AFAIU.
same for FEVD if/when we get simulated or bootstrap standard errors or confidence intervals.
There was a problem hiding this comment.
Splitting out any of the constant-only cases will make it more difficult to add more trend/exog support later
False. Drawing the lines between classes such that they are internally coherent and represent meaningful abstractions (e.g, VARProcess could represent a "VAR Process") makes things easier to understand and therefore harder to screw up.
If you want to make it easier to do X later, we should be talking about improving internal consistency across the board.
|
problem with matplotlib 1.4.3 on Travis I'm currently using 1.5.3, and don't have any failures/errors, also travis is green with matplotlib 1.5. |
|
all green, I don't remember enough of the VECM implementation to make changes there, |
|
There are some spots I haven't reviewed yet. Mind holding off for a few hours? |
| import matplotlib | ||
| have_matplotlib = True | ||
| import matplotlib.pyplot as plt | ||
| plt.switch_backend('Agg') # otherwise segfault on windows |
There was a problem hiding this comment.
Good to know about the windows segfault. Also prevents exceptions (I forget which kind) on headless Linux.
| import pytest | ||
|
|
||
| have_matplotlib = False | ||
| try: |
There was a problem hiding this comment.
This block is pretty common, may merit putting in something like sm.tools._testing, especially since it has now grown past 2-3 lines.
There was a problem hiding this comment.
(not necessarily for this PR)
| k_ex = self.coefs_exog.shape[0] if self.coefs_exog.ndim != 1 else 1 | ||
| k_c = k_ex - self.k_exog_user | ||
| else: | ||
| k_c = 0 |
There was a problem hiding this comment.
Why not just call this k_trend?
There was a problem hiding this comment.
because this is just a temporary variable, and I didn't want to confuse it with the provided k_trend, if provided.
| k_c = k_ex - self.k_exog_user | ||
| else: | ||
| k_c = 0 | ||
| self.k_trend = _params_info.get('k_trend', k_c) |
There was a problem hiding this comment.
It looks like "k_trend" and "k_exog_user" are the only keys that are relevant here. Why not just make those optional kwargs to __init__? It's clearer and doesn't risk silently ignoring anything.
There was a problem hiding this comment.
because adding k_trend explicitly is not future proof. We would have to change the signature again if or when we add seasonal, k_seasonal or other deterministic or exog with explicit built in support.
There was a problem hiding this comment.
Nothing is future proof, and it's OK to change the signature when behavior changes. Better to do that than to de-facto change the signature in an opaque and error-prone way.
| self.k_exog_user = _params_info.get('k_exog_user', 0) | ||
| if self.coefs_exog is not None: | ||
| k_ex = self.coefs_exog.shape[0] if self.coefs_exog.ndim != 1 else 1 | ||
| k_c = k_ex - self.k_exog_user |
There was a problem hiding this comment.
Restriction k_c >= 0?
There was a problem hiding this comment.
That has to hold, unless there is a bug somewhere else, or the class got the wrong k_exog_user in _params_info (which we control because VARProcess is mainly an internal class)
|
This isn't my favorite but seems OK-given-decisions-I-dislike. Pending small requested edits, LGTM. |
|
on last commit here, I will merge when green I added parameter uncertainty as option back into VARResults.forecast_cov, but did not add it to forecast_interval because that is more generic in VARProcess. unit test for forecast_cov with parameter uncertainty: |
There is still a fundamental difference in your and my approach to designing the code. |
[Comment about the status quo being awful omitted] This isn't that big a deal; carry on. Thanks for addressing the commented-out code requests. |
|
Some but not all occurrences of |
|
Most likely Checking again, I see that it shows up in unit tests, but only one with default trend. |
WIP
trying to get a few fixes into 0.9
plot now return fig, rename test_whiteness methods closes #4537
(old test_whiteness is now plot_acorr)
I'm still thinking about #4535