Are Stock Prices a Random Walk?
Most stock prices follow a random walk (perhaps with a drift). You will look at a time series of Amazon stock prices, pre-loaded in the DataFrame AMZN
, and run the 'Augmented Dickey-Fuller Test' from the statsmodels library to show that it does indeed follow a random walk.
With the ADF test, the "null hypothesis" (the hypothesis that we either reject or fail to reject) is that the series follows a random walk. Therefore, a low p-value (say less than 5%) means we can reject the null hypothesis that the series is a random walk.
This exercise is part of the course
Time Series Analysis in Python
Exercise instructions
- Import the
adfuller
module from statsmodels. - Run the Augmented Dickey-Fuller test on the series of closing stock prices, which is the column
'Adj Close'
in theAMZN
DataFrame. - Print out the entire output, which includes the test statistic, the p-values, and the critical values for tests with 1%, 10%, and 5% levels.
- Print out just the p-value of the test (
results[0]
is the test statistic, andresults[1]
is the p-value).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import the adfuller module from statsmodels
from statsmodels.tsa.stattools import adfuller
# Run the ADF test on the price series and print out the results
results = adfuller(___)
print(results)
# Just print out the p-value
print('The p-value of the test on prices is: ' + str(results[___]))