2016-02-01 87 views

回答

1

在scipy中沒有多項分佈,雖然it might be available in the future version (0.18 or up)

同時,您可以很容易DIY吧:

def logpmf(self, x, n, p): 
    """Log of the multinomial probability mass function. 
    Parameters 
    ---------- 
    x : array_like 
     Quantiles. 
    n : int 
     Number of trials 
    p : array_like, shape (k,) 
     Probabilities. These should sum to one. If they do not, then 
     ``p[-1]`` is modified to account for the remaining probability so 
     that ``sum(p) == 1``. 
    Returns 
    ------- 
    logpmf : float 
     Log of the probability mass function evaluated at `x`. 
    """ 

    x = np.asarray(x) 
    if p.shape[0] != x.shape[-1]: 
     raise ValueError("x & p shapes do not match.") 

    coef = gammaln(n + 1) - gammaln(x + 1.).sum(axis=-1) 
    val = coef + np.sum(xlogy(x, p), axis=-1) 

    # insist on that the support is a set of *integers* 
    mask = np.logical_and.reduce(np.mod(x, 1) == 0, axis=-1) 

    mask &= (x.sum(axis=-1) == n) 
    out = np.where(mask, val, -np.inf) 
    return out 

這裏gammalnscipy.special.gammalnxlogyscipy.special.xlogy。正如你看到的主要工作是確保pmf爲非零整數值爲零。

0

scipy中沒有提供Multinomial PMF功能。但是,您可以自己使用numpy.random.multinomial類來繪製樣本。

+0

@ cjohnson318該示例不使用''numpy.random.multinomial'' – mvd