code: def test_partials_logistic():
      # Here we compare to analytic derivatives and to finite-difference
      # approximations
      logistic = markov_switching._logistic
      partials_logistic = markov_switching._partials_logistic
  
      # For a number, logistic(x) = np.exp(x) / (1 + np.exp(x))
      # Then d/dx = logistix(x) - logistic(x)**2
      cases = [0, 10.0, -4]
      for x in cases:
          assert_allclose(
              partials_logistic(x), logistic(x) - logistic(x) ** 2
          )
          assert_allclose(
              partials_logistic(x), approx_fprime_cs([x], logistic)
          )
  
      # For a vector, logistic(x) returns
      # np.exp(x[i]) / (1 + np.sum(np.exp(x[:]))) for each i
      # Then d logistic(x[i]) / dx[i] = (logistix(x) - logistic(x)**2)[i]
      # And d logistic(x[i]) / dx[j] = -(logistic(x[i]) * logistic[x[j]])
      cases = [[1.0], [0, 1.0], [-2, 3.0, 1.2, -30.0]]
      for x in cases:
          evaluated = np.atleast_1d(logistic(x))
          partials = np.diag(evaluated - evaluated**2)
          for i in range(len(x)):
              for j in range(i):
                  partials[i, j] = partials[j, i] = (
                      -evaluated[i] * evaluated[j]
                  )
          assert_allclose(partials_logistic(x), partials)
          assert_allclose(
              partials_logistic(x), approx_fprime_cs(x, logistic)
          )
  
      # For a 2-dim, logistic(x) returns
      # np.exp(x[i, t]) / (1 + np.sum(np.exp(x[:, t]))) for each i, each t
      # but squeezed
      case = [[1.0]]
      evaluated = logistic(case)
      partial = [evaluated - evaluated**2]
      assert_allclose(partials_logistic(case), partial)
      assert_allclose(
          partials_logistic(case), approx_fprime_cs(case, logistic)
      )
  
      # # Here, np.array(case) is 2x1, so it is interpreted as i=0, 1 and t=0
      case = [[0], [1.0]]
      evaluated = logistic(case)[:, 0]
      partials = np.diag(evaluated - evaluated**2)
      partials[0, 1] = partials[1, 0] = -np.multiply(*evaluated)
      assert_allclose(partials_logistic(case)[:, :, 0], partials)
      assert_allclose(
          partials_logistic(case),
          approx_fprime_cs(np.squeeze(case), logistic)[..., None],
      )
  
      # Here, np.array(case) is 1x2, so it is interpreted as i=0 and t=0, 1
      case = [[0, 1.0]]
      evaluated = logistic(case)
      partials = (evaluated - evaluated**2)[None, ...]
      assert_allclose(partials_logistic(case), partials)
      assert_allclose(
          partials_logistic(case), approx_fprime_cs(case, logistic).T
      )
  
      # For a 3-dim, logistic(x) returns
      # np.exp(x[i, j, t]) / (1 + np.sum(np.exp(x[:, j, t])))
      # for each i, each j, each t
      case = np.arange(2 * 3 * 4).reshape(2, 3, 4)
      evaluated = logistic(case)
      partials = partials_logistic(case)
      for t in range(4):
          for j in range(3):
              desired = np.diag(
                  evaluated[:, j, t] - evaluated[:, j, t] ** 2
              )
              desired[0, 1] = desired[1, 0] = -np.multiply(
                  *evaluated[:, j, t]
              )
              assert_allclose(partials[..., j, t], desired)
  
file_path: statsmodels/statsmodels/tsa/regime_switching/tests/test_markov_switching.py
imports: import numpy
  from numpy.testing import assert_equal, assert_allclose, assert_raises
  import pandas
  from statsmodels.tools.numdiff import approx_fprime_cs
  from statsmodels.tsa.regime_switching import markov_switching
label_desc: Use image node_id 7 for calling a global function with example usage: test_partials_logistic() without return types