Now that you have an initial understanding of the the White Noise and the Random Walk model, let's try to generate some examples!
You will be able to:
- Explain what the goal is of time series modeling
- Understand and explain what a White Noise model is
- Understand and explain what a Random Walk model is
- Understand the mathematical formulations of Random Walk and White Noise models
To get a good sense of how a model works, it is always a good idea to generate a process. Let's consider the following example:
- Every day in August, September and October 2018, Nina takes the subway to work. Let's ignore weekends for now and assume that Nina works every day.
- We know that on average, it takes her 25 minutes, and the standard deviation is 4 minutes.
- Create and visualize a time series that reflects this information.
Let's import pandas, numpy and matplotlib.
np.random.seed(12) # do not change this seed.
Create the dates. You can do this using pd.date_range
, more info here.
# your code here
Generate the values for the wite noise process representing Nina's commute in August and September.
# your code here
Create a time series with the dates and the commute times.
# your code here
Visualize the time series and set appropriate axis labels.
# your code here
Print Nina's shortest and longest commute.
# your code here
# your code here
Look at the distribution of commute times.
# your code here
Compute the standard deviation and the mean of the commute series. The fact that the mean and standard error are constant over time is crucial!
# your code here
# your code here
Now, let's look at the mean and standard error for August and October respectively.
# your code here
Because you've generated this data, you know that the mean and constant will be the same over time. But comparing mean and standard deviation over time is useful practice for real data examples to check if a process is White Noise!
Remember from the lecture that the Random Walk Model:
- Has no specified mean or variance
- Has a strong dependence over time
Mathematically, this can be written as:
Because today's value depends on yesterday's, you need a starting value when you start off your time series. In practice, this is what the first few time series values look like:
$$ Y_0 = \text{some specified starting value}$$
Keeping this in mind, let's create a random walk model:
- starting from a value of 1000 USD of a share value upon a company's first IPO (initial public offering) in 2010 until end of November of the same year, generate a random walk model with a white noise error term, which has a standard error of 10.
# keep the random seed
np.random.seed(11)
# create a series with the specified dates
Visualize the time series with correct axis labels
# your code here
You can see how this very much looks like the exchange rate series you looked at in the lecture!
Repeat the above, but include a drift parameter
# keep the random seed
# your code here
Note that there is a very strong drift here!
One important property of the Random Walk model is that a differenced random walk returns a white noise. This is a result of the mathematical formula:
and we know that
Plot the differenced time series for the shares time series (no drift).
# your code here
This does look a lot like a white noise series!
Plot the differenced time series for the shares time series (with a drift).
# your code here
This is also a white noise series, but what can you tell about the mean?
The mean is equal to the drift
Great, you now know how a Random Walk and Moving Average model work!