ตัวสร้างเลขสุ่มและ Hacker Statistics

Statistical Thinking in Python (ตอนที่ 1)

Justin Bois

Teaching Professor at the California Institute of Technology

Hacker Statistics

  • ใช้การวัดซ้ำแบบจำลองเพื่อคำนวณความน่าจะเป็น
Statistical Thinking in Python (ตอนที่ 1)

  ch3-2.004.png

1 Image: artist unknown
Statistical Thinking in Python (ตอนที่ 1)

ch3-2.005.png

1 Image: Heritage Auction
Statistical Thinking in Python (ตอนที่ 1)

จำลองการโยนเหรียญ

ch3-2.010.png

Statistical Thinking in Python (ตอนที่ 1)

การทดลองแบบ Bernoulli

  • การทดลองที่มีสองผลลัพธ์ คือ "สำเร็จ" (True) และ "ล้มเหลว" (False)
Statistical Thinking in Python (ตอนที่ 1)

โมดูล np.random

import numpy as np
rng = np.random.default_rng()

rng
Generator(PCG64) at 0x7F9433D38120
Statistical Thinking in Python (ตอนที่ 1)

Random number seed

  • ค่าจำนวนเต็มที่ป้อนเข้าอัลกอริทึมสร้างเลขสุ่ม
  • กำหนด seed ด้วยตนเองเฉพาะเมื่อต้องการให้ผลลัพธ์ทำซ้ำได้
  • ระบุด้วย rng = np.random.default_rng(seed)
Statistical Thinking in Python (ตอนที่ 1)

จำลองการโยนเหรียญ 4 ครั้ง

rng = np.random.default_rng(42)

random_numbers = rng.random(size=4)
random_numbers
array([0.77395605, 0.43887844, 0.85859792, 0.69736803])
heads = random_numbers < 0.5
heads
array([False,  True, False, False])
np.sum(heads)
1
Statistical Thinking in Python (ตอนที่ 1)

จำลองการโยนเหรียญ 4 ครั้ง

n_all_heads = 0  # Initialize number of 4-heads trials
for _ in range(10000):
     heads = np.random.random(size=4) < 0.5
     n_heads = np.sum(heads)
     if n_heads == 4:
         n_all_heads += 1

n_all_heads / 10000
0.0607
Statistical Thinking in Python (ตอนที่ 1)

การคำนวณความน่าจะเป็นด้วย Hacker Stats

  • กำหนดวิธีจำลองข้อมูล
  • จำลองซ้ำหลายครั้ง
  • ความน่าจะเป็นคือสัดส่วนโดยประมาณของการทดลองที่ได้ผลลัพธ์ที่สนใจ
Statistical Thinking in Python (ตอนที่ 1)

มาฝึกกันเถอะ!

Statistical Thinking in Python (ตอนที่ 1)

Preparing Video For Download...