data['Close'].rolling(window=n).mean()
what is rolling ?
what is window =n ?
Hello Manav,
Imagine a price time series:
1–>4
2–>9
3–>11
4–>70
5–>2
6–>5
7–>6
8–>7
Do you think that in our series at time step 4, 70 in itself is a good measure of the price? It seems like an outlier or noise to me! Why do I say that? Well, look at the points around it,11 and 2. None is as high as 70. So in all likelihood 70 isn't a good measure.
So, how do we deal with this?
Well, one thing we can do is take an average of the prices right before it. Let's do that for the last three values including the price at that point in time. Why do we do this? Because we want to not rely on one single value that corresponds to that point. We want to balance out or smoothen a bit it's the deviation from what the price ought to be at that point. So, one we do this averaging, here is our new time series:
1–> no values before this so no average
2–>only one value before this so no average
3–> (4 + 9 + 11)/3 = 8
4–>(9 + 11 + 70)/3 = 30
5–>(11+ 70 + 2)/3 = 27.66
6–>(70 + 2+ 5)/3 = 26
7–>(2+ 5+ 6)/3 = 4.33
8–>(5 + 6+ 7)/3 = 6
As you can see, the noise or outlying at timestep 4 has been dealt with to some extent. It went from 70 to 30. Now here, in this case, the "window" over which we take average is 3 (window = 3). You could have set it to anything 2 or 5 etc. This style of averaging is called the rolling mean. You keep "rolling" forward while taking mean. That is the reason for the name of that function in your query.