In this lecture, we will be talking about Simulating MA (2), moving average of order two process. Objectives are to simulate a moving average process and interpret correlogram of a Moving average process. So let's remember, moving average process of order two for example, Xt depends on the noise two days back or two step back. It depends on Zt, Zt minus 1, zt minus 2. All of these are identical distributed independent, normal random variables with some mean and variance. And Zt minus 1 and Zt minus 2 has some weight on it, theta1 and theta2. So in this simulation, we're going to use standard normal distribution for our noise. So Xt = Zt + 0.7 Zt- 1 + 0.2 Zt- 2 and all of these noises are standard normal distribution. So have here are up and running, we can use a shell but for this simulation, I would like to open a document editor and write the code on the editor. So you can do it by just going to the file and say a new document and once you push new document, there's a new document opened up. You can write our code here and we can WAN the code but I have already opened up a document and write some piece of code here. So let's go step by step here. At the beginning these are my comments. Comments always starts with a number sign. So I'm generate noise, remember, noise are coming from normal distribution. I'm going to generate 10,000 dat point from standard normal distribution. I'm going to call MA2. This is my variable. I'm going to introduce that variable into the RM, I'm going to call it null and here with this loop, I am actually getting a rating MA2 process. So, syntax for loop in R is that 4I, I being the index in the range. My range will start from three to 10,000. Remember, my MA and MA2 process. My Xt values will depend on Ztn, Zt minus one and Zt minus two, so I'm going to use Z1 and Z2. That's why I need to start from X3. So I am starting from index three. This is my noise from right now, noise from yesterday, noise from two days ago. And I have appropriate weights on them, 0.7 and 0.2, that would generate a data set where the first two points are naught. So the first two points are missing. So I'm going to shift it, so I'm going to shift all of it by two units to the left. I'm going to call MA2 from 3 to the n, all of these basically shifted. Once I shift it I'm going to call a new process, a moving average process. Remember, this moving average process still is just a data point. It does not have time series structure on it. So let's put some time structure on this vanilla data. I have this data, I'm using ts operator, which will make it a time series. And I'm going to put it exactly back into the moving average process. Now I truly have a moving average process. Now it's time to plot this moving average process and at its column graph. So before I do that, I'm going to do this partition. I'm going to partition my multiple graphics. My graphs was going to be basically one frame. My frame's going to have a multi-frame. Basically I'm going to have two rows. And each row is going to be the plot and the ACF and to do this, I'm going to use this par routine. So par(mfrow), this stands for multi frame row. And if I put 2, 1, this means two rows and one columns. Now I can plot. I'm going to plot my moving average process and I'm going to plot my ACF. I put a title for both of them and I am going to color my plot to blue in the first row. So I do have it, I can save it using dot our extension. And all I have to do, if you are Mac, you just do command E. That will execute the source code. If you are on Windows, I think you will do CTRL + E. So if I did Command E, basically it just go to the shelve, runs it on the shelve, and gives you the output. Here I have a moving average process of order 2. Without analyzing it, it would be impossible to tell this is actually moving average process. But here is the thing. I do have my ACF and ACF has some particular structure. I realize that I have a correlation at lag 0. This is always the case. It always starts ACF one but then there's a high correlation with lag one. This makes sense because we are still getting noises from the previous step. And there's also noise coming from the two steps back, two days back and then boom, there's nothing. So this is very, very much a characteristic of MAQ process if you are looking at MAQ process. If you take ACF off MA q process, it lags or the correlation will cut off at LAGQ. In this case, we have MA2 process and then ACF has to cut from lag 2. And this is lag 1, this is lag 2, boom, I have nothing. So this is one of the ways we're going to model our time series later on. You're going to look at ACF and if you see an ACF cuts off after some lag, that gives us a reason to model our data using a moving average process. What have you learned in this lecture? You have learned how to simulate the moving average processes in R. And you have learned that ACF or moving average process of all the q cuts off at lag q.