The Law of Large Numbers
You learned in the previous exercise that due to the stochastic nature of Monte Carlo simulations, each simulation result can be very different. In this exercise, you'll leverage the Law of Large Numbers to simulate inflation in 2050 based on the average of a large number of simulations.
The monte_carlo_inflation()
function you wrote in the previous exercise is available for use. As a reminder, this is the function code:
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)
The numpy
and random
packages have been imported for you.
This exercise is part of the course
Monte Carlo Simulations in Python
Exercise instructions
- Calculate the average of 1,000 simulations where a seed between 0 and 20,000 is randomly chosen each time.
- Calculate the average of 10,000 simulations where a seed between 0 and 20,000 is randomly chosen each time.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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))