In this lecture, we are going to look at how to work with the simple moving average in code.
This lecture is going to walk you through a Prepare to CoLab notebook.
Although a very good exercise, which I always recommend, is once you know how this is done, to try
and recreate it yourself with as few references as possible.
As always, you can check the lectures, how to code by yourself and how to practice for a more in-depth
discussion.
If there's anything in this lecture you didn't understand or you think I missed the step or didn't explain
why we were doing something, please use the Q&A to inquire.
As always, you can look at the title of the notebook to determine what notebook we are currently looking
at.
So let's start by downloading the file esp five closed at CSC.
As you recall, this is our CSC of Close Presses Only.
Next, we're going to import our usual libraries, pandas, map, plot, lib and numpy.
Next we read in the csv as a dataframe using PD to read CC.
Next we grab the clothes prices for Google since we're going to drop the end values.
We want to make a copy first.
Will assign this to the variable goog x.
We'll do a google dot head as a sanity check to see what's in our data frame.
Next, we'll do a Google plot to see Google stock prices as a time series.
Next we'll calculate the log returns for Google by calling the percent change function, adding one
and then taking the log.
Next, we plot the log returns as a time series.
Next, we're going to test our moving average and code.
So on the right side, you'll see that I'm going to grab the GOOG column.
Then I call the rolling function passing in ten for the window size.
Then I call the mean function to say that I want the rolling mean.
Finally, I assign this to a new column called Smart ten.
Next we call the head function, with the argument 20 to say that we want to see the first 20 rows of
our new data frame.
As you can see, the first nine rows of semi ten are nine.
This is because no rolling window of size ten exists for these elements.
We can see that the first value in, say, May ten shows up in the 10th row as expected.
You might want to do a manual test calculation to ensure that these values are what you expect.
That is, calculate these values by hand.
Next I do another little sanity check to see what type of object is returned by the rolling function.
As you can see, it's an object of type rolling.
This is always useful in case you need to look up the documentation, you know exactly which page to
go to.
Next I call Google Plot again so that we can see our new column plotted against the original Time series.
She?
As you can see, it's basically a smoother version of our original Time series.
You might want to zoom in on your own computer or just select a few rows to see this more clearly.
Next, we're going to calculate another a simple moving average, but this time with a window size of
50.
As you can see, this is the exact same code as before, except with 50 instead of ten.
When we plot this, we see something very interesting.
First, smart 50.
By using more values in its sample, mean is even smoother than, say, ten.
So your lesson is that the more values you use, the smoother the resulting time series will be.
Second, you will see this lagging characteristic in the Time series.
That is, it appears as if the simple moving average lags behind the original TIME series.
If you try different window sizes, you'll see that this effect becomes more and more pronounced as
the window size gets larger and larger.
Next, we're going to work with a Multidimensional Time series.
Let's create a new data frame by using both Google and Apple stocks.
Again, we make a copy and drop any any values.
We'll call this data frame goog aapl.
On the next line.
You can see that after calling the rolling function, I have other options than just the mean.
Since we now have two columns of data, I can calculate the covariance.
As you can see, the results of this is pretty strange.
On the left hand side of this data frame, we can see a sort of multilevel index.
At the top level is just the date, but there's a sub level where we get to choose either Apple or Google.
So if you were to index this data frame by date, you would get a two by two covariance matrix.
This is kind of a weird way of storing what is really a three dimensional tensor and a two dimensional
data frame.
Next, I demonstrate how you can select a single row by date and convert it to an empire way to get
a single covariance matrix.
Now, as you know, in finance, we like to work with returns rather than prices.
So in the next block, we're going to calculate the log returns for both Apple and Google.
Notice that this is exactly the same code as before.
Since it works for one or multiple columns.
In the next block, we're going to calculate the simple moving average for both Apple and Google returns.
Notice that again it's just the same code as before.
Excepts repeated twice.
Next, we plot both Googles and Apples returns along with their moving averages.
It's interesting that there seems to be a pretty strong correlation between their returns, although
there are definitely some outliers.
Recall that we discovered this in an earlier lecture as well.
Next we calculate the rolling covariance of the returns.
To do this, we call rolling 50 and then we call the code function.
If we do a cocktail, we can see the last few rows of the covariance data frame.
Notice that if I don't pass in any arguments into the tail function, it just prints out the last five
rows, which cuts off one of the covariance matrices.
So this is a full covariance matrix.
This is a full covariance matrix.
And this is half of one.
Next we calculate the rolling correlation of the log returns.
To do this, we call rolling 50 again and then we call the core function.
If we do a core detail, we can see the last few rows of the dataframe I've passed in 16, so that we
see the last eight correlation matrices in full.
Notice how the correlation matrix changes over time?
For most of the correlation matrices, we can see that there is a negative correlation between Apple
and Google returns, at least for these time periods.
The final correlation is positive, demonstrating that the correlation can even change direction.