LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Monte Carlo Simulations in Python

Kurs anzeigen

Anleitung zur Übung

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

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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 bearbeiten und ausführen