code: def cochrans_q(x, return_object=True):
      """
      Cochran's Q test for identical binomial proportions.
  
      Parameters
      ----------
      x : array_like, 2d (N, k)
          data with N cases and k variables
      return_object : bool
          Return values as bunch instead of as individual values.
  
      Returns
      -------
      Returns a bunch containing the following attributes, or the
      individual values according to the value of `return_object`.
  
      statistic : float
         test statistic
      pvalue : float
         pvalue from the chisquare distribution
  
      Notes
      -----
      Cochran's Q is a k-sample extension of the McNemar test. If there
      are only two groups, then Cochran's Q test and the McNemar test
      are equivalent.
  
      The procedure tests that the probability of success is the same
      for every group.  The alternative hypothesis is that at least two
      groups have a different probability of success.
  
      In Wikipedia terminology, rows are blocks and columns are
      treatments.  The number of rows N, should be large for the
      chisquare distribution to be a good approximation.
  
      The Null hypothesis of the test is that all treatments have the
      same effect.
  
      References
      ----------
      https://en.wikipedia.org/wiki/Cochran_test
      SAS Manual for NPAR TESTS
      """
  
      x = np.asarray(x, dtype=np.float64)
      gruni = np.unique(x)
      N, k = x.shape
      count_row_success = (x == gruni[-1]).sum(1, float)
      count_col_success = (x == gruni[-1]).sum(0, float)
      count_row_ss = count_row_success.sum()
      count_col_ss = count_col_success.sum()
      assert count_row_ss == count_col_ss  # just a calculation check
  
      # From the SAS manual
      q_stat = (
          (k - 1)
          * (k * np.sum(count_col_success**2) - count_col_ss**2)
          / (k * count_row_ss - np.sum(count_row_success**2))
      )
  
      # Note: the denominator looks just like k times the variance of
      # the columns
      # Wikipedia uses a different, but equivalent expression
      # q_stat = (k-1) * (k *  np.sum(count_row_success**2) - count_row_ss**2)
      #         / (k * count_col_ss - np.sum(count_col_success**2))
  
      df = k - 1
      pvalue = stats.chi2.sf(q_stat, df)
  
      if return_object:
          b = _Bunch()
          b.statistic = q_stat
          b.df = df
          b.pvalue = pvalue
          return b
  
      return q_stat, pvalue, df
  
imports: import warnings
  import numpy
  import pandas
  from scipy import stats
  from statsmodels import iolib
  from statsmodels.tools import sm_exceptions
  from statsmodels.tools.decorators import cache_readonly
label_desc: Use image node_id 3 for calling a global function with example usage: cochrans_q(x, return_object) and returns: q_stat,  pvalue,  df, b