def gen(shape1, shape2, name): """Generate fixture data and write to file. # Arguments * `shape1`: first shape parameter * `shape2`: second shape parameter * `name::str`: output filename # Examples ``` python python> alpha = rand(1000) * 10.0 python> beta = rand(1000) * 10.0 + alpha python> gen(alpha, beta, './data.json') ``` """ y = list() for a, b in np.nditer([shape1, shape2]): y.append(beta.std(a, b)) # Store data to be written to file as a dictionary: data = {"alpha": shape1.tolist(), "beta": shape2.tolist(), "expected": y} # Based on the script directory, create an output filepath: filepath = os.path.join(DIR, name) # Write the data to the output filepath as JSON: with open(filepath, "w") as outfile: json.dump(data, outfile)
def run_one_step(self): # Pick the best one with consideration of upper confidence bounds. i = max(range(4), key=lambda x: self._as[x] / float(self._as[x] + self._bs[x]) + beta.std(self._as[x], self._bs[x]) * self.c) self.last_played = i return i
def _run_one_step(self): # Pick the best one with consideration of upper confidence bounds # Break ties at random n_act = self.bandit.n_actions maxi = max(range(n_act), key=lambda x: self._as[x] / float(self._as[x] + self._bs[x]) + self.c * beta.std(self._as[x], self._bs[x])) maxactval = self.actvals[maxi] # all max value action indices maxidx = [i for i in range(n_act) if self.actvals[i] >= maxactval] # choose one at random i = int(np.random.choice(maxidx, 1)) r = self.bandit.get_reward(i) # Update posterior self._as[i] += r self._bs[i] += (1 - r) # update the estimated action values self.actvals = [ self._as[i] / float(self._as[i] + self._bs[i]) for i in range(n_act) ] # return action and reward return i, r
def beta_estimation(_, rewards): global GLOBAL_CACHE key = (tuple(rewards), 'beta') if key in GLOBAL_CACHE: return GLOBAL_CACHE[key] trials = len(rewards) GLOBAL_CACHE[key] = (beta.mean(1 + sum(rewards), trials+1 - sum(rewards)), beta.std(1 + sum(rewards), trials+1 - sum(rewards))) return GLOBAL_CACHE[key]
def create_distributions(listD=[(0.5, 0.5), (5.0, 1.0), (1.0, 3.0), (2.0, 2.0), (2.0, 5.0), (0.8, 7.2), (1.4, 0.6)]): distributions = {} listD = [(0.5, 0.5), (5.0, 1.0), (1.0, 3.0), (2.0, 2.0), (2.0, 5.0), (0.8, 7.2), (1.4, 0.6)] for i in listD: (a, b, c, d) = create_normalized_beta(i[0], i[1]) dist = beta(a, b, c, d) s = "beta(" + str(a) + "," + str(b) + "," + str(c) + "," + str(d) + ")" distributions[s] = dist print s, "\tmean=\t", beta.stats(a, b, c, d, "m"), "\tstd=\t", beta.std(a, b, c, d) return distributions
def run_one_step(self): # Pick the best one with consideration of upper confidence bounds. i = max(range(self.bandit.n), key=lambda x: self._as[x] / float(self._as[x] + self._bs[x]) + beta.std(self._as[x], self._bs[x]) * self.c) r = self.bandit.generate_reward(i) # Update Gaussian posterior self._as[i] += r self._bs[i] += (1 - r) return i
def test_transformation_composition_II(self): num_vars = 2 alpha_stat = 5 beta_stat = 2 def beta_cdf(x): return beta_rv.cdf(x, a=alpha_stat, b=beta_stat) def beta_icdf(x): return beta_rv.ppf(x, a=alpha_stat, b=beta_stat) x_marginal_cdfs = [beta_cdf]*num_vars x_marginal_inv_cdfs = [beta_icdf]*num_vars x_marginal_means = np.asarray( [beta_rv.mean(a=alpha_stat, b=beta_stat)]*num_vars) x_marginal_stdevs = np.asarray( [beta_rv.std(a=alpha_stat, b=beta_stat)]*num_vars) def beta_pdf(x): return beta_rv.pdf(x, a=alpha_stat, b=beta_stat) x_marginal_pdfs = [beta_pdf]*num_vars z_correlation = -0.9*np.ones((num_vars, num_vars)) for ii in range(num_vars): z_correlation[ii, ii] = 1. x_correlation = gaussian_copula_compute_x_correlation_from_z_correlation( x_marginal_inv_cdfs, x_marginal_means, x_marginal_stdevs, z_correlation) x_covariance = correlation_to_covariance( x_correlation, x_marginal_stdevs) var_trans_1 = NatafTransformation( x_marginal_cdfs, x_marginal_inv_cdfs, x_marginal_pdfs, x_covariance, x_marginal_means) # rosenblatt maps to [0,1] but polynomials of bounded variables # are in [-1,1] so add second transformation for this second mapping def normal_cdf(x): return normal_rv.cdf(x) def normal_icdf(x): return normal_rv.ppf(x) std_normal_marginal_cdfs = [normal_cdf]*num_vars std_normal_marginal_inv_cdfs = [normal_icdf]*num_vars var_trans_2 = UniformMarginalTransformation( std_normal_marginal_cdfs, std_normal_marginal_inv_cdfs) var_trans = TransformationComposition([var_trans_1, var_trans_2]) num_samples = 1000 true_samples, true_canonical_samples = \ generate_x_samples_using_gaussian_copula( num_vars, z_correlation, x_marginal_inv_cdfs, num_samples) true_canonical_samples = normal_rv.cdf(true_canonical_samples) samples = var_trans.map_from_canonical_space( true_canonical_samples) assert np.allclose(true_samples, samples) canonical_samples = var_trans.map_to_canonical_space(samples) assert np.allclose(true_canonical_samples, canonical_samples)
def agent(observation, configuration): global total_reward, bandit, post_a, post_b, c if observation.step == 0: post_a, post_b = np.ones((2, configuration.banditCount)) else: r = observation.reward - total_reward total_reward = observation.reward # Update Gaussian posterior post_a[bandit] += r post_b[bandit] += 1 - r bound = post_a / (post_a + post_b) + beta.std(post_a, post_b) * c bandit = int(np.argmax(bound)) return bandit
def test_nataf_transformation(self): num_vars = 2 alpha_stat = 2 beta_stat = 5 bisection_opts = {'tol': 1e-10, 'max_iterations': 100} def beta_cdf(x): return beta_rv.cdf(x, a=alpha_stat, b=beta_stat) def beta_icdf(x): return beta_rv.ppf(x, a=alpha_stat, b=beta_stat) x_marginal_cdfs = [beta_cdf]*num_vars x_marginal_inv_cdfs = [beta_icdf]*num_vars x_marginal_means = np.asarray( [beta_rv.mean(a=alpha_stat, b=beta_stat)]*num_vars) x_marginal_stdevs = np.asarray( [beta_rv.std(a=alpha_stat, b=beta_stat)]*num_vars) def beta_pdf(x): return beta_rv.pdf(x, a=alpha_stat, b=beta_stat) x_marginal_pdfs = [beta_pdf]*num_vars z_correlation = np.array([[1, 0.7], [0.7, 1]]) x_correlation = \ gaussian_copula_compute_x_correlation_from_z_correlation( x_marginal_inv_cdfs, x_marginal_means, x_marginal_stdevs, z_correlation) x_covariance = correlation_to_covariance( x_correlation, x_marginal_stdevs) var_trans = NatafTransformation( x_marginal_cdfs, x_marginal_inv_cdfs, x_marginal_pdfs, x_covariance, x_marginal_means, bisection_opts) assert np.allclose(var_trans.z_correlation, z_correlation) num_samples = 1000 true_samples, true_canonical_samples = \ generate_x_samples_using_gaussian_copula( num_vars, z_correlation, x_marginal_inv_cdfs, num_samples) canonical_samples = var_trans.map_to_canonical_space(true_samples) assert np.allclose(true_canonical_samples, canonical_samples) samples = var_trans.map_from_canonical_space( true_canonical_samples) assert np.allclose(true_samples, samples)
def agent(observation, configuration): global reward_sums, total_reward, bandit, post_a, post_b, c n_bandits = configuration.banditCount if observation.step == 0: post_a = np.ones(n_bandits) post_b = np.ones(n_bandits) else: r = observation.reward - total_reward total_reward = observation.reward # Update Gaussian posterior post_a[bandit] += r post_b[bandit] += (1 - r) bound = post_a / (post_a + post_b).astype(float) + beta.std(post_a, post_b) * c bandit = int(np.argmax(bound)) return bandit
def play(self, observation, configuration): my_index = observation.agentIndex if observation.step == 0: self.initiallize(n_bins=self._n_bins, c=self._c) else: # Extract info from observation. my_last_action = observation.lastActions[my_index] reward = observation.reward - self.total_reward # Extract params self.posterior_a[my_last_action] += reward self.posterior_b[my_last_action] += 1 - reward self.total_reward = observation.reward # Choose action # Compute ucb target function upper_bound = (self.posterior_a / (self.posterior_a + self.posterior_b) + beta.std(self.posterior_a, self.posterior_b) * self.c) return int(np.argmax(upper_bound))
def summary(dbDir): '''create a function to summarize the Monte Carlo simulation. I believe we care about the lengths of the simulated fish and survival % by simulation The only input is the project database''' # first let's get the data we wish to describe conn = sqlite3.connect(dbDir, timeout=30.0) fish = pd.read_sql('SELECT * FROM tblFish', con=conn) survival = pd.read_sql('SELECT * FROM tblSurvive', con=conn) completion = pd.read_sql('SELECT * FROM tblCompletion', con=conn) # let's desribe fish lengths with a histogram # plt.figure(figsize = (6,3)) # fig, ax = plt.subplots() # ax.hist(fish.length.values,10,density = 1) # ax.set_xlabel('Length (ft)') # plt.show() # let's describe survival by node grouped = survival[['simulation', 'location', 'prob_surv', 'status']].groupby(['simulation', 'location']).agg({ 'prob_surv': 'count', 'status': 'sum' }).reset_index().rename(columns={ 'prob_surv': 'n', 'status': 'p' }) grouped['proportion'] = grouped.p / grouped.n # now fit a beta distribution to each node locations = grouped.location.unique() beta_dict = {} for i in locations: dat = grouped.loc[grouped.location == i] params = beta.fit(dat.proportion.values) beta_median = beta.median(params[0], params[1], params[2], params[3]) beta_std = beta.std(params[0], params[1], params[2], params[3]) beta_95ci = beta.interval(alpha=0.95, a=params[0], b=params[1], loc=params[2], scale=params[3]) beta_dict[i] = [beta_median, beta_std, beta_95ci[0], beta_95ci[1]] # now calculate whole project survival whole = completion[['simulation', 'status', 'completion']].groupby(['simulation']).agg({ 'status': 'sum', 'completion': 'sum' }).reset_index().rename(columns={ 'status': 'p', 'completion': 'n' }) whole['proportion'] = whole.p / whole.n params = beta.fit(whole.proportion.values) beta_median = beta.median(params[0], params[1], params[2], params[3]) beta_std = beta.std(params[0], params[1], params[2], params[3]) beta_95ci = beta.interval(alpha=0.95, a=params[0], b=params[1], loc=params[2], scale=params[3]) beta_dict['whole project'] = [ beta_median, beta_std, beta_95ci[0], beta_95ci[1] ] beta_fit_df = pd.DataFrame.from_dict(beta_dict, orient='index', columns=['mean', 'std', 'll', 'ul']) del i, params #print (beta_dict) print(beta_fit_df) # make a plot for each beta distribution - we like visuals! # plt.figure(figsize = (6,3)) # fig,ax = plt.subplots() # for i in beta_dict.keys(): # params = beta_dict[i] # x = np.linspace(beta.ppf(0.01, params[0][0], params[0][1]),beta.ppf(0.99, params[0][0], params[0][1]), 100) # ax.plot(x,beta.pdf(x, params[0][0], params[0][1]),label = i) # fig.legend() c = conn.cursor() c.execute('''CREATE TABLE IF NOT EXISTS tblSummary(Scenario INTEGER, FishLengthMean REAL, FishLengthSD REAL, NumFish INTEGER, NumPassedSuccess INTEGER, PercentSurvival REAL )''') conn.commit() completion = pd.read_sql('SELECT * FROM tblCompletion', conn) beta_fit_df.to_sql("tblBetaFit", con=conn, if_exists='replace') for sim in completion['simulation'].unique(): subset = completion.loc[completion['simulation'] == sim].sum() c.execute("INSERT INTO tblSummary VALUES(%d,%f,%f,%d,%d,%f);" % (sim, fish.length.mean(), fish.length.std(), len(fish), subset['status'], subset['status'] / subset['completion'])) conn.commit() c.close()
def test_correlated_beta(self): num_vars = 2 alpha_stat = 2 beta_stat = 5 bisection_opts = {'tol': 1e-10, 'max_iterations': 100} beta_cdf = lambda x: beta_rv.cdf(x, a=alpha_stat, b=beta_stat) beta_icdf = lambda x: beta_rv.ppf(x, a=alpha_stat, b=beta_stat) x_marginal_cdfs = [beta_cdf] * num_vars x_marginal_inv_cdfs = [beta_icdf] * num_vars x_marginal_means = np.asarray( [beta_rv.mean(a=alpha_stat, b=beta_stat)] * num_vars) x_marginal_stdevs = np.asarray( [beta_rv.std(a=alpha_stat, b=beta_stat)] * num_vars) beta_pdf = lambda x: beta_rv.pdf(x, a=alpha_stat, b=beta_stat) x_marginal_pdfs = [beta_pdf] * num_vars x_correlation = np.array([[1, 0.7], [0.7, 1]]) quad_rule = gauss_hermite_pts_wts_1D(11) z_correlation = transform_correlations(x_correlation, x_marginal_inv_cdfs, x_marginal_means, x_marginal_stdevs, quad_rule, bisection_opts) assert np.allclose(z_correlation[0, 1], z_correlation[1, 0]) x_correlation_recovered = \ gaussian_copula_compute_x_correlation_from_z_correlation( x_marginal_inv_cdfs,x_marginal_means,x_marginal_stdevs, z_correlation) assert np.allclose(x_correlation, x_correlation_recovered) z_variable = multivariate_normal(mean=np.zeros((num_vars)), cov=z_correlation) z_joint_density = lambda x: z_variable.pdf(x.T) target_density = partial(nataf_joint_density, x_marginal_cdfs=x_marginal_cdfs, x_marginal_pdfs=x_marginal_pdfs, z_joint_density=z_joint_density) # all variances are the same so #true_x_covariance = x_correlation.copy()*x_marginal_stdevs[0]**2 true_x_covariance = correlation_to_covariance(x_correlation, x_marginal_stdevs) def univariate_quad_rule(n): x, w = np.polynomial.legendre.leggauss(n) x = (x + 1.) / 2. w /= 2. return x, w x, w = get_tensor_product_quadrature_rule(100, num_vars, univariate_quad_rule) assert np.allclose(np.dot(target_density(x), w), 1.0) # test covariance of computed by aplying quadrature to joint density mean = np.dot(x * target_density(x), w) x_covariance = np.empty((num_vars, num_vars)) x_covariance[0, 0] = np.dot(x[0, :]**2 * target_density(x), w) - mean[0]**2 x_covariance[1, 1] = np.dot(x[1, :]**2 * target_density(x), w) - mean[1]**2 x_covariance[0, 1] = np.dot(x[0, :] * x[1, :] * target_density(x), w) - mean[0] * mean[1] x_covariance[1, 0] = x_covariance[0, 1] # error is influenced by bisection_opts['tol'] assert np.allclose(x_covariance, true_x_covariance, atol=bisection_opts['tol']) # test samples generated using Gaussian copula are correct num_samples = 10000 x_samples, true_u_samples = generate_x_samples_using_gaussian_copula( num_vars, z_correlation, x_marginal_inv_cdfs, num_samples) x_sample_covariance = np.cov(x_samples) assert np.allclose(true_x_covariance, x_sample_covariance, atol=1e-2) u_samples = nataf_transformation(x_samples, true_x_covariance, x_marginal_cdfs, x_marginal_inv_cdfs, x_marginal_means, x_marginal_stdevs, bisection_opts) assert np.allclose(u_samples, true_u_samples) trans_samples = inverse_nataf_transformation( u_samples, x_covariance, x_marginal_cdfs, x_marginal_inv_cdfs, x_marginal_means, x_marginal_stdevs, bisection_opts) assert np.allclose(x_samples, trans_samples)
def get_bayesian_ucb(self): p = self._successes + self.p q = self._failures + self.q n = p + q bucb = p / n.astype(float) + beta.std(p, q) * self.c return bucb
def std(self, n, p): std = beta.std(self, n, p) return std
def calc_score(bandit: Bandit) -> float: n_selections = bandit.n_self_pulls return (bandit.n_wins / float(n_selections) + beta.std(bandit.n_wins, n_selections - bandit.n_wins) * c)
def choose_teacher(self): mean = self._as[x] / (self._as[x] + self._bs[x]) std_scaled = self.c * beta.std(self._as, self._bs) return np.argmax(mean + std_scaled)
whole_proj_succ = scen_results.groupby( by='iteration').survival.sum().to_frame().reset_index( drop=False).rename(columns={'survival': 'successes'}) whole_proj_count = scen_results.groupby( by='iteration').survival.count().to_frame().reset_index( drop=False).rename(columns={'survival': 'count'}) # merge successes and counts whole_summ = whole_proj_succ.merge(whole_proj_count) # calculate probabilities, fit to beta, write to dictionary summarizing results whole_summ['prob'] = whole_summ['successes'] / whole_summ['count'] whole_params = beta.fit(whole_summ.prob.values) whole_median = beta.median(whole_params[0], whole_params[1], whole_params[2], whole_params[3]) whole_std = beta.std(whole_params[0], whole_params[1], whole_params[2], whole_params[3]) whole_95ci = beta.interval(alpha=0.95, a=whole_params[0], b=whole_params[1], loc=whole_params[2], scale=whole_params[3]) beta_dict['%s_%s' % ('whole', scen_num)] = [ whole_median, whole_std, whole_95ci[0], whole_95ci[1] ] # summarize scenario - whole project route_succ = scen_results.groupby( by=['iteration', 'route']).survival.sum().to_frame().reset_index( drop=False).rename(columns={'survival': 'successes'}) route_count = scen_results.groupby( by=['iteration', 'route']).survival.count().to_frame().reset_index(
# The variance = E[X**2] - (E[X])**2 exp_x_squared = np.sum(np.square(p_grid) * posterior) std = np.sqrt(exp_x_squared - mu**2) print(f'posterior mean = {mu}, posterior standard deviation = {std}') norm_approx_posterior = norm.pdf(p_grid, loc=mu, scale=std) # The Beta dist. is a conjugate pair of the binomial dist # More specifically, if X_1, ..., X_n are iid random variables from a Binomial dist. # with parameter p, and p ~ Beta(a, b), then the posterior distribution of p # given X_1 = x_1, ..., X_n = x_n is Beta(a + sum(x_1, ..., x_n), b + n - sum(x_1, ..., x_n)) # Since Uniform(0, 1) = Beta(1, 1), the hyper-parameter update rule after observing water W times # and land L times is a = W + 1 and b = L + 1 W = 6 L = 3 beta_data = beta.pdf(p_grid, W + 1, L + 1) beta_mu = beta.mean(W + 1, L + 1) beta_std = beta.std(W + 1, L + 1) norm_approx = norm.pdf(p_grid, beta_mu, beta_std) # Plot both the analytically obtained posterior and the normal approximation plt.plot(p_grid, beta_data, 'bo-', label='beta') plt.plot(p_grid, norm_approx, 'ro-', label='normal') plt.xlabel('Fraction of water') plt.ylabel('Beta(W=6, L=3)') plt.title(f'Sample= WLWWWLWLW; number of grid points = {NUM_PTS}') plt.legend() plt.show()
def get_action(self, t, bandit): return np.argmax(self.Q[bandit] + beta.std(self.a[bandit], self.b[bandit]) * self.c)
import matplotlib.pyplot as plt import numpy as np from scipy.stats import beta np.random.seed(1) # Beta Distribution a, b = 3.0, 2.0 mean, var = beta.stats(a, b, moments='mv') std = beta.std(a, b, loc=0, scale=1) fig, ax = plt.subplots(1, 1) x = np.linspace(beta.ppf(0.05, a, b), beta.ppf(0.95, a, b)) ax.plot(x, beta.pdf(x, a, b), 'b-', lw=3, alpha=0.6, label='Beta') q1 = beta.ppf(.25, a, b) median = beta.ppf(.5, a, b) q3 = beta.ppf(.75, a, b) plt.title( 'Beta Distribution ($\mu$: {:.2f}, $\sigma$: {:.2f}, $\sigma^2$: {:.2f})'. format(mean, std, var), size='xx-large') plt.xlabel('X', size='large') plt.ylabel('P(X)', size='large') # Quartile lines ax.axvline(x=q1, linewidth=3, alpha=0.6, color='black', linestyle='dashed') ax.axvline(x=median, linewidth=3, alpha=0.6, color='black', linestyle='dashed') ax.axvline(x=q3, linewidth=3, alpha=0.6, color='black', linestyle='dashed')