Plotting a moving average

We're going to be expanding on our knowledge of average by introducing the moving average. The moving average is simply the average of a subset of the data, where we have a small window in which we compute the average and then we move that window, and then we compute the average again. We keep repeating this until we've expended our data. So, in this section, we're going to take a look at creating line plots. We're going to introduce the moving average, and then we're going to plot a moving average. Let's go back to our notebook and continue from where we left off.

In the last section, the plot that we saw had a lot of + signs, and that doesn't make for an elegant graph. So, what we're going to do is to use a line plot, as demonstrated in the following example:

Now, Data2D is the formal way of introducing data to plot. There is the default which we used in the last section, but this is the formal way, which allows for a little more customization. After Data2D, you pass in parameters. The first is a list of options, Style Lines.

There is a second set of options which you can include after that, but we're going to leave that as a blank list. Finally, we put in our data, (zip [0, -1,..] aapl). This should produce the following line plot:

So, here's our line plot, uses a continuous line from start to finish. Notice how jagged it is. That's because this is a real-world dataset, and a real- world dataset is frequently messy. So, this is partly why we're introducing a moving average. What we're going to do now is introduce that moving average. Moving averages are used frequently in the analysis of a stock. For example, it's common for investors to compare a company's 50-day moving average to the 200-day moving average. As this is not a chapter on investing, we're not going to get into the details of interpreting 50- and 200-day moving averages; however, you can research those on your own. In our case, we're going to demonstrate here how to compute a 200-day moving average. These are called 200-day MAs, where MA stands for moving average. So, the first function which we will introduce is known as the movingSummovingSum is going to compute a list of sums based on the size of a window, and the size of the window is determined by the length of the first list passed to it, as demonstrated in the following screenshot:

You'll see here, in the first line, that there are two parameters being passed to the function: a list of Doubles and a list of Doubles. The first list represents the window which we are trying to sum. The second list represents the rest of the dataset. In the next line, we are saying, if there is no rest of the dataset in our first pattern, then we're just going to return a list containing the sum of the first list. But let's say that there is data in both lists. So, in the third line, you'll see that in the first list we shaved the first value off the first list, and then we computed the sum where we immediately reattached that value onto the list. We then recursively call movingSum again, where we take that first list (from which the first value has been shaved off) and then we add to the end the first value of the second list. Next, we pass the shorter version of the second list as the second parameter. As you can see, the size of the first list always stays the same. So, if the size of the first list is 3 in length, then at the end of the list it will still be 3 in length. So, let's do a quick example of how this works. If we wish to compute the moving sum of the values 1 to 10 but we have a window size of 3, then we need to set our first parameter to be 1,2,3, as shown in the following screenshot:

So, as you can see, we have the moving sum of the values from 1 to 10, but with a window size of 3. So, the sum of 1, 2 and 3 is 6; 2, 3 and 4 is 9; 3, 4 and 5 is 12; 4, 5 and 6 is 15; and so forth, all the way to the end of the list.

Now, our next function is the movingAverage function, and you can see it's longer than our movingSum, but most of this is error checking and looking for data which we should be catching, as shown in the following screenshot:

So, this function is going to take two parameters: our dataset, and then the size of the window that we want. It then passes that information on to movingSum, figures out the moving sums, and then simply divides by the window size for that particular dataset. So, we can find the moving average by using the following command:

We are computing the moving average for our values from 1 to 10 with a window size of 3. So, you can see that 6 divided by 3 is 2; 9 divided by 3 is 3; 27 divided by 3 is 9; and that's all we're doing. If we wish to compute the moving average of our Apple data, we can run the following command:

If we want a 200-day moving average, we can just say 200. Now we can plot that dataset as follows:

This command is similar to what we did earlier and we get the following output:

There we go. Remember how jagged the original dataset was? Looking at the plotting of this particular dataset, we can see how smooth it is by comparison. Moving averages will smooth the dataset. It will take the noise and kind of cover it up, and still retain most of the shape of the original dataset. In our next section, we're going to talk about how we can get publication-ready graphs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset