0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで学ぶ統計入門2

Posted at

復習用に確率論の一部を記載

▶️ 確率論ってなに?

確率論は、未来の「どうなるか分からないこと」を数字で整理する技術です。
ビジネスや機械学習でも、意思決定のベースになるスキルです!


▶️ Pythonで実践する確率論

1. 基本の確率計算

コイン投げの確率をシミュレーションしてみる

式:

$$
P(\text{heads}) = \frac{\text{表が出た回数}}{\text{総回数}}
$$

  • $P(\text{heads})$:表が出る確率
  • 表が出た回数を総回数で割るだけ!
import random

# コインを1000回投げたときに表が出る割合を確認
results = [random.choice(['heads', 'tails']) for _ in range(1000)]
prob_heads = results.count('heads') / len(results)
print("表が出る推定確率:", prob_heads)

実践ポイント: 理論上0.5だけど、実験では少しブレる!


2. 条件付き確率を使うシナリオ

式:

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

  • $P(A|B)$:Bが起きたときにAも起きる確率
  • $P(A \cap B)$:AかつBが同時に起きる確率
  • $P(B)$:Bが起きる確率

例: 商品購入者のうち、メルマガ登録している人の割合

# データ仮定
p_purchase_and_register = 0.1
p_register = 0.3

# 購入した人がメルマガ登録している確率
p_purchase_given_register = p_purchase_and_register / p_register
print("メルマガ登録者が購入する確率:", p_purchase_given_register)

実践ポイント: 何か起きた「後」の確率を考えるときに使う!


3. 全体確率を使ったシナリオ

式:

$$
P(B) = \sum_{i} P(B|A_i) \times P(A_i)
$$

  • $P(B)$:最終的にBが起きる確率
  • $P(A_i)$:それぞれのパターン($A_i$)が起きる確率
  • $P(B|A_i)$:パターン$A_i$のもとでBが起きる確率

例: 誰でもいいから陽性になる確率を計算する

# 仮定:
p_disease = 0.01  # 病気である確率
p_healthy = 0.99  # 健康である確率
p_positive_given_disease = 0.99  # 病気の人が陽性になる確率
p_positive_given_healthy = 0.05  # 健康な人が陽性になる確率

# 全体確率の計算
p_positive = (p_positive_given_disease * p_disease) + (p_positive_given_healthy * p_healthy)
print("陽性になる全体確率:", p_positive)

実践ポイント: 複数の原因があるときは、それぞれの寄与を合計する!


4. ベイズの定理を使った予測アップデート

式:

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

  • $P(A|B)$:Bが起きたときにAが真である確率(事後確率)
  • $P(B|A)$:Aが真であるときにBが起きる確率(尤度)
  • $P(A)$:Aが起きる確率(事前確率)
  • $P(B)$:Bが起きる確率(正規化項)

例: 病気検査の結果から本当に病気か推定する

# 仮定:
p_disease = 0.01
p_positive_given_disease = 0.99
p_positive_given_healthy = 0.05
p_healthy = 1 - p_disease

# 陽性の全体確率(全体確率を使う)
p_positive = (p_positive_given_disease * p_disease) + (p_positive_given_healthy * p_healthy)

# ベイズの定理による事後確率
p_disease_given_positive = (p_positive_given_disease * p_disease) / p_positive
print("陽性反応の人が実際に病気である確率:", p_disease_given_positive)

実践ポイント: 背景にある確率(全体確率)をちゃんと考慮するのが重要!


0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?