def TrapezTrunc(TC1, TC2, spread1, spread2, N, linf, lsup): from scipy.stats import trapz # define variables for trapezoidal distribution A = TC1 * (1 - spread1) B = TC2 * (1 + spread2) c = (TC1 - A) / (B - A) d = (TC2 - A) / (B - A) loc = A scale = B - A dist = trapz.rvs(c, d, loc, scale, N) truncdist = [i for i in dist if i >= linf] truncdist = [i for i in truncdist if i <= lsup] while len(truncdist) < N: adddist = trapz.rvs(c, d, loc, scale, N - len(truncdist)) truncadddist = [i for i in adddist if i >= linf] truncadddist = [i for i in truncadddist if i <= lsup] truncdist = truncdist + truncadddist return np.asarray(truncdist)
def TrapezTrunc(TC1, TC2, spread1, spread2, N, linf=float('-inf'), lsup=float('inf')): if TC1+TC2 == 0: return np.asarray([0]*N) # define variables for trapezoidal distribution if TC1 < TC2: A = TC1*(1-spread1) B = TC2*(1+spread2) c = (TC1-A)/(B-A) d = (TC2-A)/(B-A) else: A = TC2*(1-spread2) B = TC1*(1+spread1) c = (TC2-A)/(B-A) d = (TC1-A)/(B-A) loc = A scale = B-A dist = trapz.rvs(c, d, loc, scale, N) truncdist = [i for i in dist if i >= linf] truncdist = [i for i in truncdist if i <= lsup] while len(truncdist) < N: adddist = trapz.rvs(c, d, loc, scale, N-len(truncdist)) truncadddist = [i for i in adddist if i >= linf] truncadddist = [i for i in truncadddist if i <= lsup] truncdist = truncdist + truncadddist return np.asarray(truncdist)
def TrapezTrunc(TC1, TC2, spread1, spread2, N, linf, lsup): if lsup < linf: raise Exception('lsup should be larger than linf') if spread1 < 0 or spread2 < 0: raise Exception('spread should not be below 0') if TC1 > TC2: raise Exception('TC1 should be smaller than TC2') if TC1 + TC2 == 0: if N == 1: return 0 else: return np.asarray([0] * N) # define variables for trapezoidal distribution A = TC1 * (1 - spread1) B = TC2 * (1 + spread2) if B < linf or A > lsup: raise Exception('TC1, TC2, linf and lsup are not compatible') c = (TC1 - A) / (B - A) d = (TC2 - A) / (B - A) loc = A scale = B - A dist = trapz.rvs(c, d, loc, scale, N) truncdist = [i for i in dist if i >= linf] truncdist = [i for i in truncdist if i <= lsup] while len(truncdist) < N: adddist = trapz.rvs(c, d, loc, scale, N - len(truncdist)) truncadddist = [i for i in adddist if i >= linf] truncadddist = [i for i in truncadddist if i <= lsup] truncdist = truncdist + truncadddist if N == 1: return truncdist[0] else: return np.asarray(truncdist)
def get_random_value(self): """ create a trapezoidal distribution based on the fuzzy number and get a random value from it """ # trapezoidal distribution parameters loc = self.value[0] scale = self.value[3] - self.value[0] c = (self.value[1] - self.value[0]) / scale d = (self.value[2] - self.value[0]) / scale # random value from trapezoidal distribution r = trapz.rvs(c, d, loc=loc, scale=scale) return r
# Display the probability density function (``pdf``): x = np.linspace(trapz.ppf(0.01, c, d), trapz.ppf(0.99, c, d), 100) ax.plot(x, trapz.pdf(x, c, d), 'r-', lw=5, alpha=0.6, label='trapz pdf') # Alternatively, the distribution object can be called (as a function) # to fix the shape, location and scale parameters. This returns a "frozen" # RV object holding the given parameters fixed. # Freeze the distribution and display the frozen ``pdf``: rv = trapz(c, d) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = trapz.ppf([0.001, 0.5, 0.999], c, d) np.allclose([0.001, 0.5, 0.999], trapz.cdf(vals, c, d)) # True # Generate random numbers: r = trapz.rvs(c, d, size=1000) # And compare the histogram: ax.hist(r, normed=True, histtype='stepfilled', alpha=0.2) ax.legend(loc='best', frameon=False) plt.show()
for method_i, method in enumerate(methods): #for each method print "running {}".format(str(method)) for trial in tqdm(range(max_trials)): #for each possible trial run trial_data[method_i].append([]) a_k = [random() for k in range(K)] #generate a bandit problem b_k = [random() for k in range(K)] for k in range(K): if a_k[k] > b_k[k]: a_k[k], b_k[k] = b_k[k], a_k[k] means = [trapz.mean(a_k[i], b_k[i]) for i in range(K)] max_mean = max(means) samples = [[] for i in range(K)] len_samples = [0 for i in range(K)] for t in range(1, max_time + 1): #run the method arm = method(samples, t) samples[arm].append(trapz.rvs(a_k[arm], b_k[arm])) len_samples[arm] += 1 trial_data[method_i][-1].append( sum([len_samples[i] * (max_mean - means[i]) for i in range(K)])) # transpose data arrays and calculate average regrets rearranged_trial_data = [list(map(list, zip(*l))) for l in trial_data] for i in range(len(rearranged_trial_data)): for j in range(len(rearranged_trial_data[i])): vv = rearranged_trial_data[i][j] rearranged_trial_data[i][j] = sum(vv) * 1.0 / len(vv) #output with open("data22.csv", "w") as f: for r in rearranged_trial_data:
def get_trapezoidal(): return trapz.rvs(0.2, 0.8, size=1)[0]