static int stat_dbinom (lua_State *L) { /* stack should contain s, xn, pr */ lua_Number s = luaL_checknumber(L, 1); lua_Number xn = luaL_checknumber(L, 2); lua_Number pr = luaL_checknumber(L, 3); check_binom(L, 1, s, xn, pr); s = FORCE_INT(s); xn = FORCE_INT(xn); lua_pushnumber(L, dbinom_raw(s, xn, pr, 1 - pr)); return 1; }
static int cdf_dbinom (lua_State *L) { /* stack should contain s, xn, pr */ lua_Number s = luaL_checknumber(L, 1); lua_Number xn = luaL_checknumber(L, 2); lua_Number pr = luaL_checknumber(L, 3); lua_Number d; check_binom(L, 1, s, xn, pr); xn -= s; d = s*log(pr)+xn*log(1-pr); xn++; s++; /* gamma correction */ d = exp(d-dlnbet(&xn, &s)-log(xn+s-1)); lua_pushnumber(L, d); return 1; }
static int cdf_pbinom (lua_State *L) { /* stack should contain s, xn, pr */ lua_Number s = luaL_checknumber(L, 1); lua_Number xn = luaL_checknumber(L, 2); lua_Number pr = luaL_checknumber(L, 3); lua_Number p, q, ompr, bound; int which = 1; int status; check_binom(L, 1, s, xn, pr); ompr = 1-pr; cdfbin(&which, &p, &q, &s, &xn, &pr, &ompr, &status, &bound); check_status(status, bound); lua_pushnumber(L, p); return 1; }
static int cdf_qbinom (lua_State *L) { /* stack should contain p, xn, pr */ lua_Number p = luaL_checknumber(L, 1); lua_Number xn = luaL_checknumber(L, 2); lua_Number pr = luaL_checknumber(L, 3); lua_Number s; int si; check_binom(L, 2, p, xn, pr); if (p==0 || p==1) s = p*xn; else { lua_Number q = 1-p; lua_Number ompr = 1-pr; lua_Number bound; int which = 2; int status; cdfbin(&which, &p, &q, &s, &xn, &pr, &ompr, &status, &bound); check_status(status, bound); } lua_number2int(si, s); lua_pushinteger(L, si); return 1; }