Get startedGet started for free

Monte Carlo simulations

Monte-Carlo simulations are used to model a wide range of possibilities.

Monte-Carlos can be constructed in many different ways, but all of them involve generating a large number of random variants of a given model, allowing a wide distribution of possible paths to be analyzed. This can allow you to build a comprehensive forecast of possibilities to sample from without a large amount of historical data.

Generate 100 Monte-Carlo simulations for the USO oil ETF.

The parameters mu, vol, T, and S0 are available from the previous exercise.

This exercise is part of the course

Introduction to Portfolio Risk Management in Python

View Course

Exercise instructions

  • Loop from 0 to 100 (not including 100) using the range() function.
  • Call the plotting function for each iteration using the plt.plot() function, passing the range of values T (range(T)) as the first argument and the forecasted_values as the second argument.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Loop through 100 simulations
for i in ____:

    # Generate the random returns
    rand_rets = np.random.normal(mu, vol, T) + 1
    
    # Create the Monte carlo path
    forecasted_values = S0*(rand_rets).cumprod()
    
    # Plot the Monte Carlo path
    plt.plot(____, ____)

# Show the simulations
plt.show()
Edit and Run Code