code: def test_k_factor2_factor_order_6(reset_randomstate):
      # This tests that the model is correctly set up when the lag order of the
      # factor is longer than 5 and we have two factors. This is important
      # because 5 lags are always present when there is quarterly data, but we
      # want to check that, for example, we haven't accidentally relied on there
      # being exactly 5 lags available.
      # Note: as of 2020/07/25, the FRBNY code does not seem to work for 6 lags,
      # so we can't test against their code
  
      endog_M, endog_Q, factors = gen_k_factor2()
  
      # Add the factors in to endog_M, which will allow us to identify them,
      # since endog_M and endog_Q are all the same linear combination of the
      # factors
      endog_M_aug = pd.concat([factors, endog_M], axis=1)
  
      mod = dynamic_factor_mq.DynamicFactorMQ(
          endog_M_aug,
          endog_quarterly=endog_Q,
          factor_multiplicities=2,
          factor_orders=6,
          idiosyncratic_ar1=False,
          standardize=False,
      )
      res = mod.fit()
  
      # The identification for the VAR system means that it is harder to visually
      # check that the estimation procedure produced good estimates.
  
      # This is the invertible matrix that we'll use to transform the factors
      # and parameter matrices into the original form
      M = np.kron(np.eye(6), mod["design", :2, :2])
      Mi = np.linalg.inv(M)
  
      # Get the estimated parameter matrices
      Z = mod["design", :, :12]
      A = mod["transition", :12, :12]
      R = mod["selection", :12, :2]
      Q = mod["state_cov", :2, :2]
      RQR = R @ Q @ R.T
  
      # Create the transformed matrices
      Z2 = Z @ Mi
      A2 = M @ A @ Mi
      Q2 = M @ RQR @ M.T
  
      # In this example, both endog_M and endog_Q are equal to the factors,
      # so we expect the loading matrix to look like, which can be confirmed
      # (up to some numerical precision) by printing Z2
      # [ I   0   0   0  0 ]
      # [ I  2I  3I  2I  I ]
      print(Z2.round(2))
      desired = np.array(
          [
              [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              [1, 1, 2, 2, 3, 3, 2, 2, 1, 1, 0, 0],
              [1, 1, 2, 2, 3, 3, 2, 2, 1, 1, 0, 0],
          ]
      )
      assert_allclose(Z2, desired, atol=0.1)
      # Confirm that this is approximately:
      # [  0  0    0  0    0  0    0  0    0  0    0.5 -0.2 ]
      # [  0  0    0  0    0  0    0  0    0  0    0.1  0.3 ]
      print(A2.round(2))
      desired = np.array(
          [
              [
                  0,
                  0,
                  0.02,
                  0,
                  0.01,
                  -0.03,
                  0.01,
                  0.02,
                  0,
                  -0.01,
                  0.5,
                  -0.2,
              ],
              [0, 0, 0, 0.02, 0, -0.01, 0, 0, 0, 0.01, 0.1, 0.3],
              [1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              [0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              [0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0],
          ]
      )
      assert_allclose(A2, desired, atol=1e-2)
      # Confirm that this is approximately:
      # [ 1.5  0.2 ]
      # [ 0.2  0.5 ]
      # in the top left corner, and then zeros elsewhere
      print(Q2.round(2))
      desired = np.array([[1.49, 0.21], [0.21, 0.49]])
      assert_allclose(Q2[:2, :2], desired, atol=1e-2)
      assert_allclose(Q2[:2, 2:], 0, atol=1e-2)
      assert_allclose(Q2[2:, :2], 0, atol=1e-2)
      assert_allclose(Q2[2:, 2:], 0, atol=1e-2)
  
      # Finally, check that after the transformation, the factors are equal to
      # endog_M
      a = res.states.smoothed
      a2 = (M @ a.T.iloc[:12]).T
      assert_allclose(endog_M.values, a2.iloc[:, :2].values, atol=1e-10)
  
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 9 for calling a global function with example usage: test_k_factor2_factor_order_6(reset_randomstate) without return types