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

21
LINES

< > BotCompany Repo | #1030107 // chiSquared - compare 2 MultiSets

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

Libraryless. Click here for Pure Java version (3031L/19K).

1  
// Formula: 1/(MN) SUM_i[((Mni - Nmi)^2)/(mi+ni)]
2  
static <A> double chiSquared(MultiSet<A> ms1, MultiSet<A> ms2) {
3  
  int m = ms1.size(), n = ms2.size();
4  
  if (m == 0 || n == 0) ret Double.NaN;
5  
  double sum = 0;
6  
  for (A key : joinSets(keys(ms1), keys(ms2))) {
7  
    double mi = ms1.get(key);
8  
    double ni = ms2.get(key);
9  
    double item = sqr(m*ni-n*mi)/(mi+ni);
10  
    ifdef chiSquared_debug
11  
      printVars_str(+key, +mi, +ni, +item);
12  
    endifdef
13  
    sum += item;
14  
  }
15  
  ifdef chiSquared_debug
16  
    printVars_str(+sum, mn := m*n, mPlusN := m+n);
17  
  endifdef
18  
  ret sum/(m*n);
19  
  //ret sum/(m+n);
20  
  //ret sum/pow(m*n, 1.5);
21  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030107
Snippet name: chiSquared - compare 2 MultiSets
Eternal ID of this version: #1030107/9
Text MD5: 0d5b55949ef1e8a5c169f641d71d8115
Transpilation MD5: 808c980fc736d1ed2ac8399b4d481685
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-11-09 12:47:42
Source code size: 640 bytes / 21 lines
Pitched / IR pitched: No / No
Views / Downloads: 230 / 367
Version history: 8 change(s)
Referenced in: [show references]