📅  最后修改于: 2023-12-03 14:56:39.223000             🧑  作者: Mango
本主题是关于第12类RD Sharma解决方案中二项分布章节中练习33.1的解答。该练习要求学生完成一组二项分布的图表,并计算相应的概率分布和累积概率分布。
本程序旨在为学生提供对练习33.1的解答,它包含了二项分布图表生成及相应概率计算的功能。程序使用Python编程语言,为了便于使用和交流,代码片段中的代码已经进行了注释。
Python 3.0及以上版本
import matplotlib.pyplot as plt
from math import factorial
def binomial_coefficient(n, k):
"""计算二项式系数"""
return factorial(n) // (factorial(k) * factorial(n-k))
def binomial_distribution(p, n):
"""计算二项分布概率分布"""
q = 1 - p
probabilities = []
for k in range(n+1):
probability = binomial_coefficient(n, k) * p**k * q**(n-k)
probabilities.append(probability)
return probabilities
def cumulative_distribution(probabilities):
"""计算二项分布累积概率分布"""
cumulative_probabilities = [sum(probabilities[:i+1]) for i in range(len(probabilities))]
return cumulative_probabilities
def draw_binomial_distribution(num_trials, probability):
"""绘制二项分布概率图表"""
probabilities = binomial_distribution(probability, num_trials)
cumulative_probabilities = cumulative_distribution(probabilities)
# 概率分布图表
plt.subplot(121)
plt.bar(range(num_trials+1), probabilities)
plt.xlabel('Number of successes')
plt.ylabel('Probability')
plt.title('Binomial Distribution PMF')
# 累积概率分布图表
plt.subplot(122)
plt.plot(range(num_trials+1), cumulative_probabilities)
plt.xlabel('Number of successes')
plt.ylabel('Cumulative Probability')
plt.title('Binomial Distribution CDF')
plt.show()
# 绘制一个(n,p) = (10, 0.6)的二项分布概率图表
draw_binomial_distribution(10, 0.6)
程序将输出一个二项分布概率图表和一个累积概率分布图表,以及相应的数值计算结果。