確率とベイズの定理

Pythonで学ぶBayesianデータ分析

Michal Oleszak

Machine Learning Engineer

確率論

  • 不確実性の表現。
  • 0〜1の数。
    • P = 0   →   不可能
    • P = 1   →   確実
    • P = 0.5   →   五分五分

 

  • P(明日雨) = 0.75   →   明日が雨の確率75%
Pythonで学ぶBayesianデータ分析

確率の法則

加法則

  • A または B(独立)の確率
  • OR = 加算
  • サイコロで2または4を出す確率

P(2 or 4) = 1/6 + 1/6 = 0.33333... = 33.3%

乗法則

  • A かつ B(独立)の確率
  • AND = 乗算
  • サイコロで2の後に4を出す確率

P(2 and 4) = 1/6 * 1/6 = 0.02777... = 2.8%

Pythonで学ぶBayesianデータ分析

条件付き確率

  • 別の事象が起きたという前提での、ある事象の確率。
  • P(A | B)

 

  • P(orange) = 2/3   →   無条件
  • P(blue) = 1/3   →   無条件

 

  • P(blue | orange) = 1/2   →   条件付き
  • P(orange | blue) = 1   →   条件付き

箱の中に3つの玉の模式図。オレンジが2つ、青が1つ。

Pythonで学ぶBayesianデータ分析

ベイズの定理

  • 既知の確率から条件付き確率を計算する方法。

 

$$P(A|B) = \frac{P(B|A) * P(A)}{P(B)}$$

Pythonで学ぶBayesianデータ分析

ベイズの定理

  • 既知の確率から条件付き確率を計算する方法。

 

$$P(\text{accident}|\text{slippery}) = \frac{P(\text{slippery}|\text{accident}) * P(\text{accident})}{P(\text{slippery})}$$

road_conditions.head()
     accident   slippery
0       False       True
1        True       True
2       False      False
3       False      False
4       False      False
Pythonで学ぶBayesianデータ分析

ベイズの定理の実装

 

$$P(\text{accident}|\text{slippery}) = \frac{P(\text{slippery}|\text{accident}) * P(\text{accident})}{P(\text{slippery})}$$

# 事故の無条件確率
p_accident = road_conditions["accident"].mean()  # 0.0625


# 路面が滑りやすい無条件確率 p_slippery = road_conditions["slippery"].mean() # 0.0892
# 事故があるときに路面が滑りやすい確率 p_slippery_given_accident = road_conditions.loc[road_conditions["accident"]]["slippery"].mean() # 0.7142
# 路面が滑りやすいときの事故の確率 p_accident_given_slippery = p_slippery_given_accident * p_accident / p_slippery # 0.5
Pythonで学ぶBayesianデータ分析

練習してみましょう!

Pythonで学ぶBayesianデータ分析

Preparing Video For Download...