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)) fig = plt.figure(figsize=(6,3)) 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') 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') plt.show()
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)) 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')
def resample(self): self.weights = normalize(self.weights[:, np.newaxis], axis=0, norm='l1').ravel() self.states = self.states[residual_resample(self.weights)] self.weights = np.ones( (self.num_particles), dtype=int) * (1 / float(self.num_particles))
def resample(self): self.weights = normalize(self.weights[:, np.newaxis], axis=0, norm='l1').ravel() #print np.square(self.weights) ESS = 1 / float(np.sum(np.square(self.weights))) #print ESS,THRESHOLD if ESS > THRESHOLD: newStates = np.empty((len(self.weights), self.DIM), dtype=int) self.states = self.states[residual_resample(self.weights)] self.weights = np.ones((self.num_particles), dtype=int) * (1 / float(self.num_particles))
def resample(self): indexes = residual_resample(self.weights) self.particles = self.particles[indexes,:] self.weights = self.weights[indexes]