code: def test_arma_direct():
      # Tests of an ARMA model simulation against direct construction
      # This is useful for e.g. trend components
      # Note: the first elements of the generated SARIMAX datasets are based on
      # the initial state, so we do not include them in the comparisons
      np.random.seed(10239)
      nobs = 100
      eps = np.random.normal(size=nobs)
      exog = np.random.normal(size=nobs)
  
      # AR(1)
      mod = sarimax.SARIMAX([0], order=(1, 0, 0))
      actual = mod.simulate(
          [0.5, 1.0],
          nobs + 1,
          state_shocks=np.r_[eps, 0],
          initial_state=np.zeros(mod.k_states),
      )
      desired = np.zeros(nobs)
      for i in range(nobs):
          if i == 0:
              desired[i] = eps[i]
          else:
              desired[i] = 0.5 * desired[i - 1] + eps[i]
      assert_allclose(actual[1:], desired)
  
      # MA(1)
      mod = sarimax.SARIMAX([0], order=(0, 0, 1))
      actual = mod.simulate(
          [0.5, 1.0],
          nobs + 1,
          state_shocks=np.r_[eps, 0],
          initial_state=np.zeros(mod.k_states),
      )
      desired = np.zeros(nobs)
      for i in range(nobs):
          if i == 0:
              desired[i] = eps[i]
          else:
              desired[i] = 0.5 * eps[i - 1] + eps[i]
      assert_allclose(actual[1:], desired)
  
      # ARMA(1, 1)
      mod = sarimax.SARIMAX([0], order=(1, 0, 1))
      actual = mod.simulate(
          [0.5, 0.2, 1.0],
          nobs + 1,
          state_shocks=np.r_[eps, 0],
          initial_state=np.zeros(mod.k_states),
      )
      desired = np.zeros(nobs)
      for i in range(nobs):
          if i == 0:
              desired[i] = eps[i]
          else:
              desired[i] = (
                  0.5 * desired[i - 1] + 0.2 * eps[i - 1] + eps[i]
              )
      assert_allclose(actual[1:], desired)
  
      # ARMA(1, 1) + intercept
      mod = sarimax.SARIMAX([0], order=(1, 0, 1), trend="c")
      actual = mod.simulate(
          [1.3, 0.5, 0.2, 1.0],
          nobs + 1,
          state_shocks=np.r_[eps, 0],
          initial_state=np.zeros(mod.k_states),
      )
      desired = np.zeros(nobs)
      for i in range(nobs):
          trend = 1.3
          if i == 0:
              desired[i] = trend + eps[i]
          else:
              desired[i] = (
                  trend
                  + 0.5 * desired[i - 1]
                  + 0.2 * eps[i - 1]
                  + eps[i]
              )
      assert_allclose(actual[1:], desired)
  
      # ARMA(1, 1) + intercept + time trend
      # Note: to allow time-varying SARIMAX to simulate 101 observations, need to
      # give it 101 observations up front
      mod = sarimax.SARIMAX(
          np.zeros(nobs + 1), order=(1, 0, 1), trend="ct"
      )
      actual = mod.simulate(
          [1.3, 0.2, 0.5, 0.2, 1.0],
          nobs + 1,
          state_shocks=np.r_[eps, 0],
          initial_state=np.zeros(mod.k_states),
      )
      desired = np.zeros(nobs)
      for i in range(nobs):
          trend = 1.3 + 0.2 * (i + 1)
          if i == 0:
              desired[i] = trend + eps[i]
          else:
              desired[i] = (
                  trend
                  + 0.5 * desired[i - 1]
                  + 0.2 * eps[i - 1]
                  + eps[i]
              )
      assert_allclose(actual[1:], desired)
  
      # ARMA(1, 1) + intercept + time trend + exog
      # Note: to allow time-varying SARIMAX to simulate 101 observations, need to
      # give it 101 observations up front
      # Note: the model is regression with SARIMAX errors, so the exog is
      # introduced into the observation equation rather than the ARMA part
      mod = sarimax.SARIMAX(
          np.zeros(nobs + 1),
          exog=np.r_[0, exog],
          order=(1, 0, 1),
          trend="ct",
      )
      actual = mod.simulate(
          [1.3, 0.2, -0.5, 0.5, 0.2, 1.0],
          nobs + 1,
          state_shocks=np.r_[eps, 0],
          initial_state=np.zeros(mod.k_states),
      )
      desired = np.zeros(nobs)
      for i in range(nobs):
          trend = 1.3 + 0.2 * (i + 1)
          if i == 0:
              desired[i] = trend + eps[i]
          else:
              desired[i] = (
                  trend
                  + 0.5 * desired[i - 1]
                  + 0.2 * eps[i - 1]
                  + eps[i]
              )
      desired = desired - 0.5 * exog
      assert_allclose(actual[1:], desired)
  
imports: from statsmodels.compat.pandas import MONTH_END
  import numpy
  from numpy.testing import assert_, assert_allclose, assert_equal
  import pandas
  import pytest
  from scipy.signal import lfilter
  from statsmodels.tools.sm_exceptions import EstimationWarning, SpecificationWarning
  from statsmodels.tsa.statespace import dynamic_factor, sarimax, structural, varmax
  from .test_impulse_responses import TVSS
label_desc: Use image node_id 2 for calling a global function with example usage: test_arma_direct() without return types