27
3
Disclaimer: This challenge is inspired by a coding error I once made.
Okay, time for a maths lesson. A normal mean average looks like this:
Work out the sum of all numbers in a list
then divide by the size of the list.
But what if we don't know all the numbers at the time we're working out the average? We need a way to work out the average which can be added to over time. For this reason, I present the algorithm for a Progressive Mean™
The running total is the first number in the list
For each of the remaining numbers
Add the number to the running total
Divide the running total by two
So in effect we're averaging each number with the current average. (We could add to this later and get the same result)
BUT
This doesn't give the same result at all. It gives an average, but it differs from the standard methodology for finding the mean. Now the order of the list of numbers is significant.
Of course, being a curious type, I want to work out if the Progressive Mean™ tells us anything about the order of our list of numbers. So for this reason I want to compare Mean with Progressive Mean™ by means of a simple subtraction:
trend = Progressive Mean™ - Standard Mean
The Challenge
- Write a piece of code which accepts a list of numbers (in any format) which then calculates three pieces of information about it:
- Standard Mean
- Progressive Mean™
- Trend (Progressive - standard)
- Work in any language you like.
- It's golf, attempt to do the challenge in as few bytes as you can.
- Avoid Standard Loopholes
- I want the output to be human-readable numbers.
- Please include a link to an online interpreter such as tio.run
Test Cases:
[1,2,3]
Normal Mean: 2.0
Progressive Mean: 2.25
Trend: 0.25
[3, 2, 1]
Normal Mean: 2.0
Progressive Mean: 1.75
Trend: -0.25
[10, 20, 30]
Normal Mean: 20.0
Progressive Mean: 22.5
Trend: 2.5
[300, 200, 100]
Normal Mean: 200.0
Progressive Mean: 175.0
Trend: -25.0
[10, 100, 10]
Normal Mean: 40.0
Progressive Mean: 32.5
Trend: -7.5
[4, 4, 9, 8, 1, 8, 6, 9, 1, 1]
Normal Mean: 5.1
Progressive Mean: 2.62890625
Trend: -2.4710937499999996
[1, 1, 1, 4, 4, 6, 8, 8, 9, 9]
Normal Mean: 5.1
Progressive Mean: 8.5390625
Trend: 3.4390625000000004
[9, 9, 8, 8, 6, 4, 4, 1, 1, 1]
Normal Mean: 5.1
Progressive Mean: 1.47265625
Trend: -3.6273437499999996
2Do we have to output all 3 calculations, or just the trend? – Kobe – 2020-02-03T13:07:46.457
2...also a set has no order, while here the order is paramount. – Jonathan Allan – 2020-02-03T13:14:03.223
1I’m fine with it being called list. Yes, all three calculations, please. – AJFaraday – 2020-02-03T13:15:51.167
3I believe your progressive mean is a type of decaying average. – Neil – 2020-02-03T13:24:46.540
"a set of numbers (in any format)" — even in reverse? – Adám – 2020-02-03T13:25:11.960
@Adám I was more thinking about it being expressed as string formats, binary, array etc. I'd say when the order in significant we probably shouldn't allow reversed order. – AJFaraday – 2020-02-03T14:11:34.430
1@AJFaraday I'd keep the order, just read it from right to left. – Adám – 2020-02-03T14:12:14.343
Do we need to output decimal points? – S.S. Anne – 2020-02-03T17:45:05.110
@ssanne yes, we definitely need decimal points. – AJFaraday – 2020-02-03T19:00:43.770
Can we assume the list is non-empty? – IMP1 – 2020-02-04T13:41:39.643
@IMP1 Yes, you can definitely assume that. – AJFaraday – 2020-02-04T15:41:29.703
13
Just commenting to point out that your "progressive mean" is well known and used; it's an exponentially weighted moving average with coefficient 1/2.
– acwaters – 2020-02-04T19:39:04.590@acwaters That's interesting, thank you. – AJFaraday – 2020-02-04T20:11:36.200
suggested test case:
[1]
->1,1,0
; this was screwing up my answers before. I would also include a 2-element test case, which will of course have identical progressive/regular means. – Giuseppe – 2020-02-04T21:51:12.250