대수의 법칙
이전 연습 문제에서 Monte Carlo 시뮬레이션은 확률적 성격 때문에 실행할 때마다 결과가 크게 달라질 수 있다는 점을 배웠어요. 이번에는 대수의 법칙을 활용해, 아주 많은 시뮬레이션의 평균을 바탕으로 2050년의 인플레이션을 모의해 보겠습니다.
이전 연습 문제에서 작성한 monte_carlo_inflation() 함수를 그대로 사용할 수 있어요. 참고로 함수 코드는 다음과 같습니다:
def monte_carlo_inflation(year, seed):
random.seed(seed)
inflation_rate = 8.6
yearly_increase = random.randint(1, 3)
for i in range(year - 2022):
inflation_rate = inflation_rate * ((100 + yearly_increase)/100)
return(inflation_rate)
numpy와 random 패키지는 이미 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Monte Carlo 시뮬레이션
연습 안내
- 매번 0 이상 20,000 이하의 시드를 무작위로 선택해 총 1,000번 시뮬레이션을 수행하고, 그 평균을 계산하세요.
- 매번 0 이상 20,000 이하의 시드를 무작위로 선택해 총 10,000번 시뮬레이션을 수행하고, 그 평균을 계산하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Calculate the average of 1,000 simulation results with a seed between 0 and 20000
rates_1 = []
for i in range(____):
seed = random.randint(____, ____)
rates_1.append(monte_carlo_inflation(2050, ____))
print(np.mean(rates_1))
# Calculate the average of 10,000 simulation results with a seed between 0 and 20000
rates_2 = []
for i in range(____):
seed = random.randint(____, ____)
rates_2.append(monte_carlo_inflation(2050, ____))
print(np.mean(rates_2))