static int cdf_dbeta (lua_State *L) { /* stack should contain x, a and b */ lua_Number x = luaL_checknumber(L, 1); lua_Number a = luaL_checknumber(L, 2); lua_Number b = luaL_checknumber(L, 3); check_beta(L, 1, x, a, b); lua_pushnumber(L, (x==0 || x==1) ? 0 : exp((a-1)*log(x)+(b-1)*log(1-x)-dlnbet(&a, &b))); return 1; }
static int cdf_dnbinom (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_nbinom(L, 1, s, xn, pr); d = exp(xn*log(pr)+s*log(1-pr)-dlnbet(&s, &xn))/s; lua_pushnumber(L, d); return 1; }
static int cdf_dt (lua_State *L) { /* stack should contain x and df */ lua_Number x = luaL_checknumber(L, 1); lua_Number df = luaL_checknumber(L, 2); lua_Number t = 0.5; lua_Number d; check_t(L, 1, x, df); d = df/2; d = -dlnbet(&d, &t)-(df+1)/2*log(1+x*x/df); d = exp(d)/sqrt(df); lua_pushnumber(L, d); 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_df (lua_State *L) { /* stack should contain f, dfn, dfd */ lua_Number f = luaL_checknumber(L, 1); lua_Number dfn = luaL_checknumber(L, 2); lua_Number dfd = luaL_checknumber(L, 3); lua_Number df1, df2, r, d; check_f(L, 1, f, dfn, dfd); df1 = dfn/2; df2 = dfd/2; r = dfn/dfd; d = df1*log(r)+(df1-1)*log(f); d -= (df1+df2)*log(1+r*f); d -= dlnbet(&df1, &df2); lua_pushnumber(L, exp(d)); return 1; }
static int cdf_lbeta (lua_State *L) { lua_Number a = luaL_checknumber(L, 1); lua_Number b = luaL_checknumber(L, 2); lua_pushnumber(L, dlnbet(&a, &b)); return 1; }
static int fchoose (lua_Number n, lua_Number k) { lua_Number a = n - k + 1; lua_Number b = k + 1; return -dlnbet(&a, &b) - log(n + 1); }