// Formula: 1/(MN) SUM_i[((Mni - Nmi)^2)/(mi+ni)]
static double chiSquared(MultiSet ms1, MultiSet 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);
}