double power_for_sigma(double sigma, int numsum, double numtrials) /* Return the approximate summed power level required */ /* to get a Gaussian significance of 'sigma', taking */ /* into account the number of independent trials. */ { int which, status; double p, q, x, bound, mean = 0.0, sd = 1.0, df, scale = 1.0; which = 1; status = 0; x = sigma; cdfnor(&which, &p, &q, &x, &mean, &sd, &status, &bound); if (status) { printf("\nError in cdfnor() (power_for_sigma()):\n"); printf(" cdfstatus = %d, bound = %g\n\n", status, bound); printf(" p = %g, q = %g, x = %g, mean = %g, sd = %g\n\n", p, q, x, mean, sd); exit(1); } q = q / numtrials; p = 1.0 - q; which = 2; df = 2.0 * numsum; status = 0; cdfchi(&which, &p, &q, &x, &df, &status, &bound); if (status) { printf("\nError in cdfchi() (power_for_sigma()):\n"); printf(" status = %d, bound = %g\n", status, bound); printf(" p = %g, q = %g, x = %g, df = %g, scale = %g\n\n", p, q, x, df, scale); exit(1); } return 0.5 * x; }
double chi2_logp(double chi2, int dof) /* Return the natural log probability corresponding to a chi^2 value */ /* of chi2 given dof degrees of freedom. */ { double logp; if (chi2 <= 0.0) { return -INFINITY; } if (chi2 / dof > 15.0 || (dof > 150 && chi2 / dof > 6.0)) { // printf("Using asymtotic expansion...\n"); // Use some asymtotic expansions for the chi^2 distribution // this is eqn 26.4.19 of A & S logp = log_asymtotic_incomplete_gamma(0.5 * dof, 0.5 * chi2) - log_asymtotic_gamma(0.5 * dof); } else { int which, status; double p, q, bound, df = dof, x = chi2; which = 1; status = 0; /* Determine the basic probability */ cdfchi(&which, &p, &q, &x, &df, &status, &bound); if (status) { printf("\nError in cdfchi() (chi2_logp()):\n"); printf(" status = %d, bound = %g\n", status, bound); printf(" p = %g, q = %g, x = %g, df = %g\n\n", p, q, x, df); exit(1); } // printf("p = %.3g q = %.3g\n", p, q); logp = log(q); } return logp; }
inline double dcdflib_chi_quantile(double p, double df) { int what = 2; int status = 0; double x, bound, q(1 - p); cdfchi(&what, &p, &q, &x, &df, &status, &bound); return x; }
inline double dcdflib_chi_cdf(double x, double df) { int what = 1; int status = 0; double p, q, bound; cdfchi(&what, &p, &q, &x, &df, &status, &bound); return p; }
void V_cdfchi(int *which, double *p, double *q, double *x, double *df, int *status, double *bound, int *len) { int i; for (i = 0; i < *len; i++) { cdfchi((int *)which, &p[i], &q[i], &x[i], &df[i], (int *)&status[i], &bound[i]); } }
double chistat(double x, double df) { double p, q, d1 = 1.0, bound; int status, which = 1; if (x == 0.0) return 1.0; // if (x<0.0 && x>-1.0) return 1.0; if (x<0.0) return 1.0; /* do not worry about negative lrt values */ cdfchi(&which, &p, &q, &x, &df, &status, &bound); if (status != 0) dcerror(1,"cdfchi() failed in chistat(%f,%f)", x,df); return q; }
double QCHISQ(double p, int m) { int which, status; double chisq, q, df, bound; which = 2; df = (double) m; q = 1.0 - p; cdfchi(&which, &p, &q, &chisq, &df, &status, &bound); if (status == 0) return chisq; else { Warning("Error in computing chi-square; returning -1"); return -1; } }
static int cdf_pchisq (lua_State *L) { /* stack should contain x, df and opt. pnonc */ lua_Number x = luaL_checknumber(L, 1); lua_Number df = luaL_checknumber(L, 2); lua_Number pnonc = luaL_optnumber(L, 3, 0); lua_Number p, q, bound; int which = 1; int status; check_chisq(L, 1, x, df, pnonc); if (pnonc==0) /* central? */ cdfchi(&which, &p, &q, &x, &df, &status, &bound); else /* non-central */ cdfchn(&which, &p, &q, &x, &df, &pnonc, &status, &bound); check_status(status, bound); lua_pushnumber(L, p); return 1; }
static int cdf_qchisq (lua_State *L) { /* stack should contain p, df and opt. pnonc */ lua_Number p = luaL_checknumber(L, 1); lua_Number df = luaL_checknumber(L, 2); lua_Number pnonc = luaL_optnumber(L, 3, 0); lua_Number x; check_chisq(L, 2, p, df, pnonc); if (p==0 || p==1) x = (p==0) ? 0 : HUGE_VAL; else { lua_Number q = 1-p; lua_Number bound; int which = 2; int status; if (pnonc==0) /* central? */ cdfchi(&which, &p, &q, &x, &df, &status, &bound); else /* non-central */ cdfchn(&which, &p, &q, &x, &df, &pnonc, &status, &bound); check_status(status, bound); } lua_pushnumber(L, x); return 1; }