복원추출로 샘플링하기
부트스트래핑은 평균의 신뢰구간을 계산할 때 매우 유용합니다. 이제 직접 연습해 보세요!
nba_weights에는 NBA 선수들의 체중(킬로그램)이 담겨 있습니다:
nba_weights = [96.7, 101.1, 97.9, 98.1, 98.1,
100.3, 101.0, 98.0, 97.4]
이 리스트를 사용해 NBA 선수 평균 체중의 95% 신뢰구간을 계산해 보려고 합니다.
다음 모듈이 미리 임포트되어 있습니다: random, 그리고 numpy는 np로 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Monte Carlo 시뮬레이션
연습 안내
random.choices()를 사용해 리스트에서 키 9개를 복원추출로 1,000번 샘플링하세요.- 시뮬레이션 결과의 평균과 95% 신뢰구간을 계산하고, 신뢰구간의 하한을
lower, 상한을upper에 할당하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
simu_weights = []
# Sample nine values from nba_weights with replacement 1000 times
for i in range(____):
bootstrap_sample = ____
simu_weights.append(np.mean(bootstrap_sample))
# Calculate the mean and 95% confidence interval of the mean for your results
mean_weight = ____
upper = ____
lower = ____
print(mean_weight, lower, upper)