난수 생성기와 해커 통계

Python으로 하는 Statistical Thinking (파트 1)

Justin Bois

Teaching Professor at the California Institute of Technology

해커 통계

  • 반복 측정을 시뮬레이션해 확률을 계산합니다.
Python으로 하는 Statistical Thinking (파트 1)

  ch3-2.004.png

1 이미지: 작가 미상
Python으로 하는 Statistical Thinking (파트 1)

ch3-2.005.png

1 이미지: Heritage Auction
Python으로 하는 Statistical Thinking (파트 1)

동전 던지기 시뮬레이션

ch3-2.010.png

Python으로 하는 Statistical Thinking (파트 1)

베르누이 시행

  • 두 가지 결과가 있는 실험: "성공"(True), "실패"(False)
Python으로 하는 Statistical Thinking (파트 1)

np.random 모듈

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

rng
Generator(PCG64) at 0x7F9433D38120
Python으로 하는 Statistical Thinking (파트 1)

난수 시드

  • 난수 알고리즘에 입력되는 정수
  • 재현이 필요할 때만 난수 생성기에 시드 설정
  • rng = np.random.default_rng(seed)로 지정
Python으로 하는 Statistical Thinking (파트 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
Python으로 하는 Statistical Thinking (파트 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
Python으로 하는 Statistical Thinking (파트 1)

해커 통계로 확률 구하기

  • 시뮬레이션 방법 결정
  • 매우 많이 반복 시뮬레이션
  • 관심 결과가 나온 시행의 비율 ≈ 확률
Python으로 하는 Statistical Thinking (파트 1)

연습해 봅시다!

Python으로 하는 Statistical Thinking (파트 1)

Preparing Video For Download...