LoslegenKostenlos loslegen

A random walk simulation

Stochastic or random movements are used in physics to represent particle and fluid movements, in mathematics to describe fractal behavior, and in finance to describe stock market movements.

Use the np.random.normal() function to model random walk movements of the USO oil ETF with a constant daily average return (mu) and average daily volatility (vol) over the course of T trading days.

Diese Übung ist Teil des Kurses

Introduction to Portfolio Risk Management in Python

Kurs anzeigen

Anleitung zur Übung

  • Set the number of simulated days (T) equal to 252, and the initial stock price (S0) equal to 10.
  • Calculate T random normal values using np.random.normal(), passing in mu and vol, and T as parameters, then adding 1 to the values and assign it to rand_rets.
  • Calculate the random walk by multiplying rand_rets.cumprod() by the initial stock price and assign it to forecasted_values.

Interaktive Übung

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

# Set the simulation parameters
mu = np.mean(StockReturns)
vol = np.std(StockReturns)
T = ____
S0 = ____

# Add one to the random returns
rand_rets = ____ + 1

# Forecasted random walk
forecasted_values = ____

# Plot the random walk
plt.plot(range(0, T), forecasted_values)
plt.show()
Code bearbeiten und ausführen