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

// Formula: 1/(MN) SUM_i[((Mni - Nmi)^2)/(mi+ni)]
static <A> double chiSquared(MultiSet<A> ms1, MultiSet<A> ms2) {
  int m = ms1.size(), n = ms2.size();
  if (m == 0 || n == 0) ret Double.NaN;
  double sum = 0;
  for (A key : joinSets(keys(ms1), keys(ms2))) {
    double mi = ms1.get(key);
    double ni = ms2.get(key);
    double item = sqr(m*ni-n*mi)/(mi+ni);
    ifdef chiSquared_debug
      printVars_str(+key, +mi, +ni, +item);
    endifdef
    sum += item;
  }
  ifdef chiSquared_debug
    printVars_str(+sum, mn := m*n, mPlusN := m+n);
  endifdef
  ret sum/(m*n);
  //ret sum/(m+n);
  //ret sum/pow(m*n, 1.5);
}

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: 229 / 366
Version history: 8 change(s)
Referenced in: [show references]