I know how to calculate binomial coefficients for choose(5,2), but now I want to know if there is a function that I can calculate choose(5,2.1) in python or R programming language?
Asked
Active
Viewed 2,413 times
0
-
I can use calculator to calculate it is equal to 10.304, so this question make sense. – Yaphet Dec 09 '17 at 04:16
-
What code have you written so far? Please [edit] to add meaningful code and a problem description here. Posting a [Minimal, Complete, Verifiable Example](http://stackoverflow.com/help/mcve) that demonstrates your problem would help you get better answers. Thanks! – Tom Aranda Dec 09 '17 at 04:33
3 Answers
6
The combination formula for "n choose k" is given by
where n
and k
are whole numbers. The generalized version for x
and y
in the set of real numbers is given by
where Γ(x)
is the gamma function, a generalized form of the factorial.
To create this in Python, you can use the following:
import math
def generalized_binomial(x,y):
return math.gamma(x+1) / (math.gamma(y+1) * math.gamma(x-y+1))
generalized_binomial(5,2.1)
# returns:
10.304042688575835

James
- 32,991
- 4
- 47
- 70
1
Use the gamma function from scipy and the extended definition of the binomial coefficient.
>>> from scipy.special import gamma
>>> def choose(x,y):
... return gamma(x+1)/(gamma(y+1)*gamma(x-y+1))
...
>>> choose(5,2.1)
10.304042688575837

Bill Bell
- 21,021
- 5
- 43
- 58
1
In R you could make a function like this using gamma
:
choose <- function(x, y) {
return(gamma(x+1)/(gamma(y+1)*gamma(x-y+1)))
}
print(choose(5,2.1)) # 10.30404

Sash Sinha
- 18,743
- 3
- 23
- 40