code: def gen_k_factor1_nonstationary(
      nobs=1000,
      k=1,
      idiosyncratic_ar1=False,
      idiosyncratic_var=0.4,
      k_ar=1,
  ):
      # Simulate univariate random walk
      ix = pd.period_range(start="1950-01", periods=1, freq="M")
      faux = pd.Series([0], index=ix)
      mod = sarimax.SARIMAX(
          faux, order=(k_ar, 0, 0), initialization="diffuse"
      )
  
      params = np.r_[[0] * (k_ar - 1), [1.0001], 1.0]
      factor = mod.simulate(params, nobs)
  
      if idiosyncratic_ar1:
          mod_idio = sarimax.SARIMAX(faux, order=(1, 0, 0))
          endog = pd.concat(
              [
                  factor
                  + mod_idio.simulate([0.7, idiosyncratic_var], nobs)
                  for i in range(2 * k)
              ],
              axis=1,
          )
      else:
          endog = pd.concat(
              [
                  factor
                  + np.random.normal(
                      scale=idiosyncratic_var**0.5, size=nobs
                  )
                  for i in range(2 * k)
              ],
              axis=1,
          )
  
      # Construct the quarterly variable
      levels_M = 1 + endog / 100
      levels_M.iloc[0] = 100
      levels_M = levels_M.cumprod()
      log_levels_M = np.log(levels_M) * 100
  
      log_levels_Q = np.log(levels_M)
      log_levels_Q.index = log_levels_Q.index.to_timestamp()
      log_levels_Q = (
          log_levels_Q.resample(QUARTER_END).sum().iloc[:-1] * 100
      )
      log_levels_Q.index = log_levels_Q.index.to_period()
  
      # Compute the growth rate series that we'll actually run the model on
      endog_M = log_levels_M.diff().iloc[1:, :k]
      if k > 1:
          endog_M.columns = ["yM%d_f1" % (i + 1) for i in range(k)]
      else:
          endog_M.columns = ["yM_f1"]
      endog_Q = log_levels_Q.diff().iloc[1:, k:]
      if k > 1:
          endog_Q.columns = ["yQ%d_f1" % (i + 1) for i in range(k)]
      else:
          endog_Q.columns = ["yQ_f1"]
  
      return endog_M, endog_Q, factor
  
file_path: statsmodels/statsmodels/tsa/statespace/tests/test_dynamic_factor_mq_monte_carlo.py
imports: from statsmodels.compat.pandas import QUARTER_END
  import numpy
  from numpy.testing import assert_allclose
  import pandas
  import pytest
  from scipy.signal import lfilter
  from statsmodels.tsa.statespace import dynamic_factor, dynamic_factor_mq, sarimax, varmax
label_desc: Use image node_id 4 for calling a global function with example usage: gen_k_factor1_nonstationary(nobs, k, idiosyncratic_ar1, idiosyncratic_var, k_ar) and returns: endog_M,  endog_Q,  factor