Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Monte Carlo Simulations in Python

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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))
Code bewerken en uitvoeren