def draw(self): """ Sample from the categorical distribution, and payout corresponding to the selected index :return: payouts[i], where i is sampled from the categorical distribution given by pvals """ return random.multinomial(1, pvals=self.pvals).dot(self.payouts)
def draw(self): """ if sample_priors = True and random_sample = True: draw returns a random draw of a categorical distribution with parameters drawn from a Dirichlet distribution the hyperparameters on the Dirichlet are given by the bandit's metric with laplacian smoothing if sample_priors = False and random_sample = True: draw returns a random draw of a categorical distribution with parameters given by the bandit's metric if sample_priors = True and random_sample = False: draw returns argmax(random.dirichlet((x_0 + 1, ... , x_n_arms + 1))) where x_i is the ith value returned by the bandit's metric. if sample_priors = False and random_sample = False: become a purely greedy bandit with the selected arm given by argmax(metric) :return: The numerical index of the selected arm """ temp = self._schedule_fn(self.total_draws) x = array(self._metric_fn()) * temp + 1 if self.sample_priors: pvals = random.dirichlet(x) else: pvals = x / sum(x) if self.random_sample: return argmax(random.multinomial(1, pvals=pvals)) else: return argmax(pvals)
def draw(self): """ Selects arm i with probability distribution given by the softmax: P(arm_i) = exp(metric_i) / Z Where Z is the normalizing constant: Z = sum(exp(metric_i) for i in range(n_arms)) :return: The numerical index of the selected arm """ return argmax(random.multinomial(1, pvals=softmax(self._metric_fn())))
def draw(self): """ Selects arm i with probability distribution given by the softmax: P(arm_i) = exp(metric_i / temperature) / Z Where Z is the normalizing constant: Z = sum(exp(metric_i / temperature) for i in range(n_arms)) :return: The numerical index of the selected arm """ temp = self._schedule_fn(self.total_draws) return argmax(random.multinomial(1, pvals=softmax(array(self._metric_fn()) / temp)))
cov1 = np.eye(2) mu2 = np.array([5, 3]) cov2 = np.eye(2) * 2 mu3 = np.array([8, 12]) cov3 = np.array([3.4, 0, 0, 5.1]).reshape(2, 2) # multinom params p1 = 0.2 p2 = 0 p3 = 1 - p2 - p1 # random draws rnd.seed(1) knum = rnd.multinomial(draws, (p1, p2, p3)) gaus1 = rnd.multivariate_normal(mu1, cov1, knum[0]) gaus2 = rnd.multivariate_normal(mu2, cov2, knum[1]) gaus3 = rnd.multivariate_normal(mu3, cov3, knum[2]) # join columns into dataframe x1 = pd.Series(np.r_[gaus1[:, 0], gaus2[:, 0], gaus3[:, 0]]) x2 = pd.Series(np.r_[gaus1[:, 1], gaus2[:, 1], gaus3[:, 1]]) c = pd.Series(np.r_[np.zeros(knum[0]), np.ones(knum[1]), np.ones(knum[2]) * 2]) dat = {"x1" : x1, "x2" : x2, "c" : c} clustData = pd.DataFrame(dat) # plot clusters #plt.scatter(clustData["x1"], clustData["x2"], c = clustData["c"]) #plt.show()