code: def test_np_multinomial():
      pvals_list = [
          [0.0, 0.1, 0.2, 0.3, 0.4],
          [0.4, 0.3, 0.2, 0.1, 0.0],
      ]
      sizes = [None, (), (3,), (2, 5, 7), (4, 9)]
      experiements = 10000
      for pvals_mx_np_array in [False, True]:
          for have_size in [False, True]:
              for pvals in pvals_list:
                  if pvals_mx_np_array:
                      pvals = mx.np.array(pvals)
                  if have_size:
                      for size in sizes:
                          freq = mx.np.random.multinomial(
                              experiements, pvals, size=size
                          ).asnumpy() / _np.float32(experiements)
                          # for those cases that didn't need reshape
                          if size in [None, ()]:
                              if type(pvals) == np.ndarray:
                                  mx.test_utils.assert_almost_equal(
                                      freq,
                                      pvals.asnumpy(),
                                      rtol=0.20,
                                      atol=1e-1,
                                  )
                              else:
                                  mx.test_utils.assert_almost_equal(
                                      freq, pvals, rtol=0.20, atol=1e-1
                                  )
                          else:
                              # check the shape
                              assert freq.shape == size + (
                                  len(pvals),
                              ), "freq.shape={}, size + (len(pvals))={}".format(
                                  freq.shape, size + (len(pvals))
                              )
                              freq = freq.reshape((-1, len(pvals)))
                              # check the value for each row
                              for i in range(freq.shape[0]):
                                  if type(pvals) == np.ndarray:
                                      mx.test_utils.assert_almost_equal(
                                          freq[i, :],
                                          pvals.asnumpy(),
                                          rtol=0.20,
                                          atol=1e-1,
                                      )
                                  else:
                                      mx.test_utils.assert_almost_equal(
                                          freq[i, :],
                                          pvals,
                                          rtol=0.20,
                                          atol=1e-1,
                                      )
                  else:
                      freq = mx.np.random.multinomial(
                          experiements, pvals
                      ).asnumpy() / _np.float32(experiements)
                      if type(pvals) == np.ndarray:
                          mx.test_utils.assert_almost_equal(
                              freq,
                              pvals.asnumpy(),
                              rtol=0.20,
                              atol=1e-1,
                          )
                      else:
                          mx.test_utils.assert_almost_equal(
                              freq, pvals, rtol=0.20, atol=1e-1
                          )
      # check the zero dimension
      sizes = [(0), (0, 2), (4, 0, 2), (3, 0, 1, 2, 0)]
      for pvals_mx_np_array in [False, True]:
          for pvals in pvals_list:
              for size in sizes:
                  if pvals_mx_np_array:
                      pvals = mx.np.array(pvals)
                  freq = mx.np.random.multinomial(
                      experiements, pvals, size=size
                  ).asnumpy()
                  assert freq.size == 0
      # check [] as pvals
      for pvals_mx_np_array in [False, True]:
          for pvals in [[], ()]:
              if pvals_mx_np_array:
                  pvals = mx.np.array(pvals)
              freq = mx.np.random.multinomial(
                  experiements, pvals
              ).asnumpy()
              assert freq.size == 0
              for size in sizes:
                  freq = mx.np.random.multinomial(
                      experiements, pvals, size=size
                  ).asnumpy()
                  assert freq.size == 0
      # test small experiment for github issue
      # https://github.com/apache/mxnet/issues/15383
      small_exp, total_exp = 20, 10000
      for pvals_mx_np_array in [False, True]:
          for pvals in pvals_list:
              if pvals_mx_np_array:
                  pvals = mx.np.array(pvals)
              x = np.random.multinomial(small_exp, pvals)
              for _ in range(total_exp // small_exp):
                  x = x + np.random.multinomial(20, pvals)
          freq = (x.asnumpy() / _np.float32(total_exp)).reshape(
              (-1, len(pvals))
          )
          for i in range(freq.shape[0]):
              if type(pvals) == np.ndarray:
                  mx.test_utils.assert_almost_equal(
                      freq[i, :], pvals.asnumpy(), rtol=0.20, atol=1e-1
                  )
              else:
                  mx.test_utils.assert_almost_equal(
                      freq[i, :], pvals, rtol=0.20, atol=1e-1
                  )
  
imports: from __future__ import absolute_import
  from __future__ import division
  import itertools
  import os
  import pytest
  import operator
  import numpy
  import mxnet
  from mxnet import np, npx, autograd
  from mxnet.gluon import HybridBlock
  from mxnet.test_utils import same, assert_almost_equal, rand_shape_nd, rand_ndarray, use_np
  from common import retry, TemporaryDirectory, xfail_when_nonstandard_decimal_separator
  from mxnet.test_utils import verify_generator, gen_buckets_probs_with_ppf, assert_exception, is_op_runnable, collapse_sum_like
  from mxnet.ndarray.ndarray import py_slice
  from mxnet.base import integer_types
label_desc: Use image node_id 15 for calling a global function with example usage: test_np_multinomial() without return types