print('Hello world!')
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import binom, poisson

# === ベルヌーイ分布 ===
n = 1  # 試行回数(例:コインを1回投げる)
p = 0.5  # 成功確率(例:表が出る確率50%)
x = np.arange(0, n + 1)  # 成功回数(0回 or 1回)
cdf_values = binom.cdf(x, n, p)  # 累積確率(成功回数ごとの合計確率)

fig, ax1 = plt.subplots(figsize=(10, 5))
bars = ax1.bar(x, binom.pmf(x, n, p), color='lightblue', edgecolor='black', linewidth=1.2, alpha=0.7, label='f(x)=P(X=x)')
for bar, pmf_value in zip(bars, binom.pmf(x, n, p)):
    ax1.text(bar.get_x() + bar.get_width() / 2, bar.get_height(), f'{pmf_value:.3f}', ha='center', va='bottom')  # 各バーの上に確率を表示
ax1.set_title(f'PMF of Bernoulli Distribution (n={n}, p={p})')  # グラフのタイトル
ax1.set_xlabel('Number of Successes')  # 横軸ラベル
ax1.set_ylabel('Probability: f(x)')  # 縦軸ラベル
ax1.legend()
ax1.grid(True)
plt.show()

fig, ax2 = plt.subplots(figsize=(10, 5))
for i, cdf_value in enumerate(cdf_values):
    marker_color = 'white' if i < len(cdf_values) - 1 else 'black'
    if i > 0:
        ax2.hlines(cdf_values[i - 1], i - 1, i, color='navy', linewidth=2)  # 横線で確率の変化を示す
    if i < len(cdf_values) - 1:
        ax2.hlines(cdf_value, i, i + 1, color='navy', linewidth=2)
        ax2.plot(i + 1, cdf_value, 'o', markersize=8, markerfacecolor=marker_color, markeredgecolor='black')  # 白抜き or 黒丸のマーカー
    ax2.text(i + 0.5, cdf_value, f'{cdf_value:.3f}', ha='center', va='bottom')  # 確率値を表示
ax2.set_title(f'CDF of Bernoulli Distribution (n={n}, p={p})')  # グラフのタイトル
ax2.set_xlabel('Number of Successes')  # 横軸ラベル
ax2.set_ylabel('Cumulative Probability: F(x)')  # 縦軸ラベル
ax2.set_xticks(x)
ax2.set_yticks(np.arange(0, 1.1, 0.1))
ax2.grid(True)
plt.show()

# === 二項分布 ===
n = 5  # 試行回数(例:サイコロを5回振る)
p = 1 / 6  # 成功確率(例:1の目が出る確率)
x = np.arange(0, n + 1)  # 成功回数(0回~5回)
cdf_values = binom.cdf(x, n, p)  # 累積確率(成功回数ごとの合計確率)

fig, ax3 = plt.subplots(figsize=(10, 5))
bars = ax3.bar(x, binom.pmf(x, n, p), color='lightblue', edgecolor='black', linewidth=1.2, alpha=0.7, label='f(x)=P(X=x)')
for bar, pmf_value in zip(bars, binom.pmf(x, n, p)):
    ax3.text(bar.get_x() + bar.get_width() / 2, bar.get_height(), f'{pmf_value:.4f}', ha='center', va='bottom')  # 各バーの上に確率を表示
ax3.set_title(f'PMF of Binomial Distribution (n={n}, p={p})')  # グラフのタイトル
ax3.set_xlabel('Number of Successes (e.g., getting 1 on a die)')  # 横軸ラベル
ax3.set_ylabel('Probability: f(x)')  # 縦軸ラベル
ax3.legend()
ax3.grid(True)
plt.show()

fig, ax4 = plt.subplots(figsize=(10, 5))
for i, cdf_value in enumerate(cdf_values):
    marker_color = 'white' if i < len(cdf_values) - 1 else 'black'
    if i > 0:
        ax4.hlines(cdf_values[i - 1], i - 1, i, color='navy', linewidth=2)  # 横線で確率の変化を示す
    if i < len(cdf_values) - 1:
        ax4.hlines(cdf_value, i, i + 1, color='navy', linewidth=2)
        ax4.plot(i + 1, cdf_value, 'o', markersize=8, markerfacecolor=marker_color, markeredgecolor='black')  # 白抜き or 黒丸のマーカー
    ax4.text(i + 0.5, cdf_value, f'{cdf_value:.4f}', ha='center', va='bottom')  # 確率値を表示
ax4.set_title(f'CDF of Binomial Distribution (n={n}, p={p})')  # グラフのタイトル
ax4.set_xlabel('Number of Successes (e.g., getting 1 on a die)')  # 横軸ラベル
ax4.set_ylabel('Cumulative Probability: F(x)')  # 縦軸ラベル
ax4.set_xticks(x)
ax4.set_yticks(np.arange(0, 1.1, 0.1))
ax4.grid(True)
plt.show()

# === ポアソン分布 ===
lambda_parameter = 2.5  # ある一定時間内に発生するイベントの平均回数(例:サッカーの試合でのゴール数)
x_values = np.arange(0, 10, 1)  # 発生するイベントの回数(0回~9回)
pmf_values = poisson.pmf(x_values, lambda_parameter)  # 各回数ごとの確率を計算
cdf_values = poisson.cdf(x_values, lambda_parameter)  # 累積確率を計算(この回数以下の合計確率)

fig, ax5 = plt.subplots(figsize=(10, 5))
bars = ax5.bar(x_values, pmf_values, color='blue', alpha=0.7, edgecolor='black', label='f(x)=P(X=x)')
for bar, pmf in zip(bars, pmf_values):
    ax5.text(bar.get_x() + bar.get_width()/2, bar.get_height(), f'{pmf:.3f}', ha='center', va='bottom')  # 各バーの上に確率を表示
ax5.set_title(f'PMF of Poisson Distribution (λ={lambda_parameter})')  # グラフのタイトル
ax5.set_xlabel('Number of Events (e.g., Soccer Goals)')  # 横軸ラベル
ax5.set_ylabel('Probability: f(x)')  # 縦軸ラベル
ax5.legend()
ax5.grid(True)
plt.show()

fig, ax6 = plt.subplots(figsize=(10, 5))
for i, cdf in enumerate(cdf_values):
    marker_color = 'white' if i < len(cdf_values) - 1 else 'black'
    if i > 0:
        ax6.hlines(cdf_values[i - 1], x_values[i - 1], x_values[i], color='navy', linewidth=2)  # 横線で確率の変化を示す
    if i < len(cdf_values) - 1:
        ax6.hlines(cdf, x_values[i], x_values[i + 1], color='navy', linewidth=2)
        ax6.plot(x_values[i + 1], cdf, 'o', markersize=8, markerfacecolor=marker_color, markeredgecolor='black')  # 白抜き or 黒丸のマーカー
    ax6.text(x_values[i] + 0.5, cdf, f'{cdf:.3f}', ha='center', va='bottom')  # 確率値を表示
ax6.set_title(f'CDF of Poisson Distribution (λ={lambda_parameter})')  # グラフのタイトル
ax6.set_xlabel('Number of Events (e.g., Soccer Goals)')  # 横軸ラベル
ax6.set_ylabel('Cumulative Probability: F(x)')  # 縦軸ラベル
ax6.set_xticks(x_values)
ax6.set_yticks(np.round(np.linspace(0, 1, 11), 2))
ax6.grid(True)
plt.show()

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: