code: def test_fdr_twostage():
      # test for iteration in fdrcorrection_twostage, new maxiter
      # example from BKY
      pvals = [
          0.0001,
          0.0004,
          0.0019,
          0.0095,
          0.0201,
          0.0278,
          0.0298,
          0.0344,
          0.0459,
          0.3240,
          0.4262,
          0.5719,
          0.6528,
          0.7590,
          1.000,
      ]
      n = len(pvals)
  
      # bh twostage fdr
      k = 0
      # same pvalues as one-stage fdr
      res0 = multipletests(pvals, alpha=0.05, method="fdr_bh")
      res1 = fdrcorrection_twostage(
          pvals, alpha=0.05, method="bh", maxiter=k, iter=None
      )
      res2 = multipletests(
          pvals, alpha=0.05, method="fdr_tsbh", maxiter=k
      )
      assert_allclose(res1[1], res0[1])
      assert_allclose(res2[1], res1[1])
  
      k = 1
      # pvalues corrected by first stage number of rejections
      res0 = multipletests(pvals, alpha=0.05, method="fdr_bh")
      res1 = fdrcorrection_twostage(
          pvals, alpha=0.05, method="bh", maxiter=k, iter=None
      )
      res2 = multipletests(
          pvals, alpha=0.05, method="fdr_tsbh", maxiter=k
      )
      res3 = multipletests(pvals, alpha=0.05, method="fdr_tsbh")
      assert_allclose(res1[1], res0[1] * (1 - res0[0].sum() / n))
      assert_allclose(res2[1], res1[1])
      assert_allclose(res3[1], res1[1])  # check default maxiter
  
      # bky has an extra factor 1+alpha in fdr twostage independent of iter
      fact = 1 + 0.05
      k = 0
      # same pvalues as one-stage fdr
      res0 = multipletests(pvals, alpha=0.05, method="fdr_bh")
      res1 = fdrcorrection_twostage(
          pvals, alpha=0.05, method="bky", maxiter=k, iter=None
      )
      res2 = multipletests(
          pvals, alpha=0.05, method="fdr_tsbky", maxiter=k
      )
      assert_allclose(res1[1], np.clip(res0[1] * fact, 0, 1))
      assert_allclose(res2[1], res1[1])
  
      k = 1
      # pvalues corrected by first stage number of rejections
      res0 = multipletests(pvals, alpha=0.05, method="fdr_bh")
      res1 = fdrcorrection_twostage(
          pvals, alpha=0.05, method="bky", maxiter=k, iter=None
      )
      res2 = multipletests(
          pvals, alpha=0.05, method="fdr_tsbky", maxiter=k
      )
      res3 = multipletests(pvals, alpha=0.05, method="fdr_tsbky")
      assert_allclose(res1[1], res0[1] * (1 - res0[0].sum() / n) * fact)
      assert_allclose(res2[1], res1[1])
      assert_allclose(res3[1], res1[1])
  
imports: import pytest
  import numpy
  from numpy.testing import assert_almost_equal, assert_equal, assert_allclose
  from statsmodels.stats.multitest import multipletests, fdrcorrection, fdrcorrection_twostage, NullDistribution, local_fdr, multitest_methods_names
  from statsmodels.stats.multicomp import tukeyhsd
  from scipy.stats.distributions import norm
  import scipy
  from packaging import version
label_desc: Use image node_id 4 for calling a global function with example usage: test_fdr_twostage() without return types