Repository: statsmodels/statsmodels

Issue:
BUG: sarimax fit-predict values are nan when time series length is 17
#### issue

for time series with length 17, sarimax with order=(1,1,1), seasonal_order=(0,1,0,12), and trend='n'
produces np.nan as fitted and predicted values.

#### Code Sample


```python
import random
import statsmodels.api

y = random.sample(range(100,200), 17)

sarimax_model = statsmodels.api.tsa.statespace.SARIMAX(
                                                            endog = y,
                                                            order = (1,1,1),
                                                            seasonal_order = (0,1,0,12),
                                                            trend = 'n'
                                                       ).fit()

sarimax_model.fittedvalues
sarimax_model.predict(start=len(y), end=len(y)+11)

```
#### Expected Output

above model fails for time series with length < 17 but for time series with length > 17 it behave normal.

#### Output of ``import statsmodels.api as sm; sm.show_versions()``

<details>

[paste the output of ``import statsmodels.api as sm; sm.show_versions()`` here below this line]
INSTALLED VERSIONS
------------------
Python: 3.7.1.final.0

Statsmodels
===========

Installed: 0.10.2 (\Local\Continuum\anaconda3\lib\site-packages\statsmodels)

</details>



Discussion:
I have also been coming across a number of bizarre behaviors for short time series where the predictions are `nan` at times and reasonable at others. Unfortunately can't share data, but this has been pretty painful, especially when implementing rolling CV; small initial windows can do weird things.
This is an area where we can improve.  Examples of problems and what a better solution would look like would be very useful.
In this example, it is an issue computing starting parameters that should be relatively easy to fix.

Resolving patch:
diff --git a/statsmodels/tsa/statespace/sarimax.py b/statsmodels/tsa/statespace/sarimax.py
--- a/statsmodels/tsa/statespace/sarimax.py
+++ b/statsmodels/tsa/statespace/sarimax.py
@@ -897,7 +897,10 @@ def _conditional_sum_squares(endog, k_ar, polynomial_ar, k_ma,
             params_ma = params[offset:k_params_ma + offset]
             offset += k_params_ma
         if residuals is not None:
-            params_variance = (residuals[k_params_ma:]**2).mean()
+            if len(residuals) > 1:
+                params_variance = (residuals[k_params_ma:] ** 2).mean()
+            else:
+                params_variance = np.var(endog)
 
         return (params_trend, params_ar, params_ma,
                 params_variance)


Test patch:
diff --git a/statsmodels/tsa/statespace/tests/test_sarimax.py b/statsmodels/tsa/statespace/tests/test_sarimax.py
--- a/statsmodels/tsa/statespace/tests/test_sarimax.py
+++ b/statsmodels/tsa/statespace/tests/test_sarimax.py
@@ -2750,3 +2750,16 @@ def test_plot_too_few_obs(reset_randomstate):
     results = mod.fit()
     with pytest.raises(ValueError, match="Length of endogenous"):
         results.plot_diagnostics(figsize=(30, 15))
+
+
+def test_sarimax_starting_values_few_obsevations(reset_randomstate):
+    # GH 6396, 6801
+    y = np.random.standard_normal(17)
+
+    sarimax_model = sarimax.SARIMAX(
+        endog=y, order=(1, 1, 1), seasonal_order=(0, 1, 0, 12), trend="n"
+    ).fit(disp=False)
+
+    assert np.all(
+        np.isfinite(sarimax_model.predict(start=len(y), end=len(y) + 11))
+    )