code: def gen_k_factor2(
      nobs=10000,
      k=2,
      idiosyncratic_ar1=False,
      idiosyncratic_var=0.4,
      k_ar=6,
  ):
      # Simulate bivariate VAR(6) for the factor
      ix = pd.period_range(start="1950-01", periods=1, freq="M")
      faux = pd.DataFrame([[0, 0]], index=ix, columns=["f1", "f2"])
      mod = varmax.VARMAX(faux, order=(k_ar, 0), trend="n")
      A = np.zeros((2, 2 * k_ar))
      A[:, -2:] = np.array([[0.5, -0.2], [0.1, 0.3]])
      Q = np.array([[1.5, 0.2], [0.2, 0.5]])
      L = np.linalg.cholesky(Q)
      params = np.r_[A.ravel(), L[np.tril_indices_from(L)]]
  
      # Simulate the factors
      factors = mod.simulate(params, nobs)
  
      # Add in the idiosyncratic part
      faux = pd.Series([0], index=ix)
      mod_idio = sarimax.SARIMAX(faux, order=(1, 0, 0))
      phi = [0.7, -0.2] if idiosyncratic_ar1 else [0, 0.0]
      tmp = factors.iloc[:, 0] + factors.iloc[:, 1]
  
      # Monthly variables
      endog_M = pd.concat([tmp.copy() for i in range(k)], axis=1)
      columns = []
      for i in range(k):
          endog_M.iloc[:, i] = endog_M.iloc[:, i] + mod_idio.simulate(
              [phi[0], idiosyncratic_var], nobs
          )
          columns += [f"yM{i + 1}_f2"]
      endog_M.columns = columns
  
      # Monthly versions of quarterly variables
      endog_Q_M = pd.concat([tmp.copy() for i in range(k)], axis=1)
      columns = []
      for i in range(k):
          endog_Q_M.iloc[:, i] = endog_Q_M.iloc[
              :, i
          ] + mod_idio.simulate([phi[0], idiosyncratic_var], nobs)
          columns += [f"yQ{i + 1}_f2"]
      endog_Q_M.columns = columns
  
      # Create quarterly versions of quarterly variables
      levels_M = 1 + endog_Q_M / 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 quarterly growth rate series
      endog_Q = log_levels_Q.diff()
  
      return endog_M, endog_Q, factors
  
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 8 for calling a global function with example usage: gen_k_factor2(nobs, k, idiosyncratic_ar1, idiosyncratic_var, k_ar) and returns: endog_M,  endog_Q,  factors