Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python have a function which computes multinomial coefficients?

Tags:

I was looking for a Python library function which computes multinomial coefficients.

I could not find any such function in any of the standard libraries. For binomial coefficients (of which multinomial coefficients are a generalization) there is scipy.special.binom and also scipy.misc.comb. Also, numpy.random.multinomial draws samples from a multinomial distribution, and sympy.ntheory.multinomial.multinomial_coefficients returns a dictionary related to multinomial coefficients.

However, I could not find a multinomial coefficients function proper, which given a,b,...,z returns (a+b+...+z)!/(a! b! ... z!). Did I miss it? Is there a good reason there is none available?

I would be happy to contribute an efficient implementation to SciPy say. (I would have to figure out how to contribute, as I have never done this).

For background, they do come up when expanding (a+b+...+z)^n. Also, they count the ways of depositing a+b+...+z distinct objects into distinct bins such that the first bin contains a objects, etc. I need them occasionally for a Project Euler problem.

BTW, other languages do offer this function: Mathematica, MATLAB, Maple.

like image 256
Reiner Martin Avatar asked Sep 22 '17 22:09

Reiner Martin


People also ask

How do you find the coefficient of a multinomial?

A multinomial coefficient describes the number of possible partitions of n objects into k groups of size n1, n2, …, nk. The formula to calculate a multinomial coefficient is: Multinomial Coefficient = n! / (n1! * n2!

How do you find the binomial coefficient in Python?

Use the math. comb() Function to Calculate the Binomial Coefficient in Python. The comb() function from the math module returns the combination of the given values, which essentially has the same formula as the binomial coefficient. This method is an addition to recent versions of Python 3.8 and above.

What does multinomial coefficient count?

In formal terms, the multinomial coefficient formula gives the expansion of (k1 + k2 … + kn) where ki are non-negative integers. Informally, you can think of it as a way to find how many permutations are possible when you have duplicate values for k.


1 Answers

To partially answer my own question, here is my simple and fairly efficient implementation of the multinomial function:

def multinomial(lst):     res, i = 1, 1     for a in lst:         for j in range(1,a+1):             res *= i             res //= j             i += 1     return res 

It seems from the comments so far that no efficient implementation of the function exists in any of the standard libraries.

Update (January 2020). As Don Hatch has pointed out in the comments, this can be further improved by looking for the largest argument (especially for the case that it dominates all others):

def multinomial(lst):     res, i = 1, sum(lst)     i0 = lst.index(max(lst))     for a in lst[:i0] + lst[i0+1:]:         for j in range(1,a+1):             res *= i             res //= j             i -= 1     return res 
like image 92
Reiner Martin Avatar answered Sep 25 '22 13:09

Reiner Martin