def plot_multinomial_resample(a): N = len(a) cmap = mpl.colors.ListedColormap([[0., .4, 1.], [0., .8, 1.], [1., .8, 0.], [1., .4, 0.]]*(int(N/4) + 1)) cumsum = np.cumsum(np.asarray(a) / np.sum(a)) cumsum = np.insert(cumsum, 0, 0) with figsize(y=2): fig = plt.figure() ax = plt.gcf().add_axes([0.05, 0.475, 0.9, 0.15]) norm = mpl.colors.BoundaryNorm(cumsum, cmap.N) bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap, norm=norm, drawedges=False, spacing='proportional', orientation='horizontal') # make N subdivisions, and chose a random position within each one b = random(N) plt.scatter(b, [.5]*len(b), s=60, facecolor='k', edgecolor='k') bar.set_ticks([]) plt.title('multinomial resampling') end_interactive(fig)
def plot_residual_resample(a): N = len(a) a_norm = np.asarray(a) / np.sum(a) cumsum = np.cumsum(a_norm) cumsum = np.insert(cumsum, 0, 0) cmap = mpl.colors.ListedColormap([[0., .4, 1.], [0., .8, 1.], [1., .8, 0.], [1., .4, 0.]]*(int(N/4) + 1)) with figsize(y=2): fig = plt.figure() ax = plt.gcf().add_axes([0.05, 0.475, 0.9, 0.15]) norm = mpl.colors.BoundaryNorm(cumsum, cmap.N) bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap, norm=norm, drawedges=False, spacing='proportional', orientation='horizontal') indexes = residual_resample(a_norm) bins = np.bincount(indexes) for i in range(1, N): n = bins[i-1] # number particles in this sample if n > 0: b = np.linspace(cumsum[i-1], cumsum[i], n+2)[1:-1] plt.scatter(b, [.5]*len(b), s=60, facecolor='k', edgecolor='k') bar.set_ticks([]) plt.title('residual resampling') end_interactive(fig)
def plot_cumsum(a): with figsize(y=2): fig = plt.figure() N = len(a) cmap = mpl.colors.ListedColormap([[0., .4, 1.], [0., .8, 1.], [1., .8, 0.], [1., .4, 0.]]*(int(N/4) + 1)) cumsum = np.cumsum(np.asarray(a) / np.sum(a)) cumsum = np.insert(cumsum, 0, 0) #fig = plt.figure(figsize=(6,3)) fig=plt.gcf() ax = fig.add_axes([0.05, 0.475, 0.9, 0.15]) norm = mpl.colors.BoundaryNorm(cumsum, cmap.N) bar = mpl.colorbar.ColorbarBase(ax, cmap=cmap, norm=norm, drawedges=False, spacing='proportional', orientation='horizontal') if N > 10: bar.set_ticks([]) end_interactive(fig)
def plot_markov_chain(): fig = plt.figure(figsize=(4,4), facecolor='w') ax = plt.axes((0, 0, 1, 1), xticks=[], yticks=[], frameon=False) #ax.set_xlim(0, 10) #ax.set_ylim(0, 10) box_bg = '#DDDDDD' kf1c = Circle((4,5), 0.5, fc=box_bg) kf2c = Circle((6,5), 0.5, fc=box_bg) ax.add_patch (kf1c) ax.add_patch (kf2c) plt.text(4,5, "Straight",ha='center', va='center', fontsize=14) plt.text(6,5, "Turn",ha='center', va='center', fontsize=14) #btm plt.text(5, 3.9, ".05", ha='center', va='center', fontsize=18) ax.annotate('', xy=(4.1, 4.5), xycoords='data', xytext=(6, 4.5), textcoords='data', size=10, arrowprops=dict(arrowstyle="->", ec="k", connectionstyle="arc3,rad=-0.5")) #top plt.text(5, 6.1, ".03", ha='center', va='center', fontsize=18) ax.annotate('', xy=(6, 5.5), xycoords='data', xytext=(4.1, 5.5), textcoords='data', size=10, arrowprops=dict(arrowstyle="->", ec="k", connectionstyle="arc3,rad=-0.5")) plt.text(3.5, 5.6, ".97", ha='center', va='center', fontsize=18) ax.annotate('', xy=(3.9, 5.5), xycoords='data', xytext=(3.55, 5.2), textcoords='data', size=10, arrowprops=dict(arrowstyle="->", ec="k", connectionstyle="angle3,angleA=150,angleB=0")) plt.text(6.5, 5.6, ".95", ha='center', va='center', fontsize=18) ax.annotate('', xy=(6.1, 5.5), xycoords='data', xytext=(6.45, 5.2), textcoords='data', size=10, arrowprops=dict(arrowstyle="->", fc="0.2", ec="k", connectionstyle="angle3,angleA=-150,angleB=2")) plt.axis('equal') plt.show() bp.end_interactive(fig)