Python Forum
How to create a moving average backward of a serie of sumbers?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a moving average backward of a serie of sumbers?
#1
Hi,

Here's what i want to do. I tried in C# but was wondering if it would not be more pratical in Python.

I have a serie of numbers: var Quodown = new List<int>() {9,7,6,9,7,6,4,3,0}; or if you prefer in python list = [9,7,6,9,7,6,4,3,0]

I am summing the numbers backward: 0+3 =3 than 0+3+4 = 7 ...

for (int i = 8; i < Quodown.Count; i--)

                {
					sumi += Quodown[i];
					
					Print("sumi:  "+ sumi);
					
					//Print(i);
                }
Now i want a moving average of each numbers i summed in the first loop.

(3 + 0) / 2 = 1.5
(4 +3 +0) / 3 = 2.333
etc

I suppose i shoud recycle the i and make a nested for loop but its not working.

for (int y = i; y <  8; y++)

		                {avg = sumi / y;
									
									Print(y);
								}
Output:
sumi: 0 sumi: 3 7 // it must divide by the number of elements but it is not counting the elements just returning the position of the element sumi: 7 6 7 sumi: 13 5 // actually it should be 3 , 2 , 1 6 7 sumi: 20 4 // 4,3,2,1 5 6 7
Any help?

Thank you
Reply
#2
What is a moving average? If you have A, B, C, D and your averaging window is 3, is the average
(A + B + C) / 3, (B + C + D) / 3?
Or do you pad with zero so you end up with the same number of points?
(0 + A + B) / 3, (A + B + C) / 3, (B + C + D) / 3, (C + D + 0) / 3
Or do you change the window size?
A, (A + B) / 2, (A + B + C) / 3, (B + C + D) / 3
Or maybe this way?
(A + B + C) / 3, (B + C + D) / 3, (C + D) / 2, D

The first is easy to do.
import random


numbers = [random.randint(0, 20) for _ in range(10)]
print(numbers)

window = 4
avg = [sum(numbers[a : a + window]) / window for a in range(len(numbers) - window + 1)]
print(avg)
Output:
[18, 6, 12, 16, 9, 5, 4, 9, 20, 12] [13.0, 10.75, 10.5, 8.5, 6.75, 9.5, 11.25]
And the last one is easy to do:
import random


numbers = [random.randint(0, 20) for _ in range(10)]
print(numbers)

window = 4
avg = [sum(numbers[a : a + window]) / len(numbers[a : a + window]) for a in range(len(numbers))]
print(avg)
Output:
[17, 1, 15, 17, 7, 15, 14, 8, 10, 18] [12.5, 10.0, 13.5, 13.25, 11.0, 11.75, 12.5, 12.0, 14.0, 18.0]
Reply
#3
Something like this?

from random import randint

# make a list of nums
nums = [randint(0, 99) for i in range(100)]
# or use len(nums) - 2 below if you don't want a list of 1 number
average_sum = [sum(nums[a:]) / len(nums[a:]) for a in range(len(nums) - 1, -1, -1)]
# the average converges to around half the maximum number, 100 in this case
Frankd likes this post
Reply
#4
(Jun-25-2024, 06:10 AM)Pedroski55 Wrote:
range(len(nums) - 1, -1, -1)]
Not the best way to move backward in Python there are own method for this.
>>> Quodown = [9, 7, 6, 9, 7, 6, 4, 3, 0]
>>> reversed(Quodown)
<list_reverseiterator object at 0x000001D98D2BC130>
>>> list(reversed(Quodown))
[0, 3, 4, 6, 7, 9, 6, 7, 9]

>>> # The more famous one in Python
>>> Quodown[::-1]
[0, 3, 4, 6, 7, 9, 6, 7, 9]
So something like this.
Quodown = [9, 7, 6, 9, 7, 6, 4, 3, 0]
# Calculate cumulative sums from end to start
cumulative_sums = []
sumi = 0
for value in reversed(Quodown):
    sumi += value
    cumulative_sums.append(sumi)

# Calculate moving averages based on cumulative sums
moving_averages = [value / (index + 1) for index, value in enumerate(cumulative_sums)]
for index, value in enumerate(cumulative_sums, start=1):
    print(f"Cumulative sum of the first {index} elements: {value}")
# Print moving averages results
print('-' * 40)
for index, value in enumerate(moving_averages, start=1):
    print(f"Moving average of the first {index} elements: {value:.3f}")
Output:
Cumulative sum of the first 1 elements: 0 Cumulative sum of the first 2 elements: 3 Cumulative sum of the first 3 elements: 7 Cumulative sum of the first 4 elements: 13 Cumulative sum of the first 5 elements: 20 Cumulative sum of the first 6 elements: 29 Cumulative sum of the first 7 elements: 35 Cumulative sum of the first 8 elements: 42 Cumulative sum of the first 9 elements: 51 ---------------------------------------- Moving average of the first 1 elements: 0.000 Moving average of the first 2 elements: 1.500 Moving average of the first 3 elements: 2.333 Moving average of the first 4 elements: 3.250 Moving average of the first 5 elements: 4.000 Moving average of the first 6 elements: 4.833 Moving average of the first 7 elements: 5.000 Moving average of the first 8 elements: 5.250 Moving average of the first 9 elements: 5.667
Frankd likes this post
Reply
#5
That is exactly what i wanted.

Now i am trying to compare each element of reversed_list with my nums list.

For example, the sum of all numbers in num gives 51 and the average is 5.66 . I want to find every number in nums that are above 5.66 (7,6,9,7,6)
Than the sum of the fist next numbers are 42 and the average is 5.25 and all above numbers in the list are (6,9,7,6) not including the first number.
etc. till we get to the last two 3 and 0 with an average of 1.5 and the above number is none.

I tried with
if any(x>nums for x in reversed_list): print(nums)

but i get an error:

Error:
Traceback (most recent call last): File "<main.py>", line 25, in <module> File "<main.py>", line 25, in <genexpr> TypeError: '>' not supported between instances of 'float' and 'list'
Anyway i dont think thats how to do this. In the end i want to be able to manipulate the results to make calculation, like myInt = (10 + (numbers above average(7,6,9,7,6)) / 51 for exemple.

Any suggestion?

import numpy as np

nums = [9,7,6,9,7,6,4,3,0]
cumsum = np.cumsum(nums[::-1])[::-1] 

print(cumsum)

# or use len(nums) - 2 below if you don't want a list of 1 number
average_sum = [sum(nums[a:]) / len(nums[a:]) for a in range(len(nums) - 1, -1, -1)]
# the average converges to around half the maximum number, 100 in this case
#sum = sum(nums[0:]) 
#print(sum)
reversed_list = list(reversed(average_sum))
print(reversed_list)

myInt = 10
newList = [myInt / x for x in cumsum]

print(newList)


if any(x>nums for x in reversed_list):
    print(nums)

#print(average_sum)
(Jun-25-2024, 06:10 AM)Pedroski55 Wrote: Something like this?

from random import randint

# make a list of nums
nums = [randint(0, 99) for i in range(100)]
# or use len(nums) - 2 below if you don't want a list of 1 number
average_sum = [sum(nums[a:]) / len(nums[a:]) for a in range(len(nums) - 1, -1, -1)]
# the average converges to around half the maximum number, 100 in this case
Reply
#6
I just saw your post way after the one from Pedrosky.

Its a bit easier to understand. Still try to get for each ma the numbers above each one to insert equations.

(Jun-25-2024, 02:00 PM)snippsat Wrote:
(Jun-25-2024, 06:10 AM)Pedroski55 Wrote:
range(len(nums) - 1, -1, -1)]
Not the best way to move backward in Python there are own method for this.
>>> Quodown = [9, 7, 6, 9, 7, 6, 4, 3, 0]
>>> reversed(Quodown)
<list_reverseiterator object at 0x000001D98D2BC130>
>>> list(reversed(Quodown))
[0, 3, 4, 6, 7, 9, 6, 7, 9]

>>> # The more famous one in Python
>>> Quodown[::-1]
[0, 3, 4, 6, 7, 9, 6, 7, 9]
So something like this.
Quodown = [9, 7, 6, 9, 7, 6, 4, 3, 0]
# Calculate cumulative sums from end to start
cumulative_sums = []
sumi = 0
for value in reversed(Quodown):
    sumi += value
    cumulative_sums.append(sumi)

# Calculate moving averages based on cumulative sums
moving_averages = [value / (index + 1) for index, value in enumerate(cumulative_sums)]
for index, value in enumerate(cumulative_sums, start=1):
    print(f"Cumulative sum of the first {index} elements: {value}")
# Print moving averages results
print('-' * 40)
for index, value in enumerate(moving_averages, start=1):
    print(f"Moving average of the first {index} elements: {value:.3f}")
Output:
Cumulative sum of the first 1 elements: 0 Cumulative sum of the first 2 elements: 3 Cumulative sum of the first 3 elements: 7 Cumulative sum of the first 4 elements: 13 Cumulative sum of the first 5 elements: 20 Cumulative sum of the first 6 elements: 29 Cumulative sum of the first 7 elements: 35 Cumulative sum of the first 8 elements: 42 Cumulative sum of the first 9 elements: 51 ---------------------------------------- Moving average of the first 1 elements: 0.000 Moving average of the first 2 elements: 1.500 Moving average of the first 3 elements: 2.333 Moving average of the first 4 elements: 3.250 Moving average of the first 5 elements: 4.000 Moving average of the first 6 elements: 4.833 Moving average of the first 7 elements: 5.000 Moving average of the first 8 elements: 5.250 Moving average of the first 9 elements: 5.667
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Moving average strategy irina_shubina 2 1,965 Jul-31-2022, 05:11 PM
Last Post: paulyan
  from Json Time Serie file to python dictionnary Reims 1 2,140 Sep-11-2019, 08:17 AM
Last Post: DeaD_EyE
  how do i create average of 1000 frames per second from video with yolo Rupen_gurung 0 1,560 Jul-20-2019, 01:55 AM
Last Post: Rupen_gurung
  Arabic language is backward in python charts bandar 3 3,797 Feb-23-2018, 07:10 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020