def MAB_regret_plot2(fname, xs_dict, colors=None, title=None, fig_kwargs=None, cut_time=None, name_filter=None, name_change=None): if colors is None: colors = sns.color_palette('Set1', len(xs_dict)) sns.set(style="white", font_scale=1.4, rc={"lines.linewidth": 20}) plt.figure(**with_default(fig_kwargs, dict())) for i, (name, value_matrix) in enumerate(xs_dict.items()): if name_filter is not None: if not name_filter(name): continue mean_x = np.mean(value_matrix, axis=0) time_points = sparse_index(with_default(cut_time, len(mean_x))) plt.plot(time_points, mean_x[time_points], lw=1, label=name_change[name], color=colors[i], linestyle='-' if 'once' in name else "--") plt.xlabel('Trials') plt.ylabel('Cum. Regrets') plt.xticks([]) plt.yticks([]) plt.ylim([-5, 100]) if title is not None: plt.title(title) for l in plt.gca().lines: print(l.get_linewidth()) plt.setp(l, linewidth=2) sns.despine() plt.savefig(fname, bbox_inches='tight', pad_inches=0.02) plt.close()
def MAB_optimal_probability_plot2(fname, arm_freqs, colors=None, title=None, fig_kwargs=None, cut_time=None, name_filter=None, name_change=None, no_legend=False): """ Draw figures showing optimal arm selection probability (based on multiple runs) """ if colors is None: colors = sns.color_palette('Set1', len(arm_freqs)) sns.set(style="white", font_scale=1.4, rc={"lines.linewidth": 20}) plt.figure(**with_default(fig_kwargs, dict())) for i, (name, arm_freq) in enumerate(arm_freqs.items()): if name_filter is not None: if not name_filter(name): continue time_points = sparse_index(with_default(cut_time, len(arm_freq))) plt.plot(time_points, arm_freq[time_points], lw=1, label=name_change[name], color=colors[i], linestyle='-' if 'once' in name else "--") if not no_legend: plt.legend(loc=4) plt.xlabel('Trials') plt.ylabel('Opt. Arm Prob.') plt.xticks([]) plt.ylim(-0.05, 1.02) if title is not None: plt.title(title) for l in plt.gca().lines: print(l.get_linewidth()) plt.setp(l, linewidth=2) for line in plt.gca().legend().get_lines(): line.set_linewidth(2) sns.despine() plt.savefig(fname, bbox_inches='tight', pad_inches=0.02) plt.close()
def naked_MAB_optimal_probability_plot(axes: Axes, arm_freqs, cut_time, legend=False, hide_ylabel=False, hide_yticklabels=False, **_kwargs): for i, (name, arm_freq) in list(enumerate(arm_freqs.items())): time_points = sparse_index(with_default(cut_time, len(arm_freq)), 200) axes.plot(time_points, arm_freq[time_points], lw=1, label=name.split(' ')[0] if '(TS)' in name else None, color=COLORS[i], linestyle='-' if '(TS)' in name else '--') if legend: axes.legend(loc=4, frameon=False) axes.set_xlabel('Trials') if not hide_ylabel: axes.set_ylabel('Probability') axes.get_yaxis().set_label_coords(-0.15, 0.5) axes.set_yticks([0, 0.5, 1.0]) if hide_yticklabels: axes.set_yticklabels([]) axes.set_ylim(-0.05, 1.02)
def sup_KL(mu_ref, divergence, lower=None): """ Find largest mu that satisfies KL(mu_ref, mu) <= divergence """ if divergence <= 0: return mu_ref if KL(mu_ref, 1.0) <= divergence: return 1.0 return brenth(lambda x: KL(mu_ref, x) - divergence, with_default(lower, mu_ref), 1)
def __init__(self, G: CausalDiagram, F=None, P_U=None, D=None, more_U=None): self.G = G self.F = F self.P_U = P_U self.D = with_default(D, defaultdict(lambda: (0, 1))) self.more_U = set() if more_U is None else set(more_U) self.query00 = functools.lru_cache(1024)(self.query00)
def naked_MAB_regret_plot(axes: Axes, xs_dict, cut_time, band_alpha=0.1, legend=False, hide_ylabel=False, adjust_ymax=1, hide_yticklabels=False, **_kwargs): for i, (name, value_matrix) in list(enumerate(xs_dict.items())): mean_x = np.mean(value_matrix, axis=0) sd_x = np.std(value_matrix, axis=0) lower, upper = mean_x - sd_x, mean_x + sd_x time_points = sparse_index(with_default(cut_time, len(mean_x)), 200) axes.plot(time_points, mean_x[time_points], lw=1, label=name.split(' ')[0] if '(TS)' in name else None, color=COLORS[i], linestyle='-' if '(TS)' in name else '--') axes.fill_between(time_points, lower[time_points], upper[time_points], color=COLORS[i], alpha=band_alpha, lw=0) if legend: axes.legend(loc=2, frameon=False) if not hide_ylabel: axes.set_ylabel('Cum. Regrets') axes.get_yaxis().set_label_coords(-0.15, 0.5) if adjust_ymax != 1: ymin, ymax = axes.get_ylim() axes.set_ylim(ymin, ymax * adjust_ymax) if hide_yticklabels: axes.set_yticklabels([])