📄 frqtab.c
字号:
--fx; s_x += *fx * *fx; } /* compute sum_x N(x)^2 */ for (fy = ftab->frq_y +(y = ftab->ycnt); --y >= 0; ) { if (*--fy <= 0) continue; /* skip empty rows */ t = 0; /* process a conditional distribution */ c = ftab->frq_xy +ftab->xcnt; for (fx = ftab->frq_x +(x = ftab->xcnt); --x >= 0; --c) if (*--fx > 0) t += (*c)[y]*(*c)[y]; w_xy += t / *fy; /* sum_y 1/N(y) sum_x N(x,y)^2 */ } t = ftab->known; t *= t; /* compute N^2 */ switch (measure & 0xff) { /* evaluate the measure code */ case FEM_GINIMOD: /* modified Gini index */ if (s_x <= 0) return 0; /* check the denominator */ gini = s_xy / s_x - s_y / t; break; case FEM_RELIEF: /* relief measure */ if ((s_y <= 0) || (t -s_y <= 0)) return 0; gini = s_xy / s_y - (s_x -s_xy) / (t -s_y); break; default: /* symmetric Gini index */ t = 2*t -s_x -s_y; /* compute and */ if (t <= 0) return 0; /* check the denominator */ gini = ((w_xy +w_yx) *ftab->known -s_x -s_y) /t; break; } /* compute requested measure */ if (measure & FEF_WGTD) return gini *(ftab->known/ftab->frq); return gini; /* return Gini index/relief measure */} /* _gini() *//*--------------------------------------------------------------------*/static double _wdiff (FRQTAB *ftab, int measure, double *params){ /* --- weighted sum of differences */ int x, y, m; /* loop variables, integer exponent */ double **c; /* to traverse the table columns */ double *fx, *fy, *fxy; /* to traverse the frequencies */ double e, s, t; /* exponent of difference, buffers */ double sum; /* sum of weighted differences */ assert(ftab); /* check the function argument */ if (ftab->known < EPSILON) return 0; e = (params) ? *params : 1.0; /* get the sensitivity parameter */ if (e <= 0.0) e = 1.0; /* an exponent less than 0 is useless */ if (e == 1.0) m = 1; /* (exponent of absolute difference) */ else if (e == 2.0) m = 2; /* and create a flag to distinguish */ else m = 0; /* the two simplest special cases */ sum = 0; /* traverse the joint distribution */ c = ftab->frq_xy +ftab->xcnt; /* and the column distribution */ for (fx = ftab->frq_x +(x = ftab->xcnt); --x >= 0; --c) { if (*--fx <= 0) continue; /* skip empty and combined columns */ s = 0; /* traverse the row distribution */ fxy = *c +ftab->ycnt; /* and the rows of the column */ fy = ftab->frq_y +(y = ftab->ycnt); switch (m) { /* distinguish special cases */ case 1: while (--y >= 0){/* if exponent for difference is 1 */ t = *fx * *--fy -*--fxy *ftab->known; s += *fxy *fabs(t); } break; /* sum weighted absolute differences */ case 2: while (--y >= 0){/* if exponent for difference is 2 */ t = *fx * *--fy -*--fxy *ftab->known; s += *fxy *t *t; } break; /* sum weighted squared differences */ default: while (--y >= 0){/* if any other exponent */ t = *fx * *--fy -*--fxy *ftab->known; s += *fxy *pow(fabs(t), e); } break; /* raise the differences to */ } /* the given power and sum them */ sum += s; /* sum the results for the columns */ } t = ftab->known; /* normalize the sum */ return sum /(pow(t*t, e) *((measure & FEF_WGTD) ? ftab->frq : t));} /* _wdiff() */ /* return sum of weighted differences *//*--------------------------------------------------------------------*/static double _chi2 (FRQTAB *ftab, int measure, double *params){ /* --- chi^2 measure */ int x, y; /* loop variables */ double **c; /* to traverse the table columns */ double *fx, *fy, *fxy; /* to traverse the frequencies */ double s, t, p; /* temporary buffers */ double chi2; /* chi^2 measure */ assert(ftab); /* check the function argument */ if (ftab->known < EPSILON) return 0; chi2 = 0; /* traverse the joint distribution */ c = ftab->frq_xy +ftab->xcnt; /* and the column distribution */ for (fx = ftab->frq_x +(x = ftab->xcnt); --x >= 0; --c) { if (*--fx <= 0) continue; /* skip empty and combined columns */ s = 0; /* traverse the rows of the column */ fxy = *c +ftab->ycnt; /* and the row distribution */ for (fy = ftab->frq_y +(y = ftab->ycnt); --y >= 0; ) { --fxy; /* go to the next row */ if (*--fy <= 0) continue; /* traverse non-empty rows */ p = *fx * *fy; /* compute (N(x)N(y) - N N(x,y))^2 */ t = p -*fxy *ftab->known; /* / (N(x)N(y)) */ s += t *t /p; /* and sum these values */ } chi2 += s; /* sum the results for the columns */ } /* (results in chi^2 *N) */ if ((measure & 0xff) == FEM_CHI2NRM) { t = (ftab->xcnt-1) *(ftab->ycnt-1); if (t > 0) chi2 /= t; /* normalize the chi^2 measure by */ } /* dividing by the degrees of freedom */ return chi2 /((measure & FEF_WGTD) ? ftab->frq : ftab->known);} /* _chi2() */ /* return the chi^2 measure *//*--------------------------------------------------------------------*/static double _wevid (FRQTAB *ftab, int measure, double *params){ /* --- weight of evidence */ int x, y; /* loop variables */ double **c; /* to traverse the table columns */ double *fx, *fy, *fxy; /* to traverse the frequencies */ double outer, inner; /* inner and outer sum */ double a, b, z; /* temporary buffers */ assert(ftab); /* check the function argument */ if (ftab->known < EPSILON) return 0; outer = 0; /* traverse the joint distribution */ c = ftab->frq_xy +ftab->xcnt; /* and the column distribution */ for (fx = ftab->frq_x +(x = ftab->xcnt); --x >= 0; --c) { if (*--fx <= 0) continue; /* skip empty and combined columns */ inner = 0; /* traverse the rows of the column */ fxy = *c +ftab->ycnt; /* and the row distribution */ for (fy = ftab->frq_y +(y = ftab->ycnt); --y >= 0; ) { z = *--fy * *--fxy; /* compute the common term */ a = *fxy* ftab->known -z; /* and the terms of */ b = *fx * *fy -z; /* the odds fraction */ if ((a >= EPSILON) && (b >= EPSILON)) inner += *fy *fabs(log(a/b)); } /* compute the inner sum */ outer += *fx *inner; /* compute the outer sum */ } outer /= LN_2 *ftab->known; /* scale and return the result */ return outer /((measure & FEF_WGTD) ? ftab->frq : ftab->known);} /* _wevid() *//*--------------------------------------------------------------------*/static double _relev (FRQTAB *ftab, int measure, double *params){ /* --- relevance */ int x, y; /* loop variables */ double **c; /* to traverse the table columns */ double *fx, *fy, *fxy; /* to traverse the frequencies */ double max, tmp; /* (maximal) frequency ratio */ double relev = 0, sum; /* relevance measure, buffer */ assert(ftab); /* check the function argument */ if (ftab->known < EPSILON) return 0; c = ftab->frq_xy +ftab->xcnt; /* traverse the joint distribution */ for (fx = ftab->frq_x +(x = ftab->xcnt); --x >= 0; --c) { if (*--fx <= 0) continue; /* skip empty and combined columns */ max = sum = 0; /* traverse the rows of the column */ fxy = *c +ftab->ycnt; /* and the row distribution */ for (fy = ftab->frq_y +(y = ftab->ycnt); --y >= 0; ) { --fy; /* go to the next row */ if (*--fxy <= 0) continue;/* skip zero frequencies */ tmp = *fxy / *fy; /* compute the frequency ratio */ if (tmp <= max) sum += tmp; else { sum += max; max = tmp; } } /* sum ratios with the exception */ relev += sum; /* of the/one maximal ratio */ } relev = 1 -relev /(ftab->ycnt-1); if (measure & FEF_WGTD) return relev *(ftab->known/ftab->frq); return relev; /* return the relevance measure */} /* _relev() *//*--------------------------------------------------------------------*/static double _bdm (FRQTAB *ftab, int measure, double *params){ /* --- Bayesian-Dirichlet metric */ int x, y; /* loop variables */ double **c; /* to traverse the table columns */ double *fx, *fy, *fxy; /* to traverse the frequencies */ double p, s, a; /* (sum of) prior(s), sensitivity */ double bdm, r, t, z; /* Bayesian-Dirichlet metric, buffers */ assert(ftab); /* check the function argument */ if (ftab->known < EPSILON) return 0; a = (params && (params[0]+1 > 0)) ? params[0]+1 : 1; a *= a; /* get the sensitivity parameter */ p = (params && (params[1] != 0)) ? params[1] : 1; if (p < 0) p /= -ftab->ycnt; /* get the prior/equiv. sample size */ for (r = 0, fy = ftab->frq_y +(y = ftab->ycnt); --y >= 0; ) r += logGa(*--fy *a +p); /* process the class distribution */ s = ftab->ycnt *p; /* and compute the reference value */ r += -ftab->ycnt *logGa(p) +(logGa(s) -logGa(ftab->known *a +s)); if (params && (params[1] < 0))/* adapt prior for l. equiv. version */ s = ftab->ycnt *(p /= ftab->xcnt); z = logGa(s) -ftab->ycnt *logGa(p); c = ftab->frq_xy +ftab->xcnt; /* process the joint distribution */ for (bdm = 0, fx = ftab->frq_x +(x = ftab->xcnt); --x >= 0; --c) { if (*--fx < 0) continue; /* skip empty and combined columns */ for (t = 0, fxy = *c +(y = ftab->ycnt); --y >= 0; ) t += logGa(*--fxy *a +p); /* process a conditional distribution */ bdm += t +z -logGa(*fx *a +s); } /* sum the results over the columns */ bdm -= r; /* and return the weighted BD metric */ return bdm /(LN_2 *((measure & FEF_WGTD) ? ftab->frq : ftab->known));} /* _bdm() *//*--------------------------------------------------------------------*/static double _bdmod (FRQTAB *ftab, int measure, double *params){ /* --- modified BD / K2 metric 2 */ int x, y; /* loop variables */ double **c; /* to traverse the table columns */ double *fx, *fy, *fxy; /* to traverse the frequencies */ double p, a; /* prior and sensitivity */ double bdm, r, t, z; /* Bayesian-Dirichlet metric, buffers */ assert(ftab); /* check the function argument */ if (ftab->known < EPSILON) return 0; a = (params && (params[0] >= 0)) ? params[0] : 0; if (a < 0) a = 0; /* get the sensitivity parameter */ p = (params && (params[1] != 0)) ? params[1] : 1; if (p < 0) p /= -ftab->ycnt; /* get the prior/equiv. sample size */ for (r = 0, fy = ftab->frq_y +(y = ftab->ycnt); --y >= 0; ) { z = *--fy *a +p; r += logGa(z +*fy) -logGa(z); } z = ftab->known *a +ftab->ycnt *p; /* process the */ r += logGa(z) -logGa(z +ftab->known); /* class distribution */ if (params && (params[1] < 0))/* adapt the prior for the */ p /= ftab->xcnt; /* likelihood equivalent version */ c = ftab->frq_xy +ftab->xcnt; /* and the column distribution */ for (bdm = 0, fx = ftab->frq_x +(x = ftab->xcnt); --x >= 0; --c) { if (*--fx < 0) continue; /* skip empty and combined columns */ for (t = 0, fxy = *c +(y = ftab->ycnt); --y >= 0; ) { z = *--fxy *a +p; t += logGa(z +*fxy) -logGa(z); } z = *fx *a +ftab->ycnt *p; /* process a conditional distribution */ bdm += t +(logGa(z) -logGa(z +*fx)); } /* sum the results over the columns */ bdm -= r; /* and return the weighted BD metric */ return bdm /(LN_2 *((measure & FEF_WGTD) ? ftab->frq : ftab->known));} /* _bdmod() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -