Repository: statsmodels/statsmodels

Issue:
BUG: SARIMAX starting value estimation is incorrect when the MA order is large relative to sample size 
#### Describe the bug

Thank you for the amazing package 🙂 

**Potential bug:** Fitting a `SARIMAX` under certain `order` / `endog` conditions produces a `RuntimeWarning` resulting from taking a mean from an empty slice, which ultimately leads to an 'lbfgs' convergence failure:

#### Code Sample, a copy-pastable example if possible

```python
import numpy as np
from statsmodels import api as sm

X = None
y = np.array([
    3066.3, 3260.2, 3573.7, 3423.6, 3598.5, 3802.8, 3353.4, 4026.1,
    4684. , 4099.1, 3883.1, 3801.5, 3104. , 3574. , 3397.2, 3092.9,
    3083.8, 3106.7, 2939.6
])

arima = sm.tsa.statespace.SARIMAX(
    endog=y,
    exog=X,
    order=(0, 1, 5),
    seasonal_order=(0, 0, 0, 0),
    trend=None,
)

arima_results = arima.fit(
    start_params=None,
    method="lbfgs",
    maxiter=50,
    disp=5,
)
```

Results in:

```
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/statsmodels/tsa/statespace/sarimax.py:902: RuntimeWarning: Mean of empty slice.
  params_variance = (residuals[k_params_ma:] ** 2).mean()
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/numpy/core/_methods.py:170: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/statsmodels/tsa/statespace/sarimax.py:978: UserWarning: Non-invertible starting MA parameters found. Using zeros as starting parameters.
  warn('Non-invertible starting MA parameters found.'
RUNNING THE L-BFGS-B CODE

           * * *

Machine precision = 2.220D-16
 N =            6     M =           10
 This problem is unconstrained.

At X0         0 variables are exactly at the bounds

At iterate    0    f=          NaN    |proj g|=          NaN

           * * *

Tit   = total number of iterations
Tnf   = total number of function evaluations
Tnint = total number of segments explored during Cauchy searches
Skip  = number of BFGS updates skipped
Nact  = number of active bounds at final generalized Cauchy point
Projg = norm of the final projected gradient
F     = final function value

           * * *

   N    Tit     Tnf  Tnint  Skip  Nact     Projg        F
    6      1     21      1     0     0         NaN         NaN
  F =                       NaN

ABNORMAL_TERMINATION_IN_LNSRCH

 Line search cannot locate an adequate point after MAXLS
  function and gradient evaluations.
  Previous x, f and g restored.
 Possible causes: 1 error in function or gradient evaluation;
                  2 rounding error dominate computation.
/opt/miniconda3/envs/ml/lib/python3.7/site-packages/statsmodels/base/model.py:568: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
  ConvergenceWarning)
```

The culprint seems to be [this line](https://github.com/statsmodels/statsmodels/blob/main/statsmodels/tsa/statespace/sarimax.py#L902) in `_conditional_sum_of_squares`, where the slice over the residuals results in an empty vector, and ultimately start params of zeroes.

```python
params_variance = (residuals[k_params_ma:] ** 2).mean()
```

#### Expected Output

My expectation is that either:

1. The fit would succeed
2. The fit would fail with a non-cryptic error. In this case, if there is an issue in the interaction between the length of the endog vector (19) and the order (`(0, 1, 5)`), I would expect a clear `ValueError` or `RuntimeError` rather than an easily missed warning that can lead to non-obvious failures.

#### Output of ``import statsmodels.api as sm; sm.show_versions()``

<details>

```
INSTALLED VERSIONS
------------------
Python: 3.7.10.final.0
OS: Darwin 19.6.0 Darwin Kernel Version 19.6.0: Thu Jan 13 01:26:33 PST 2022; root:xnu-6153.141.51~3/RELEASE_X86_64 x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

statsmodels
===========

Installed: 0.12.2 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/statsmodels)

Required Dependencies
=====================

cython: 0.29.24 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/Cython)
numpy: 1.19.5 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/numpy)
scipy: 1.7.1 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/scipy)
pandas: 1.3.2 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/pandas)
    dateutil: 2.8.2 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/dateutil)
patsy: 0.5.1 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/patsy)

Optional Dependencies
=====================

matplotlib: 3.4.3 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/matplotlib)
    backend: MacOSX
cvxopt: Not installed
joblib: 1.0.1 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/joblib)

Developer Tools
================

IPython: 7.22.0 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/IPython)
    jinja2: 3.0.0 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/jinja2)
sphinx: Not installed
    pygments: 2.8.1 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/pygments)
pytest: 6.2.4 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/pytest)
virtualenv: 20.8.1 (/opt/miniconda3/envs/ml/lib/python3.7/site-packages/virtualenv)
```

</details>



Discussion:
Slightly funny how we're walking up the dependency chain here 😄, thanks for drilling down, @tgsmith61591!
FYI @garrus990, we're now in `statsmodels`, from `pmdarima`, from `sktime`.
This is a bug related to starting values calculation. 

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
@@ -898,7 +898,7 @@ 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:
-            if len(residuals) > 1:
+            if len(residuals) > max(1, k_params_ma):
                 params_variance = (residuals[k_params_ma:] ** 2).mean()
             else:
                 params_variance = np.var(endog)


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
@@ -2813,6 +2813,24 @@ def test_sarimax_starting_values_few_obsevations(reset_randomstate):
     )
 
 
+def test_sarimax_starting_values_few_obsevations_long_ma(reset_randomstate):
+    # GH 8232
+    y = np.random.standard_normal(9)
+    y = [
+        3066.3, 3260.2, 3573.7, 3423.6, 3598.5, 3802.8, 3353.4, 4026.1,
+        4684. , 4099.1, 3883.1, 3801.5, 3104. , 3574. , 3397.2, 3092.9,
+        3083.8, 3106.7, 2939.6
+    ]
+
+    sarimax_model = sarimax.SARIMAX(
+        endog=y, order=(0, 1, 5), trend="n"
+    ).fit(disp=False)
+
+    assert np.all(
+        np.isfinite(sarimax_model.predict(start=len(y), end=len(y) + 11))
+    )
+
+
 def test_sarimax_forecast_exog_trend(reset_randomstate):
     # Test that an error is not raised that the given `exog` for the forecast
     # period is a constant when forecating with an intercept