code: def simulate_k_factor1(nobs=1000):
      mod_sim = dynamic_factor.DynamicFactor(
          np.zeros((1, 4)), k_factors=1, factor_order=1, error_order=1
      )
      loadings = [1.0, -0.75, 0.25, -0.3, 0.5]
      p = np.r_[
          loadings[: mod_sim.k_endog],
          [10] * mod_sim.k_endog,
          0.5,
          [0.0] * mod_sim.k_endog,
      ]
      ix = pd.period_range(start="1935-01", periods=nobs, freq="M")
      endog = pd.DataFrame(mod_sim.simulate(p, nobs), index=ix)
  
      true = pd.Series(p, index=mod_sim.param_names)
  
      # Compute levels series (M and Q)
      ix = pd.period_range(
          start=endog.index[0] - 1,
          end=endog.index[-1],
          freq=endog.index.freq,
      )
      levels_M = 1 + endog.reindex(ix) / 100
      levels_M.iloc[0] = 100
      levels_M = levels_M.cumprod()
      log_levels_M = np.log(levels_M) * 100
      # Recast to datetime and then back to period to resample
      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()
  
      # This is an alternative way to compute the quarterly levels
      # endog_M = endog.iloc[:, :3]
      # x = endog.iloc[:, 3:]
      # endog_Q = (x + 2 * x.shift(1) + 3 * x.shift(2) + 2 * x.shift(3) +
      #            x.shift(4)).resample('Q', convention='e').last().iloc[:-1] / 3
      # levels_Q = 1 + endog.iloc[:, 3:] / 100
      # levels_Q.iloc[0] = 100
  
      # Here is another alternative way to compute the quarterly levels
      # weights = np.array([1, 2, 3, 2, 1])
      # def func(x, weights):
      #     return np.sum(weights * x)
      # r = endog_M.rolling(5)
      # (r.apply(func, args=(weights,), raw=False).resample('Q', convention='e')
      #                                           .last().iloc[:-1].tail())
  
      # Compute the growth rate series that we'll actually run the model on
      endog_M = log_levels_M.iloc[:, :3].diff()
      endog_Q = log_levels_Q.iloc[:, 3:].diff()
  
      return endog_M, endog_Q, log_levels_M, log_levels_Q, true
  
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 1 for calling a global function with example usage: simulate_k_factor1(nobs) and returns: endog_M,  endog_Q,  log_levels_M,  log_levels_Q,  true