Repository: statsmodels/statsmodels

Issue:
Whiteness test of a VAR model returns p-value nan
I noticed sometimes the `test_whiteness` function in `var_model.py` returns `nan` as the p-value. After some searching and digging in the code I realized this happens due to `df` equals to 0, and then the chi square test receives this `df` and returns `nan`.

Searching the web showed that the `df` should not be 0 when checking for a chi square test.

In order to reproduce this follow the next steps.
1. Create a VAR model with the following data:
```python
[[-1.7351859419697604, -4.6097261953412385], [-2.7444152637216206, 0.36803839099457036], [0.4661153045879871, -2.3240113911468114], [2.8704536166734727, 0.6098348718318576], [-2.135355388696624, -1.39782506791029], [0.11741391841555071, 4.133510766269701], [-1.6372549019607732, -1.8415295888375525], [3.34502064686383, 3.261136085002043], [2.755918645008336, 2.013894564773196], [-1.26793395836782, -3.487127094401316], [1.857762640351865, -1.4213880007799276], [-7.036388928953954, -2.9863970254223204], [6.029832447895373, 1.6991005723630366], [-2.4399224307568943, 3.067525211229224], [-1.356625663347927, 1.8811229362805193], [0.6919301946360861, 2.928405738286969], [2.9861743805273164, -2.9690408032129056], [2.571078431372541, -5.708114897645281], [-3.8067810457516345, 4.157504326904196], [5.757761437908499, -0.4721753526500123], [-10.318627450980387, -2.627663790427377], [0.9644604504381675, 0.08773962024166337], [5.405803024111165, 3.067421349336243], [-3.569201383046078, -2.2172850166845706], [0.24308777454587016, 1.653825434591667], [1.3623043823168786, -1.9183571519984923], [-4.901552287581694, -0.33519611851281894], [3.4775396911250454, 2.8640967931921395], [2.6911694592017454, -1.6223947691050284], [-4.8473357863177995, -4.312219043727005], [9.456125460749078, 11.40489547666457], [-3.9840621168778085, -5.346511561800327], [2.3105843890478184, 1.3517382413087944], [-3.813171423725464, 0.10388548057258618], [7.0750476161834825, -1.493413059508697], [-3.680572597137015, -3.8675388389206873], [1.0473324002962983, -0.794140025865886], [-7.978860760635698, -0.8906514704546353], [1.9927610059341418, -1.1513936195650132], [0.4562551103843049, 2.7444289280392553], [2.459851885316212, -1.1460112890000005], [0.7450154050177531, 6.089052287581694], [3.9399049055666353, 2.1113112695016696], [-6.813270918638537, -2.1018486807851957], [1.4262664233381628, 4.769260777162998], [-1.3306781880440468, -5.0454717318925475], [2.438653815309351, 3.1072051742474116], [-1.0744652063417135, -5.272324768184276], [-0.9429543275745118, -2.623555547826683], [-2.6279792390899104, -1.4445764922233764], [11.625694498417587, 4.707826596896425], [-3.8515475799364935, -1.6347450952851474], [1.1326015477765878, 1.8021259198691766], [-4.14444795300534, -1.8994276369583005], [-7.1803330275033375, -2.4753151508159092], [3.619192456529788, 4.157623601587872], [2.460496959081169, 4.900096757133596], [0.5361667347772823, 1.030241111565175], [-4.772056764971595, -9.934396115055364], [6.111247610905394, 5.408854635692876], [0.9328023710505846, -2.036332337048691], [-0.3496053981690892, -3.063575730346699], [-2.762191660253791, 2.2830913484548745], [-1.273785445224533, -2.6717599118463937], [1.8659391027643721, 1.1408805662431263], [0.9713935431140186, -1.7441918280604725], [6.121779269533718, 5.392354597876228], [-1.8880718954248294, 0.977424590191859], [1.3047385620915009, -0.07110747854515864], [2.4052287581699403, 0.7723743359215405], [-7.285095647689445, -3.4023368951176707], [6.596778654225389, 3.2075163398692865], [1.482380007217614, 1.338643790849673], [-2.3437859304369226, -3.1307189542483655], [-4.265636560440825, 4.314133986928098], [-4.955450484920846, -6.701982853487394], [1.1355975437443817, -2.3705145401530885], [0.9421036880274016, 1.9182004089979543], [-1.8490245749916028, 1.6417177914110397], [6.315663351393098, 2.5794213498270295], [-6.863506334286882, -4.5496852938504375], [6.7960768287699125, 1.801470588235297], [2.2734746227874183, 6.629901960784309], [-2.142293584781683, -7.809983860619916], [3.167074940496491, 5.142956834315662], [-6.008922100283996, -2.9556961094014724], [-3.6272987331426236, -2.724213506217332], [3.9247564711758542, -0.11098819467824939], [-2.2909191193164276, 1.1388888888888857], [-0.22950571728488, 2.9647403523960776], [0.4428281529203417, 2.49250421277776], [-1.760638610436672, -2.893158341968288], [-2.7377450980392126, -1.9139524058862207], [5.179626046032084, 0.6764217435804198], [3.324070290151198, 1.6901928051133268], [1.8111973845525142, 1.9717589699004492], [-7.065145334673815, -3.0530498195736584], [0.32505542866645953, 2.3575042535744757]]
```
2. Fit the model with `maxlags=1` and `ic=None`.
3. Run a whiteness test on the results with `nlags=1`.

Of course maybe I'm missing something, but I don't know what is the meaning of this `nan` (is it lower than 0.05? higher?).

Thank you.



Discussion:

Resolving patch:
diff --git a/statsmodels/tsa/vector_ar/var_model.py b/statsmodels/tsa/vector_ar/var_model.py
--- a/statsmodels/tsa/vector_ar/var_model.py
+++ b/statsmodels/tsa/vector_ar/var_model.py
@@ -1959,12 +1959,17 @@ def test_whiteness(self, nlags=10, signif=0.05, adjusted=False):
         Parameters
         ----------
         nlags : int > 0
+            The number of lags tested must be larger than the number of lags
+            included in the VAR model.
         signif : float, between 0 and 1
+            The significance level of the test.
         adjusted : bool, default False
+            Flag indicating to apply small-sample adjustments.
 
         Returns
         -------
-        results : WhitenessTestResults
+        WhitenessTestResults
+            The test results.
 
         Notes
         -----
@@ -1975,6 +1980,10 @@ def test_whiteness(self, nlags=10, signif=0.05, adjusted=False):
         ----------
         .. [1] Lütkepohl, H. 2005. *New Introduction to Multiple Time Series Analysis*. Springer.
         """
+        if nlags - self.k_ar <= 0:
+            raise ValueError("The whiteness test can only be used when nlags "
+                             "is larger than the number of lags included in "
+                             f"the model ({self.k_ar}).")
         statistic = 0
         u = np.asarray(self.resid)
         acov_list = _compute_acov(u, nlags)


Test patch:
diff --git a/statsmodels/tsa/vector_ar/tests/test_var.py b/statsmodels/tsa/vector_ar/tests/test_var.py
--- a/statsmodels/tsa/vector_ar/tests/test_var.py
+++ b/statsmodels/tsa/vector_ar/tests/test_var.py
@@ -843,3 +843,11 @@ def test_summaries_exog(reset_randomstate):
     assert 'exog_1' in summ
     assert 'exog_2' in summ
     assert 'exog_3' in summ
+
+
+def test_whiteness_nlag(reset_randomstate):
+    # GH 6686
+    y = np.random.standard_normal((200,2))
+    res = VAR(y).fit(maxlags=1, ic=None)
+    with pytest.raises(ValueError, match="The whiteness test can only"):
+        res.test_whiteness(1)