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 MeanTM
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 MeanTM tells us anything about the order of our list of numbers. So for this reason I want to compare Mean with Progressive MeanTM by means of a simple subtraction:
trend = Progressive MeanTM - 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 MeanTM
- 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
-
3\$\begingroup\$ Do we have to output all 3 calculations, or just the trend? \$\endgroup\$Kobe– Kobe2020年02月03日 13:07:46 +00:00Commented Feb 3, 2020 at 13:07
-
3\$\begingroup\$ ...also a set has no order, while here the order is paramount. \$\endgroup\$Jonathan Allan– Jonathan Allan2020年02月03日 13:14:03 +00:00Commented Feb 3, 2020 at 13:14
-
1\$\begingroup\$ I’m fine with it being called list. Yes, all three calculations, please. \$\endgroup\$AJFaraday– AJFaraday2020年02月03日 13:15:51 +00:00Commented Feb 3, 2020 at 13:15
-
3\$\begingroup\$ I believe your progressive mean is a type of decaying average. \$\endgroup\$Neil– Neil2020年02月03日 13:24:46 +00:00Commented Feb 3, 2020 at 13:24
-
17\$\begingroup\$ 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. \$\endgroup\$acwaters– acwaters2020年02月04日 19:39:04 +00:00Commented Feb 4, 2020 at 19:39
28 Answers 28
R, 57 bytes
function(v,`!`=sum,N=!1^v)c(A<-!v/N,P<-!v/2^c(N:1,N),P-A)
A clever collaboration with digEmAll and RobinRyder; uses the observation by Grimmy and Kevin's explanation from the comments, which I replicate below:
For anyone else wondering why the example
[a,b,c,d] -> [a,b,c,c,d,d,d,d]doesn't match the explanation of the code, it actually transforms[a,b,c,d]to[a,b,b,c,c,c,c,d,d,d,d,d,d,d,d,a](so two of each value in comparison to the example). The example confused me for a moment. But nice answer, +1 from me! I also like that it has all these different types of the letter 'A' in the code:āÅÅAÂÆa. xD
This uses R's recycling rules to use two copies of l[1]/2^N in the sumby just sticking an extra N at the end of the decay rate.
R, 62 bytes
function(l)c(p<-Reduce(function(x,y)(x+y)/2,l),a<-mean(l),p-a)
Straightforward implementation.
-
2\$\begingroup\$ Try it online! - same length (62) but using the fancier approach (maybe I'm missing some trivial improvements) \$\endgroup\$digEmAll– digEmAll2020年02月04日 12:54:52 +00:00Commented Feb 4, 2020 at 12:54
-
1\$\begingroup\$ 59 bytes by building on @digEmAll 's approach. \$\endgroup\$Robin Ryder– Robin Ryder2020年02月04日 17:49:27 +00:00Commented Feb 4, 2020 at 17:49
-
\$\begingroup\$ @RobinRyder neither yours nor digEmAll's (nor my 51 byte solution) works for the case of 1 element...I guess I can ask about that. \$\endgroup\$Giuseppe– Giuseppe2020年02月04日 21:35:13 +00:00Commented Feb 4, 2020 at 21:35
-
1
J, 17 bytes
Translation of this.
-:@+/@|.(,,-)+/%#
Anonymous tacit prefix function. Returns list of Progressive MeanTM, Standard Mean, Trend.
Try it online!
This consists of three parts: (2÷⍨+)⌿∘⌽ (,,-) +⌿÷≢
+/ % # is the sum divided by the number of elements
...⌿@|. reverses the list and then reduces from right to left using the function:
-:@+ add next value to previous value and halve that
(,,-) is the concatenation of the PM and the SM, concatenated to their difference
Python 3.8 (pre-release), (削除) 70 (削除ここまで) 69 bytes
-1 byte thanks to Kyle Gullion
returns (mean, p_mean, trend)
lambda x:(m:=sum(x)/len(x),[p:=x[0],*[p:=(p+n)/2for n in x]][-1],p-m)
Alternate version with more of a reducer implementation (89 bytes):
lambda x:(m:=sum(x)/(l:=len(x)),[x:=[sum(x[0:2])/2]+x[2:]for n in range(l-1)][-1],x[0]-m)
Although this is longer, I suspect this reducer implementation may be useful for future python 3.8 code golfing.
I know there has to be some more clever tricks to golf this further...
-
\$\begingroup\$ can shave a byte using tuple unpacking: Try it online! \$\endgroup\$Kyle G– Kyle G2020年02月04日 14:44:23 +00:00Commented Feb 4, 2020 at 14:44
05AB1E, 13 bytes
ÅAIÅ»+;}θ‚ÂÆa
Outputs as a triplet [mean, progressive-mean, trend].
Try it online or verify all test cases.
Explanation:
ÅA # Calculate the arithmetic mean of the (implicit) input-list
I # Push the input-list again
Å» # Left-reduce it by:
+ # Add the two values together
; # And halve it
}θ # After the reduction, leave only the last value
‚ # Pair it with the earlier calculated arithmetic mean
 # Bifurcate this pair; short for Duplicate & Reverse copy
Æ # Reduce it by subtraction
a # And append this trend to the pair
# (after which the result is output implicitly)
-
1\$\begingroup\$ @Grimmy I kinda wish
Å»+;}θcould be done shorter though. Unfortunately.»ÅAdoesn't work, sinceÅAonly works on lists, and not on the values on the stack, so it will just output the last integer of the input-list unfortunately. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年02月03日 14:09:22 +00:00Commented Feb 3, 2020 at 14:09 -
2\$\begingroup\$ I found the fix:
ā<oÅΓĆ‚€ÅAÂÆª. The first number has the same weight as the second number, my mistake was giving it only half of that weight. This is now 13 bytes like yours; I'll make it a separate answer. \$\endgroup\$Grimmy– Grimmy2020年02月03日 14:10:57 +00:00Commented Feb 3, 2020 at 14:10 -
2
Scratch 2.0/3.0, 35 blocks/263 bytes
SB Syntax:
when gf clicked
set[i v]to(2
set[L v]to(length of[I v
set[T v]to(item(1)of[I v
set[P v]to(T
repeat((L)-(1
change[T v]by(item(i)of[I v
change[P v]by(item(i)of[I v
set[P v]to((P)/(2
change[i v]by(1
end
set[T v]to((T)/(L
say(join(join(join(join(P)[,])(T))[,])((P)-(T
It's accidentally a polyglot. I accidentally forgot my password and email for my other scratch account so I had to make a new one so y'all can Try it on Scratch!
Y'all need to change the input list shown in the top left corner on the project for this to work properly.
Python 3, (削除) 81 (削除ここまで) \$\cdots\$ (削除) 75 (削除ここまで) 71 bytes
Saved (削除) 1 (削除ここまで)5 bytes thanks to Reinstate Monica!!!
def f(l):
m=sum(l)/len(l);p=l[0]
for n in l:p=(p+n)/2
return m,p,p-m
-
\$\begingroup\$ You can remove the parentheses in the return statement, no? \$\endgroup\$squid– squid2020年02月03日 16:44:39 +00:00Commented Feb 3, 2020 at 16:44
-
\$\begingroup\$ @ReinstateMonica Didn't know that - Thanks! :-) \$\endgroup\$Noodle9– Noodle92020年02月03日 16:46:51 +00:00Commented Feb 3, 2020 at 16:46
-
\$\begingroup\$ No problem! Also, since
(p[0]+p[0])/2 == p[0], the pop is unnecessary; you can just usel[0]\$\endgroup\$squid– squid2020年02月03日 16:50:34 +00:00Commented Feb 3, 2020 at 16:50 -
\$\begingroup\$ @ReinstateMonica Clever - thanks again! :-) \$\endgroup\$Noodle9– Noodle92020年02月03日 17:00:45 +00:00Commented Feb 3, 2020 at 17:00
JavaScript (ES6), 55 bytes
Returns [mean, p_mean, trend].
a=>a.map(v=>t=(s+=v,n++)?(t+v)/2:v,n=s=0)&&[s/=n,t,t-s]
05AB1E, 12 bytes
āÍoîÅΓ‚ÅAÂÆa
The progressive mean is equivalent to a weighted arithmetic mean, with exponentially increasing weights. For example, the progressive mean of a 4 element list [a, b, c, d] is the arithmetic mean of [a, b, c, c, d, d, d, d].
ā # range [1..length of input]
Í # subtract 2 from each
o # 2**each: [0.5, 1, 2, ...]
î # round up: [1, 1, 2, ...]
ÅΓ # run-length decode
‚ # pair with the input
ÅA # arithmetic mean of each list
 # push a reversed copy
Æ # reduce by subtraction
a # append
-
4\$\begingroup\$ For anyone else wondering why the example
[a,b,c,d] -> [a,b,c,c,d,d,d,d]doesn't match the explanation of the code, it actually transforms[a,b,c,d]to[a,b,b,c,c,c,c,d,d,d,d,d,d,d,d,a](so two of each value in comparison to the example). The example confused me for a moment. But nice answer, +1 from me! I also like that it has all these different types of the letter 'A' in the code:āÅÅAÂÆª. xD \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年02月03日 14:32:50 +00:00Commented Feb 3, 2020 at 14:32 -
1\$\begingroup\$ @KevinCruijssen I changed the code to match the explanation (same byte count). \$\endgroup\$Grimmy– Grimmy2020年02月03日 14:46:03 +00:00Commented Feb 3, 2020 at 14:46
APL (Dyalog Unicode), (削除) 20 (削除ここまで) 18 bytes SBCS
-2 thanks to Bubbler.
Anonymous tacit prefix function. Returns list of Progressive MeanTM, Standard Mean, Trend.
(2÷⍨+)⌿∘⌽(,,-)+⌿÷≢
This consists of three parts: (2÷⍨+)⌿∘⌽ (,,-) +⌿÷≢
+⌿ ÷ ≢ is the sum divided by the count
(...)⌿∘⌽ reverses the list and then reduces from right to left using the function:
2÷⍨+ add next value to previous value and divide that by two
(,,-) is the concatenation of the PM and the SM, concatenated to their difference
Factor, (削除) 80 (削除ここまで) 79 bytes
: f ( s -- s ) [ mean ] [ dup first [ + 2. / ] reduce ] bi 2dup swap - 3array ;
Wolfram Language (Mathematica), 31 bytes
{a=Mean@#,b=+##/2&~Fold~#,b-a}&
Try it online! Pure function. Takes a list of numbers as input and returns a list of rational numbers as output. I'm pretty sure that improper fractions count as human-readable, but if they aren't, adding N@ (+2 bytes) to the start causes the function to output approximate numbers.
Jelly, 9 bytes
Æm;+H\/ṄI
A full program which accepts a list of decimals which prints:
[mean, progressive-mean]
trend
How?
Æm;+H\/ṄI - Link: list of numbers, A
Æm - arithmetic mean sum(A)/length(A) = mean
/ - reduce (A) by:
\ - the dyad f(leftEntry, rightEntry):
+ - addition leftEntry + rightEntry
H - halve (leftEntry + rightEntry) / 2 = progressiveMean
; - concatenate [mean, progressiveMean]
Ṅ - print this list and yield it
I - deltas [progressiveMean - mean]
- implicit print (Jelly represents with a single item as the item)
PowerShell, 54 bytes
$args|%{$y+=$_;$z=($_+$z)/(2-!$i++)}
($y/=$i)
$z
$z-$y
Takes input by splatting. The only neat trick is the !$i++ bit which negates the first iteration where $i is 0 to 1, and all subsequent numbers to 0.
Raku (Perl 6), (削除) 41 (削除ここまで) 40 bytes
{|($/=(.reduce((*+*)/2),.sum/$_)),0ドル-1ドル}
My original solution had my \a=and used [-]|@a at the end, but abusing $/ is always fun, especially with also abusing assignments. Pretty straightforward calculation wise.
-
\$\begingroup\$ FWIW, the TIO link no longer matches the solution. \$\endgroup\$Mark Reed– Mark Reed2022年12月31日 18:05:42 +00:00Commented Dec 31, 2022 at 18:05
C (gcc), (削除) 111 (削除ここまで) 109 bytes
#define P printf("%f "
f(a,l,i)int*a;{float s=*a++,p=s;for(i=l-1;i--;p+=*a++,p/=2)s+=*a;P,s/=l);P,p);P,p-s);}
-
1\$\begingroup\$ 95 bytes \$\endgroup\$ceilingcat– ceilingcat2022年06月16日 08:27:03 +00:00Commented Jun 16, 2022 at 8:27
Charcoal, 27 bytes
≔∕ΣθLθη≔§θ0ζFθ≔⊘+ιζζI⟦ηζ−ζη
Try it online! Link is to verbose version of code. Explanation:
≔∕ΣθLθη
Calculate the arithmetic mean.
≔§θ0ζ
Start with the first element. We therefore end up averaging this element with itself, but that doesn't affect the final result of course.
Fθ≔⊘+ιζζ
Loop over each element an average it with the progressive mean so far.
I⟦ηζ−ζη
Print the arithmetic and progressive means and their difference.
If we had only needed the result once then ΣEθ∕ιX2−Lθ∨κ1 could have been used to calculate the progressive mean directly, saving 3 bytes over the naïve method.
Haskell, 84 bytes
I thought Haskell could do decently here because this challenge works on lists and I tend to think Haskell and lists go well together... But I couldn't come up with anything very clever. Let's see if @xnor drops by and has anything nice :P
f(h:l)=g$foldl(\(p,s,w)n->((p+n)/2,s+n,w+1))(h,h,1)l
f[]=f[0]
g(p,s,l)=(s/l,p,p-s/l)
I am defining an anonymous function \(p,s,w) n -> ((p+n)/2, s+n, w+1) that receives a triplet and the next list element, and does three things: updates the progressive mean, sums the whole list and computes its length. In the end, function g does some last-minute calculations.
You can try it online
Burlesque, 19 bytes
psJr{.+2./}javq.-c!
Takes input of the form 300.0 200.0 100.0. If we didn't have to print the other values would be 3 bytes shorter (i.e. Without the continuation).
Returns progressive meanTM, arithmetic mean, trend
ps # Parse list to array
J # Duplicate
r{ # Reduce by (Progressive Mean)
.+ # Adding
2./ # Dividing by 2
}
jav # Calulcate regular mean
q.- # Quoted (boxed) subtract {.-}
c! # On a non-destructive stack
Python 3.8, 91 bytes
I wanted to flatten the recursive relation, and I got this:
lambda l:(m:=sum(l)/(n:=len(l)),(p:=sum(l[-i]/2**i for i in range(1,n))+l[0]/2**(n-1)),p-m)
The use of range instead of the list itself makes it much longer.
-
\$\begingroup\$ you can remove the brackets around the middle section to save 2 bytes Try it online! \$\endgroup\$mabel– mabel2020年02月04日 16:09:57 +00:00Commented Feb 4, 2020 at 16:09
Ruby, 54 bytes
->x{a=x.sum/x.size;b=x.inject{|a,b|a/2+b/2};[a,b,b-a]}
Takes a list of numbers in the format [1.0, 2.0, 3.0]
and returns an array containing the mean, progressive-mean, and trend, respectively.
Rust, (削除) 144 (削除ここまで) 122 bytes
let f=|y:&[f64]|{let m=y.iter().sum::<f64>()/y.len()as f64;let pm=y[1..].iter().fold(y[0],|acc,x|(acc+x)/2.);(m,pm,pm-m)};
Straightforward approach. Shaved 22 bytes thanks SirBogman, and thanks Mabel for showing TIO's Rust support.
-
2\$\begingroup\$ here you go :) Try it online! \$\endgroup\$mabel– mabel2020年02月05日 22:49:04 +00:00Commented Feb 5, 2020 at 22:49
-
1\$\begingroup\$ You can remove the return keyword and the semicolon following the return statement. \$\endgroup\$SirBogman– SirBogman2020年02月07日 00:41:03 +00:00Commented Feb 7, 2020 at 0:41
Java (JDK), (削除) 116 (削除ここまで) 115 bytes
v->{float m=0,p=0;for(int i=0;i<a.length;i++){m+=a[i];p+=a[i];p=i!=0?p/2:p;}m/=a.length;return m+"\n"+p+"\n"+(p-m);
-
\$\begingroup\$ 105 bytes \$\endgroup\$ceilingcat– ceilingcat2020年02月07日 23:04:21 +00:00Commented Feb 7, 2020 at 23:04
Scala, 126 bytes
Golfed version. Try it online!
def f(v:Array[Float])={var m,p:Float=0;for(i<-v.indices){m+=v(i);p+=v(i);p=if(i!=0)p/2 else p;};m/=v.length;s"$m\n$p\n${p-m}"}
Ungolfed version. Try it online!
object Main extends App {
val a: Array[Float] = Array(9, 9, 8, 8, 6, 4, 4, 1, 1, 1)
val f: Array[Float] => String = v => {
var m, p: Float = 0
for (i <- v.indices) {
m += v(i)
p += v(i)
p = if (i != 0) p / 2 else p
}
m /= v.length
s"$m\n$p\n${p - m}"
}
println(f(a))
}