0
1

More than 3 years have passed since last update.

Pythonで理解する確率分布

Posted at

背景

最近教師あり学習を行おうとして、データが足りないというケースに遭遇することが増えてきたので、データがなくても学習してくれる強化学習に関心を持ちました。

環境

Google Colaboratoryで実行しました。

参考

「仕事で始める機械学習第2版」268頁~270頁
この書籍では6目のサイコロを2回振った値(2D6)が5以下になる確率を計算していました。

この記事では12目のサイコロを2回振った場合に合計が10以下になる確率を計算していきたいと思います。

googlecolab
import numpy as np
import scipy.stats 
import matplotlib.pyplot as plt
import japanize_matplotlib

n = 1000000 #サイコロを振る回数の指定
dice_total = np.random.randint(1,13, size=n) #1個目のサイコロを振る
dice_total +=np.random.randint(1,13, size=n) #2個目のサイコロを振る
x, y = np.unique(dice_total, return_counts=True)
y = y/n #カウント値を出現確率に変換
plt.plot(x, y, marker="o", label="2D12 PMF")
plt.legend()
plt.xlabel("2つのサイコロの出目の合計")
plt.ylabel("出現確率")
plt.show()

サイコロの出目の合計値の分布はこのようになりました。
2つのダイスの出目の合計.png

今回の条件は10以下の場合なので、ここからは2,3,4,5,6,7,8,9,10が出たパターンの累積和を求めていきます。

googlecolab
cumsum_y = np.cumsum(y)
print(cumsum_y)
plt.plot(x ,cumsum_y, marker="o", label="2D12 CDF")
plt.legend()
plt.xlabel("2つのサイコロの出目の合計")
plt.ylabel("出現確率の累積和")
output
[0.006789 0.020778 0.041668 0.0695   0.103853 0.145727 0.19453  0.250322
 0.312429 0.382303 0.458935 0.542448 0.618804 0.68793  0.750322 0.805665
 0.854397 0.895944 0.930564 0.958424 0.979276 0.99311  1.      ]

2つのサイコロの出目の合計(累積和).png

リストの中身を見てみると1が出る可能性は0%なので0と表示されています。
左から順番にリストを確認していき、インデックス[9]つまり10の場合の累積和は0.312429となっています。
よって31.2%ということが分かります。

一応理論値を出してみました。サイコロの理論値2~10まで
1/144+2/144+3/144+4/144+5/144+6/144+7/144+8/144+9/14=0.3125

ということで合っていました。

まとめ

確率分布の概念についてサイコロの出目を題材にサイコロを用いて計算してみました。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1