Python Scipy 添加概率分布

Python Scipy add probability distributions

给定多个概率分布,scipy(或其他库)中是否有允许我添加分布的优化函数?考虑这个简单的例子:

假设我有一个 6 面骰子和一个 20 面骰子,我想知道从 2 滚动到 26 的概率质量函数。

from scipy.stats import randint
import pandas as pd
import plotly.express as px
x_points_6 = [x+1 for x in range(6)]
x_points_20 = [x+1 for x in range(20)]

dist_6_sided = [randint.pmf(k,1,7) for k in x_points_6]
dist_20_sided = [randint.pmf(k,1,21) for k in x_points_20]
total_dist = add_dist(x_points_6, dist_6_sided, x_points_20, dist_20_sided)
df = pd.DataFrame({'x':total_dist[0], 'y':total_dist[1]})

我有一个功能可以强行添加分布:

def add_dist(value1, prob1, value2, prob2):
    result_value = []
    result_prob = []
    for prob1_i, value1_i in zip(prob1, value1):
        for prob2_i, value2_i in zip(prob2, value2):
            value = value1_i + value2_i
            prob = prob1_i * prob2_i
            result_value.append(value)
            result_prob.append(prob)
    unique_values = set(result_value)
    count_x = []
    for i in unique_values:
        count_x.append(result_value.count(i))
    return([result_value, result_prob])

有了这个,我可以获得结果分布:

df_added_pdfs = df.groupby(['x']).sum()
fig = px.bar(df_added_pdfs)
fig.show()

我正在寻找可以添加任何 scipy 的内置离散或连续分布函数(不仅仅是简单的统一情况)的解决方案。我想我可能错过了正确的搜索词。我认为这将是例行公事,并且 scipy 或 numpy 中会有一个函数来执行此操作。我正在寻找一个功能更优化的库。

您可以使用 convolution 个 PDF:

from scipy.stats import randint
from scipy.signal import convolve
import pandas as pd
import plotly.express as px
x_points_6 = [x+1 for x in range(6)]
x_points_20 = [x+1 for x in range(20)]

dist_6_sided = [randint.pmf(k,1,7) for k in x_points_6]
dist_20_sided = [randint.pmf(k,1,21) for k in x_points_20]

conv = convolve(dist_6_sided, dist_20_sided)
df = pd.DataFrame({'x':range(2, 26+1), 'y':conv})

df_added_pdfs = df.groupby(['x']).sum()
fig = px.bar(df_added_pdfs)
fig.show()