Hi. In this video, we're going to talk about moving averages and how they're used with time series data. Moving averages are not that difficult to work with, recall the formula for an average is simply the sum of xi over i equaling 1 to n. So add them all up and then you divide by n, the number of elements. A moving average is similar to a regular average. The k-period moving average. K is the number of periods of a series. We just take the average of k elements, then we'd just take the consecutive values. What this does is smooths out any fluctuations so that we have a better understanding of what's going on in the data. Here the formula for a simple moving average. The simple moving average, basically is almost identical and calculation to a regular average except that it takes a series of values for k-periods as opposed to the whole set. So what do I mean by that? Recall the formula for a regular average xi and i is equal 1 to n. So you're adding up all your x's and then you divide by n. So if it's the height of players on a basketball team, you add up all the heights in that column and then you divide by the number of players n. Similarly in a moving average, you take k-periods. If you look at the formula here for the simple moving average, you still see that summation. But it doesn't go from 1 to n, it goes from this t minus k plus 1 to t. Then for the number of periods. So say you're doing a moving average for three periods. K would be three for three periods. What is this t minus k plus 1 thing? So say you have some time series data and here's something for period 1, period 2, period 3, 4, 5, 6, and you're doing k is equal to 3. So you're going to do a moving average on 3. You're going to take these three numbers for period 4, 5 and 6 and divide by 3. If you look at the indexes, that means you're taking x at sub period 6, x at sub period 5, and x at sub period 4. So 6 which is the current period t minus 3 would give you 3. This number here. But you want to start at 4. So that's why you have this plus 1 value here. Another way to think about it. If you are maybe playing golf and you're sitting at the fifth hole. How many holes you have left to play? We have the fifth hole, the sixth hole, the seventh hole, eighth hole, and the ninth hole, and nine more. So that's where that plus 1 goes in. So let's take a couple of examples. Here is calculating the simple moving average for say three lags, k is equal to 3. Here, you can think of it as a sliding window. We use those three numbers, we add them up and divide by 3. I've already done the calculation, we get 20. If we add these three numbers and divide by 3, we get 15, take these three numbers and we get 16, and so on and so forth. That's your moving average. Another way to think about a moving average is that, it slides down this window of three values or window of k values, slides down as you go through the time series. A related moving average, this is called a lagging moving average, just called a centered moving average where if you're at time t taken the past k values, for example, if you're at time 10 and you take the previous three values, k is equal to 3, that's one way of doing it. Another way of doing it is to center. Let me erase the ink. Erase all ink on slide. Another way to think about is, if you're going to calculate the moving average for three lags centered, then you'd find that middle point here and then calculate that moving average. If you use this as a moving center point, then this would be the moving outwards. That's called a centered moving average. If you're interested, the formula for that is your moving average is equal to 1 over 2k plus 1. The summation, y of t plus j, and j ranges from minus k to plus k. This 2k plus 1, if you study that formula and get a little hairy, but basically what it's saying is there's k, there's that centered value, and there's k more. That's equal to 2k plus 1. So if you're doing a moving average with three centered around this value, that's that here and then one lag here and one lag on this side, and that's where that 2k comes from. Don't worry about it for this class but you should know that there are other types of moving averages. So I leave it to you to look at this spreadsheet and try to calculate the moving average for five flags. That's also useful. I've printed the answers here for the three lagged moving average. One thing to note about moving averages is that they're not very good with data with a trend, so may be its cycles and it's cycling up or there's some seasonal patterns to the data. For example, there might be a spike in sales around the Christmas holidays. There's always a spike of sales at that time. But it is good for understanding the data and smoothing it out and understanding the general pattern of the data.