def LogisticRegressionMethod(X, y, ind = 0, alpha = 0.05, beta = 0.2): statmodel = ut.define_model(y) w_hat0 = ut.get_params(np.delete(X, ind, axis = 1), y) w_hat1 = ut.get_params(X, y) predict0 = statmodel(y, np.delete(X, ind, axis = 1)).predict(w_hat0) predict1 = statmodel(y, X).predict(w_hat1) fpr0, tpr0, threshold0 = roc_curve(y, predict0) fpr1, tpr1, threshold1 = roc_curve(y, predict1) c0 = threshold0[np.argmax((tpr0 - threshold0)**2 - (fpr0 - threshold0)**2)] c1 = threshold1[np.argmax((tpr1 - threshold1)**2 - (fpr1 - threshold1)**2)] p0 = np.mean(predict0 > c0) p1 = np.mean(predict1 > c0) t_alpha = sps.norm.ppf(1 - 0.5*alpha) t_beta = sps.norm.ppf(1 - beta) m_size = ((np.sqrt(p0*(1-p0))*t_alpha+t_beta*np.sqrt(p1*(1-p1)))**2)/((p0-p1)**2) return {'m*': int(m_size), }
def wald(X, y, ind_u = None, epsilon = 0.2, alpha = 0.05, beta = 0.2): m, n = X.shape statmodel = ut.define_model(y) if ind_u is None: ind_u = np.concatenate([np.ones(n // 2), np.zeros(n - n//2)]).astype(bool) # ind_u = np.concatenate([np.ones(1), np.zeros(n-1)]).astype(bool) model = statmodel(y, X) w_hat = ut.get_params(X, y) wu0 = w_hat[ind_u]+ epsilon wv_hat = minimize(ut.fix_variables(ut.negative_func(model.loglike_fixed), wu0, ind_u), np.zeros(X.shape[1] - ind_u.sum()), jac = ut.fix_variables(ut.negative_func(model.score_fixed), wu0, ind_u, 1), hess = ut.fix_variables(ut.negative_func(model.hessian_fixed), wu0, ind_u, 2), method = 'Newton-CG')['x'] w_0 = ut.stitch_vectors(wu0, wv_hat, ind_u) V = np.array(np.linalg.inv(-model.hessian_fixed(w_hat))[ind_u][:,ind_u], ndmin=2) V_star = np.array(np.linalg.inv(-model.hessian_fixed(w_0))[ind_u][:,ind_u], ndmin=2) Sigma = m*V Sigma_star = m*V_star w_u = w_hat[ind_u] w_u0 = w_0[ind_u] delta = np.dot((w_u - w_u0), np.linalg.inv(Sigma)@(w_u - w_u0)) classification = len(list(set(list(y)))) == 2 if classification: alpha_star = ut.fix_alpha(alpha, Sigma, Sigma_star) else: alpha_star = alpha gamma_star = ut.get_gamma(ind_u, alpha_star, beta) m_star = np.ceil(gamma_star/delta).astype(int) return {'m*':m_star}
async def handle_danr(message: "Message", trigger_type: str, trigger: str) -> None: """ Handle the booru danr request Args: message: Discord message object related to this request trigger_type: the trigger type that called this function ('author', 'first_word', or 'contains') trigger: the relevant string from the message that triggered this call """ await message.channel.trigger_typing() await process_request(message.channel, 1, get_params(message))
async def handle_choose(message: "Message", trigger_type: str, trigger: str) -> None: """ Handler to pick a random word from a message Args: message: Discord message object related to this request trigger_type: the trigger type that called this function ('author', 'first_word', or 'contains') trigger: the relevant string from the message that triggered this call """ options = get_params(message) await message.channel.send(random.choice(options))
async def process_request(self, message: "Message") -> Any: """ Process a request for a reminder Args: message: Discord message object related to this request """ params = get_params(message) to_remind = params[0] # Parse out who to remind remind_user = None # For 'me', set remind user as author if to_remind.lower() == "me": remind_user = message.author # For a mention, get the user object else: # Extract just the user id from the mentions to_remind_id = int( to_remind.replace("<@", "").replace("!", "").replace(">", "")) for user in message.mentions: if user.id == to_remind_id: remind_user = user if remind_user is None: # User was never set; invalid request msg = "Invalid <user>\n" + usage return await message.channel.send(msg) # Parse out number integer try: remind_offset = int(params[1]) except Exception: msg = "Invalid <number>\n" + usage return await message.channel.send(msg) # Parse out time unit try: remind_multiplier = offset_map[params[2].lower()] except Exception: msg = "Invalid <time_unit>\n" + usage await message.channel.send(msg) return remind_time = time.time() + (remind_offset * remind_multiplier) # Get the raw message after params raw_message = message.content[message.content.find(params[2]) + len(params[2]):] with lock: heapq.heappush( self.jobs, RemindEvent(remind_user.id, remind_time, raw_message)) await message.channel.send("ok")
def lagrange(X, y, ind_u = None, epsilon = 0.2, alpha = 0.05, beta = 0.2): m, n = X.shape statmodel = ut.define_model(y) if ind_u is None: ind_u = np.concatenate([np.ones(n // 2), np.zeros(n - n//2)]).astype(bool) # ind_u = np.concatenate([np.ones(1), np.zeros(n-1)]).astype(bool) ind_v = ind_u == False model = statmodel(y, X) w_hat = ut.get_params(X, y) mu = model.predict(w_hat) if len(list(set(list(y)))) == 2: v = mu*(1-mu) else: v = np.ones_like(y)*(mu-y).var() wu0 = w_hat[ind_u] + epsilon wv_hat = minimize(ut.fix_variables(ut.negative_func(model.loglike_fixed), wu0, ind_u), np.zeros(ind_v.sum()), jac = ut.fix_variables(ut.negative_func(model.score_fixed), wu0, ind_u, 1), hess = ut.fix_variables(ut.negative_func(model.hessian_fixed), wu0, ind_u, 2), method = 'Newton-CG')['x'] w_0 = ut.stitch_vectors(wu0, wv_hat, ind_u) I = -model.hessian_fixed(w_0) I_muv = I[ind_u][:,ind_v] I_mvv = I[ind_v][:,ind_v] Z_star = (X[:,ind_u].T - I_muv @ np.linalg.inv(I_mvv) @ X[:,ind_v].T).T Z_star_matrices = np.asarray([Z_star[i,None].T @ Z_star[i, None] for i in range(m)]) delta = np.ones_like(y) mu_star = model.predict(w_0) xi_m = (((mu - mu_star)*delta[None,:]).T * Z_star).sum(0) Sigma_m = ((v * delta**2).reshape(-1,1,1) * Z_star_matrices).sum(0) gamma_0 = (xi_m @ np.linalg.inv(Sigma_m) @ xi_m)/m gamma = ut.get_gamma(ind_u, alpha, beta) m_star = np.ceil(gamma/gamma_0).astype(int) return {'m*': m_star}
def likelihood_ratio(X, y, ind_u=None, epsilon = 0.2, alpha = 0.05, beta = 0.2): m, n = X.shape statmodel = ut.define_model(y) if ind_u is None: ind_u = np.concatenate([np.ones(n // 2), np.zeros(n - n//2)]).astype(bool) # ind_u = np.concatenate([np.ones(1), np.zeros(n-1)]).astype(bool) model = statmodel(y, X) w_hat = ut.get_params(X, y) wu0 = w_hat[ind_u] + epsilon wv_hat = minimize(ut.fix_variables(ut.negative_func(model.loglike_fixed), wu0, ind_u), np.zeros(X.shape[1] - ind_u.sum()), jac = ut.fix_variables(ut.negative_func(model.score_fixed), wu0, ind_u, 1), hess = ut.fix_variables(ut.negative_func(model.hessian_fixed), wu0, ind_u, 2), method = 'Newton-CG')['x'] w_0 = ut.stitch_vectors(wu0, wv_hat, ind_u) theta = X @ w_hat theta_star = X @ w_0 if len(list(set(list(y)))) == 2: a = 1. b = lambda w: -np.log(1 - model.predict(w) + 1e-30) grad_b = lambda w: model.predict(w) else: a = 2*((y - theta).std())**2 b = lambda w: model.predict(w)**2 grad_b = lambda w: 2*model.predict(w) delta_star = 2*(1/a)*((theta-theta_star)*grad_b(w_hat) - b(w_hat) + b(w_0)).mean() gamma_star = ut.get_gamma(ind_u, alpha, beta) m_star = np.ceil(gamma_star/delta_star).astype(int) return {'m*': m_star}
async def handle_spam(message: "Message", trigger_type: str, trigger: str) -> None: """ Handle the booru spam request Args: message: Discord message object related to this request trigger_type: the trigger type that called this function ('author', 'first_word', or 'contains') trigger: the relevant string from the message that triggered this call """ params = get_params(message) try: amount = int(params[0]) if amount < 1: await message.channel.send(":thinking:") return except Exception: await message.channel.send( "Usage: `spam <amount> <optional space seperated tags>`") return params = params[1:] await message.channel.trigger_typing() await process_request(message.channel, amount, params)