Calculate the Progressive Mean™

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

AJFaraday

Posted 2020-02-03T12:57:53.613

Reputation: 10 466

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

Answers

10

R, 57 bytes

function(v,`!`=sum,N=!1^v)c(A<-!v/N,P<-!v/2^c(N:1,N),P-A)

Try it online!

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ÂÆª. 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)

Try it online!

Straightforward implementation.

Giuseppe

Posted 2020-02-03T12:57:53.613

Reputation: 21 077

2Try it online! - same length (62) but using the fancier approach (maybe I'm missing some trivial improvements) – digEmAll – 2020-02-04T12:54:52.223

159 bytes by building on @digEmAll 's approach. – Robin Ryder – 2020-02-04T17:49:27.440

@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. – Giuseppe – 2020-02-04T21:35:13.747

1

@digEmAll actually, using the observation of Kevin's comment here, we can just use R's recycling rules to take Robin's answer and shave off another two bytes.

– Giuseppe – 2020-02-04T21:43:41.790

8

J, 17 bytes

Translation of this.

-:@+/@|.(,,-)+/%#

Anonymous tacit prefix function. Returns list of Progressive Mean™, 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

Adám

Posted 2020-02-03T12:57:53.613

Reputation: 37 779

7

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)

Try it online!

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.

Try it online!

I know there has to be some more clever tricks to golf this further...

mabel

Posted 2020-02-03T12:57:53.613

Reputation: 1 489

can shave a byte using tuple unpacking: Try it online!

– Kyle G – 2020-02-04T14:44:23.117

6

05AB1E, 13 bytes

ÅAIÅ»+;}θ‚ÂÆª

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
   ª   # And append this trend to the pair
       # (after which the result is output implicitly)

Kevin Cruijssen

Posted 2020-02-03T12:57:53.613

Reputation: 67 575

1@Grimmy I kinda wish Å»+;}θ could be done shorter though. Unfortunately .»ÅA doesn't work, since ÅA only works on lists, and not on the values on the stack, so it will just output the last integer of the input-list unfortunately. – Kevin Cruijssen – 2020-02-03T14:09:22.440

2I 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. – Grimmy – 2020-02-03T14:10:57.313

2

Actually, ÅA vectorizes, so I can drop the for a 12.

– Grimmy – 2020-02-03T14:19:26.107

6

Scratch 2.0/3.0, 35 blocks/263 bytes

Blocks

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.

Lyxal

Posted 2020-02-03T12:57:53.613

Reputation: 5 253

5

Python 3, 81 \$\cdots\$ 75 71 bytes

Saved 15 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

Try it online!

Noodle9

Posted 2020-02-03T12:57:53.613

Reputation: 2 776

You can remove the parentheses in the return statement, no? – Reinstate Monica – 2020-02-03T16:44:39.480

@ReinstateMonica Didn't know that - Thanks! :-) – Noodle9 – 2020-02-03T16:46:51.230

No problem! Also, since (p[0]+p[0])/2 == p[0], the pop is unnecessary; you can just use l[0] – Reinstate Monica – 2020-02-03T16:50:34.070

@ReinstateMonica Clever - thanks again! :-) – Noodle9 – 2020-02-03T17:00:45.090

4

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]

Try it online!

Arnauld

Posted 2020-02-03T12:57:53.613

Reputation: 111 334

4

APL (Dyalog Unicode), 20 18 bytesSBCS

-2 thanks to Bubbler.

Anonymous tacit prefix function. Returns list of Progressive Mean™, Standard Mean, Trend.

(2÷⍨+)⌿∘⌽(,,-)+⌿÷≢

Try it online!

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 two by that

(,,-) is the concatenation of the PM and the SM, concatenated to their difference

Adám

Posted 2020-02-03T12:57:53.613

Reputation: 37 779

@Bubbler Of course it can. Thanks. – Adám – 2020-02-03T13:29:08.863

4

05AB1E, 12 bytes

āÍoîÅΓ‚ÅAÂÆª

Try it online!

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
           ª   # append

Grimmy

Posted 2020-02-03T12:57:53.613

Reputation: 12 521

4For 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 – Kevin Cruijssen – 2020-02-03T14:32:50.440

1@KevinCruijssen I changed the code to match the explanation (same byte count). – Grimmy – 2020-02-03T14:46:03.013

3

Factor, 80 79 bytes

: f ( s -- s ) [ mean ] [ dup first [ + 2. / ] reduce ] bi 2dup swap - 3array ;

Try it online!

Galen Ivanov

Posted 2020-02-03T12:57:53.613

Reputation: 13 815

3

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.

LegionMammal978

Posted 2020-02-03T12:57:53.613

Reputation: 15 731

2

Jelly, 9 bytes

Æm;+H¥/ṄI

A full program which accepts a list of decimals which prints:

[mean, progressive-mean]
trend

Try it online!

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)

Jonathan Allan

Posted 2020-02-03T12:57:53.613

Reputation: 67 804

2

PowerShell, 54 bytes

$args|%{$y+=$_;$z=($_+$z)/(2-!$i++)}
($y/=$i)
$z
$z-$y

Try it online!

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.

Veskah

Posted 2020-02-03T12:57:53.613

Reputation: 3 580

2

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.

Try it online!

user0721090601

Posted 2020-02-03T12:57:53.613

Reputation: 928

1

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

RGS

Posted 2020-02-03T12:57:53.613

Reputation: 5 047

1

Charcoal, 27 bytes

≔∕ΣθLθη≔§θ⁰ζFθ≔⊘⁺ιζζI⟦ηζ⁻ζη

Try it online! Link is to verbose version of code. Explanation:

≔∕ΣθLθη

Calculate the arithmetic mean.

≔§θ⁰ζ

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θ∕ιX²⁻Lθ∨κ¹ could have been used to calculate the progressive mean directly, saving 3 bytes over the naïve method.

Neil

Posted 2020-02-03T12:57:53.613

Reputation: 95 035

1

PHP, 90 88 bytes

function($i){for($j=$i[0];$x=$i[$c++|0];$s+=$x,$j+=$x,$j/=2);return[$s/=$c-1,$j,$j-$s];}

Try it online!

640KB

Posted 2020-02-03T12:57:53.613

Reputation: 7 149

1

Perl 5 -MList::Util=sum,reduce -pa, 51 50 bytes

say+($\=reduce{$a=$a/2+$b/2}@F)-($_=(sum@F)/@F.$/)

Try it online!

Output format is

trend
traditional
progressive

Xcali

Posted 2020-02-03T12:57:53.613

Reputation: 7 671

1

Burlesque, 19 bytes

psJr{.+2./}javq.-c!

Try it online!

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 mean™, 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

DeathIncarnate

Posted 2020-02-03T12:57:53.613

Reputation: 916

1

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)

Try it online!

The use of range instead of the list itself makes it much longer.

Émile Jetzer

Posted 2020-02-03T12:57:53.613

Reputation: 111

you can remove the brackets around the middle section to save 2 bytes Try it online!

– mabel – 2020-02-04T16:09:57.387

1

Ruby, 54 bytes

->x{a=x.sum/x.size;b=x.inject{|a,b|a/2+b/2};[a,b,b-a]}

Try it online!

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.

IMP1

Posted 2020-02-03T12:57:53.613

Reputation: 510

1

Haskell, 58 bytes

s!p=(s,p,p-s)
f x=(sum x/sum[1|_<-x])!foldl1(((/2).).(+))x

Try it online!

user28667

Posted 2020-02-03T12:57:53.613

Reputation: 579

1

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.

Try it online!

Yuri-M-Dias

Posted 2020-02-03T12:57:53.613

Reputation: 111

2

here you go :) Try it online!

– mabel – 2020-02-05T22:49:04.703

1You can remove the return keyword and the semicolon following the return statement. – SirBogman – 2020-02-07T00:41:03.867

1

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);

Try it online!

x_linus

Posted 2020-02-03T12:57:53.613

Reputation: 329

105 bytes – ceilingcat – 2020-02-07T23:04:21.910