Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

31
LINES

< > BotCompany Repo | #1036458 // HalfWelford [running average - I think this is nonsense. Average does the same]

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (10102L) is out of date.

1  
// from https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm
2  
srecord HalfWelford(long count, dbl mean) {
3  
  void add(double newValue) {
4  
    count++;
5  
    dbl delta = newValue - mean;
6  
    mean += delta / count;
7  
  }
8  
  
9  
  void remove(double valueToRemove) {
10  
    guarantee(count > 0);
11  
    
12  
    double newValue = valueToRemove;
13  
    mean = (mean-newValue/count)/(1-1.0/count);
14  
    --count;
15  
  }
16  
 
17  
  bool isEmpty() { ret count == 0; }
18  
19  
  dbl average aka avg aka mean() { ret count == 0 ? Double.NaN : mean; }
20  
21  
  toString {
22  
    S s = nValues(count);
23  
    if (!isEmpty())
24  
      s += ", avg: " + avg();
25  
    ret s;
26  
  }
27  
  
28  
  double get() {
29  
    ret avg();
30  
  }
31  
}

Author comment

Began life as a copy of #1033575

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036458
Snippet name: HalfWelford [running average - I think this is nonsense. Average does the same]
Eternal ID of this version: #1036458/2
Text MD5: bf9d391c493cbf9ba320dab793c8dbca
Author: stefan
Category: javax / maths
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-16 02:00:18
Source code size: 714 bytes / 31 lines
Pitched / IR pitched: No / No
Views / Downloads: 75 / 107
Version history: 1 change(s)
Referenced in: [show references]