static int cdf_dpois (lua_State *L) { /* stack should contain s and xlam */ lua_Number s = luaL_checknumber(L, 1); lua_Number xlam = luaL_checknumber(L, 2); lua_Number d; check_pois(L, 1, s, xlam); d = s+1; d = exp(s*log(xlam)-xlam-dlngam(&d)); lua_pushnumber(L, d); return 1; }
/* scale here is 1/rate */ static int cdf_dgamma (lua_State *L) { /* stack should contain x, shape and opt. scale */ lua_Number x = luaL_checknumber(L, 1); lua_Number shape = luaL_checknumber(L, 2); lua_Number scale = luaL_optnumber(L, 3, 1); lua_Number d; check_gamma(L, 1, x, shape, scale); d = x * scale; d = exp(shape*log(d)-d-dlngam(&shape))/x; lua_pushnumber(L, d); return 1; }
static int cdf_dchisq (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 = 0; lua_Number d; check_chisq(L, 1, x, df, pnonc); /* compute central dchisq */ d = df/2; d = exp((d-1)*log(x)-x/2-d*M_LN2-dlngam(&d)); lua_pushnumber(L, d); return 1; }
static int stat_dchisq (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 d; check_chisq(L, 1, x, df, pnonc); /* compute central dchisq */ d = df / 2; d = exp((d - 1) * log(x) - x / 2 - d * M_LN2 - dlngam(&d)); /* compute non-central if that's the case */ if (pnonc != 0) { /* non-central? */ /* evaluate weighted series */ int i; lua_Number t = d *= exp(-pnonc / 2); /* first term */ for (i = 1; i < MAXITER && d > LBOUND && t > DBL_EPSILON * d; i++) d += t *= x * pnonc / (2 * i * (df + 2 * (i - 1))); } lua_pushnumber(L, d); return 1; }
static int cdf_lgamma (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); lua_pushnumber(L, dlngam(&x)); return 1; }