Get startedGet started for free

Get the Drift

In the last exercise, you simulated stock prices that follow a random walk. You will extend this in two ways in this exercise.

  • You will look at a random walk with a drift. Many time series, like stock prices, are random walks but tend to drift up over time.
  • In the last exercise, the noise in the random walk was additive: random, normal changes in price were added to the last price. However, when adding noise, you could theoretically get negative prices. Now you will make the noise multiplicative: you will add one to the random, normal changes to get a total return, and multiply that by the last price.

This exercise is part of the course

Time Series Analysis in Python

View Course

Exercise instructions

  • Generate 500 random normal multiplicative "steps" with mean 0.1% and standard deviation 1% using np.random.normal(), which are now returns, and add one for total return.
  • Simulate stock prices P:
    • Cumulate the product of the steps using the numpy .cumprod() method.
    • Multiply the cumulative product of total returns by 100 to get a starting value of 100.
  • Plot the simulated random walk with drift.

Hands-on interactive exercise

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

# Generate 500 random steps
steps = np.random.normal(loc=___, scale=___, size=___) + ___

# Set first element to 1
steps[0]=1

# Simulate the stock price, P, by taking the cumulative product
P = ___ * np.cumprod(___)

# Plot the simulated stock prices
plt.plot(___)
plt.title("Simulated Random Walk with Drift")
plt.show()
Edit and Run Code