简体   繁体   English

如何用非整数计算二项式系数

[英]how to calculate binomial coefficents with non-integer numbers

我知道如何计算select(5,2)的二项式系数,但是现在我想知道是否存在可以用python或R编程语言计算select(5,2.1)的函数?

The combination formula for "n choose k" is given by “ n选择k”的组合公式为

在此处输入图片说明

where n and k are whole numbers. 其中nk是整数。 The generalized version for x and y in the set of real numbers is given by 实数集中xy的广义形式为

在此处输入图片说明

where Γ(x) is the gamma function, a generalized form of the factorial. 其中Γ(x)是伽马函数,阶乘的广义形式。

To create this in Python, you can use the following: 要在Python中创建此代码,可以使用以下代码:

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

Use the gamma function from scipy and the extended definition of the binomial coefficient. 使用scipy中的伽马函数和二项式系数的扩展定义。

>>> 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

In R you could make a function like this using gamma : 在R中,您可以使用gamma如下函数:

choose <- function(x, y) {
  return(gamma(x+1)/(gamma(y+1)*gamma(x-y+1)))
}

print(choose(5,2.1)) # 10.30404

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM