0

I need to replace a list of numbers, [1,0,3,2,1,4,1,2] for instance, by the moving average with a window of 3. The second element, 0 in this case, should become (1+0+3)/3. The problem I have is that I want to keep the new total the same as the original one, 14 in this case. With the constraint of keeping the same total value, I cannot use a truncated moving average for the first and last elements.

Is there a way around this issue?

maRmat
  • 101
  • 2
  • It's not clear (to me) what you are asking here. Why would you just change the second element to the average of the first three? More generally, just think of the simplest non-trivial case: $(a,b,c,d)$. What do you want to replace that $4-$tuple with? – lulu Jul 26 '23 at 14:24
  • 2
    A way is to 'wrap around' the list. The first element will become (2+1+0)/3, where the 2 is the originally last element. – Brian Cheung Jul 26 '23 at 14:29
  • @lulu I am just giving an example for the second element. – maRmat Jul 26 '23 at 14:30
  • @BrianCheung In theory it would do the job but practically I am working with time series of events by hours and I cannot use the last hour of the previous day with the first 2 of the current day since it would change the total. Using the last hour of a given day with the first 2 would not make sense. – maRmat Jul 26 '23 at 14:32
  • 2
    I see. I know nothing about time series, but does it make sense to use some sort of weighted average? Say, use 12/3 + 01/3 for the first element. – Brian Cheung Jul 26 '23 at 14:37
  • So, write out exactly what you want for $(a,b,c,d)$. that should clarify the question. – lulu Jul 26 '23 at 14:39
  • Should say: if you don't know what you want in that case, then write down the tests you would apply to tell if a proposed solution worked or not. You've said that you want the sums to match, fine. I can do that by replacing each term with the total average $\frac {a+b+c+d}4$. But I guess that's not what you want? So...what test would that fail? For $3-$tuples that is what you want, right? Replace each element with $\frac {a+b+c}3$? If not, then indicate what you do want for $3$ elements. – lulu Jul 26 '23 at 14:49
  • @BrianCheung I guess this is the only way to get the total value the same yes. – maRmat Jul 26 '23 at 14:52
  • 2
    I agree with Brian that at each end you could simply do a weighted average, by adding 2/3 the first/last entry and 1/3 of its neighbour. This can be generalised to larger windows (of odd length). If the window extends over the end of the list, just compute the average as if the missing entries from the previous/next day were equal to the middle entry of the window. – Jaap Scherphuis Jul 26 '23 at 14:54
  • 1
    @lulu for all elements of a serie except the first and last ones, I want the new elements to be the moving average with a window 3. The i-th element should become $k_i=(n_{i-1}+n_i+n_{i+1})/3$ and $\sum n_i = \sum k_i$. – maRmat Jul 26 '23 at 14:57
  • @JaapScherphuis This answers my question, thanks. – maRmat Jul 26 '23 at 15:01
  • 1
    There is another variation you could do for larger windows. If your entries are $(x_1,x_2,x_3,..., x_n)$, then treat it as if there are reversed copies of that list of entries before and after it when you take the running average, i.e. $(...,x_2,x_1)(x_1,x_2,x_3,..., x_n)(x_n,x_{n-1},...)$. This might be a bit smoother than my previous suggestion if the entries near either end oscillate. – Jaap Scherphuis Jul 26 '23 at 15:22

0 Answers0