
二項分布を表示するコードを紹介します。
二項分布 $B(n, p)$ の試行回数 $n$ と成功確率 $p$ を変えて、分布の形の違いを観察しました。
二項分布のコード【Python】
二項分布のPythonコード
次のコードを入力すれば、二項分布を表示できます。
「二項分布の情報」の $n$ と $p$ の数値を変えて、任意の二項分布が作成できます。
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom
from scipy.stats import norm
# 二項分布の情報
n = 100 # Number of trials
p = 0.3 # Probability of success
# 二項分布を表示
x_values = np.arange(0, n + 1)
binomial_probs = binom.pmf(x_values, n, p)
plt.bar(x_values, binomial_probs, color='y', alpha=0.7, label = "Binomial-distribution")
# Other Informations
plt.legend() #凡例表示
plt.xlabel("Number of Successes")
plt.ylabel("Probability")
plt.title(f"Binomial Distribution (n={n}, p={p})")
plt.grid(True)
plt.show()
二項分布の表示結果

【観察】二項分布の試行回数の変更
観察開始(Pythonコード)
試行回数 $n$ を変えて、二項分布 $B(n, 0.3)$ の形状を観察しました。
試行結果
試行回数 $n$ が 1から5までの間の二項分布を並べて観察しましょう。
試行回数が少ないときの二項分布





最後に、試行回数が $n=100$ のとき(再掲)と、$n=1000$ のときの二項分布の形状を掲載します。
$n=100$ の二項分布

$n=1000$ の二項分布

二項分布って、こんなものです!