Skip to main content

Random Multinomial

np.random.multinomial(n, [p_0, p_1, p_2, ...])

Determines the number of times each outcome occurs based on the given probabilities.

Input:
n : integer
Number of random samples to draw.
[p_0, p_1, p_2, ...] : list or array of floats
Probabilities for each outcome.
Returns:
Number of times each outcome is drawn
Return Type:
array of integers
Note:
  • Each element in [p_0, p_1, p_2, ...] must be between 0 and 1. The elements in [p_0, p_1, p_2, ...] must sum to 1.

np.random.multinomial(100, [0.2, 0.5, 0.3])

array([19, 54, 27])

np.random.multinomial(100, [0.1, 0.1, 0.8]) 

array([12, 8, 80])

#use np.random.choice to achieve the function of np.random.multinomial
def manual_multinomial(n, p):
values = np.arange(len(p))
choices = np.random.choice(values, size=n, replace=True, p=p)
value_counts = np.array([])
for value in values:
value_count = np.count_nonzero(choices == value)
value_counts = np.append(value_counts, value_count)
return value_counts.astype(int)

manual_multinomial(100, [0.1, 0.1, 0.8])

array([10, 16, 74])