In this lecture, we are going to discuss a very simple technique and time series analysis called the
simple moving average.
The way it works is this.
We start with a time series, then suppose we have some fixed length window.
Then at each point in the Time series, we drag the window along and we calculate the sample mean of
all the points in that window.
This is the simple moving average.
Let's do an example.
Suppose we have the numbers to zero eight three six one one six five five and let's say a window is
of size three.
So let's try to calculate the simple moving average.
The first value is going to be the average of two zero and eight, which is three point thirty three.
The second value is going to be the average of zero eight and three, which is three point sixty seven.
The third value is going to be the average of eight, three and six, which is five point six seven.
I would encourage you to try and calculate the rest of the values yourself in case you want a better
understanding of how this is done.
Note that we cannot fill in the first two values, since by definition we don't have three values in
which to calculate an average.
Therefore, this works like the financial return where we leave the values we can calculate as an.
So what is the purpose of the simple moving average, although it might seem extremely simple, it can
actually be quite useful.
For example, suppose that we need to calculate the mean and variance of a stock.
We might use these values to characterise that stock's distribution.
Well, we know that stock returns exhibit volatility clustering.
So we have at two options.
One, we can use all the values in our data set to calculate the mean and variance, or we can just
use the most recent values specified by the window size.
This might give us a better estimate, since probably the return at six months ago or six years ago
might not be relevant for the return today.
Therefore, a moving average might be more useful than an overall average.
In addition, learning the basic calculation behind the simple moving average also sets us up to learn
about the exponentially weighted moving average, which then sets us up to learn a more complicated
at times series forecasting methods.
So think of this as a stepping stone to the more advanced methods in the section.
Let's not discuss how the simple moving average will look in code for this, it's convenient to use
pandas in pandas.
We use the rolling function to specify that we want to do some calculation on a rolling window.
The input argument to this is the window size.
Note that this doesn't return the simple moving average right away, since there are actually multiple
things you might want to calculate from the rolling window.
In fact, this just returns that rolling window itself in pandas.
This is a rolling object from the rolling window return value.
We can calculate different kinds of statistics, such as the sum of the mean, the min, the max, the
variance and so forth.
If you have a rolling window of multiple columns, you can also calculate multi-dimensional statistics
such as the covariance and correlation.
In the next lecture, we will apply this code to actual data.