當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python sympy.stats.Multinomial()用法及代碼示例


借助於sympy.stats.Multinomial()方法,我們可以創建具有多項式分布的離散隨機變量。

多項分布是多項實驗結果的概率分布。

用法:sympy.stats.Multinomial(syms, n, p)

參數:
 syms:the symbol
 n:is the number of trials, a positive integer
 p:event probabilites, p>= 0 and p<= 1

返回:a discrete random variable with Multinomial Distribution

範例1:

Python3

# import sympy, Multinomial, density, symbols 
from sympy.stats.joint_rv_types import Multinomial 
from sympy.stats import density 
from sympy import symbols, pprint 
  
x1, x2, x3 = symbols('x1, x2, x3', nonnegative = True, integer = True) 
p1, p2, p3 = symbols('p1, p2, p3', positive = True) 
  
# Using sympy.stats.Multinomial() method 
M = Multinomial('M', 3, p1, p2, p3) 
multiDist = density(M)(x1, x2, x3) 
  
pprint(multiDist)

輸出:



/    x1   x2   x3                      
|6*p1  *p2  *p3                        
|----------------  for x1 + x2 + x3 = 3
<  x1!*x2!*x3!                         
|                                      
|       0               otherwise      
\                                   

範例2:

Python3

# import sympy, Multinomial, density, symbols 
from sympy.stats.joint_rv_types import Multinomial 
from sympy.stats import density 
from sympy import symbols, pprint 
  
x1, x2, x3 = symbols('x1, x2, x3', nonnegative = True, integer = True) 
  
# Using sympy.stats.Multinomial() method 
M = Multinomial('M', 4, 0, 1, 0) 
multiDist = density(M)(x1, x2, x3) 
  
pprint(multiDist)

輸出:

/     x1  x3                      
| 24*0  *0                        
|-----------  for x1 + x2 + x3 = 4
<x1!*x2!*x3!                      
|                                 
|     0            otherwise      
\                                




相關用法


注:本文由純淨天空篩選整理自ravikishor大神的英文原創作品 sympy.stats.Multinomial() function in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。