def plot(self, data=None, indices=None, color='b', plot_params=True, label=''): from util import project_data, plot_gaussian_projection, plot_gaussian_2D if data is not None: data = flattendata_distribution(data) D = self.mu.shape[0] if D > 2 and ((not hasattr(self, 'plotting_subspace_basis')) or (self.plotting_subspace_basis.shape[1] != D)): # TODO improve this bookkeeping. need a notion of collection. it's # totally potentially broken and confusing to set class members like # this! subspace = np.random.randn(D, 2) self.__class__.plotting_subspace_basis = np.linalg.qr( subspace)[0].T.copy() if data is not None: if D > 2: data = project_data(data, self.plotting_subspace_basis) plt.plot(data[:, 0], data[:, 1], marker='.', linestyle=' ', color=color) if plot_params: if D > 2: plot_gaussian_projection(self.mu, self.sigma, self.plotting_subspace_basis, color=color, label=label) else: plot_gaussian_2D(self.mu, self.sigma, color=color, label=label)
def plot(self, data=None, indices=None, color='b', plot_params=True, label=''): from util import project_data, plot_gaussian_projection, plot_gaussian_2D if data is not None: data = util.flattendata(data) D = len(self.mu) if isinstance(self.mu, np.ndarray) else 1 assert D >= 2 if D > 2 and ((not hasattr(self, 'plotting_subspace_basis')) or (self.plotting_subspace_basis.shape[1] != D)): subspace = np.random.randn(D, 2) self.__class__.plotting_subspace_basis = np.linalg.qr( subspace)[0].T.copy() if data is not None: if D > 2: data = project_data(data, self.plotting_subspace_basis) plt.plot(data[:, 0], data[:, 1], marker='.', linestyle=' ', color=color) if plot_params: if D > 2: plot_gaussian_projection(self.mu, self.Sigma, self.plotting_subspace_basis, color=color, label=label) else: plot_gaussian_2D(self.mu, self.Sigma, color=color, label=label)
def test_ukf(): ### generate some spiral data passed through some nonlinear business theta = 0.05*np.pi A = 1.01*np.array([[np.cos(theta), -np.sin(theta)],[np.sin(theta),np.cos(theta)]]) B = np.eye(2) d = 5*np.ones(2) x = np.zeros((100,2)) x[0] = (0,5) for i in range(1,100): x[i] = A.dot(x[i-1]) + B.dot(np.random.randn(2)) f = lambda x: x**2 y = f(x) + d*np.random.normal(size=x.shape) ### run unscented kalman filter initial_distn = Gaussian(x[0],0.1*np.eye(2)) filtered_distns = kalman.ukf_optimized(A,B,f,d,initial_distn,y,1,1) ### plot things filtered_mus = np.array([d.mu for d in filtered_distns]) plt.figure() plt.subplot(2,1,1) plt.plot(y[:,0],y[:,1],'bx-',label='observations') plt.legend() plt.subplot(2,1,2) plt.plot(filtered_mus[:,0],filtered_mus[:,1],'g.-',label='filtered') plt.plot(x[:,0],x[:,1],'kx-.',label='true state') for df in filtered_distns: plot_gaussian_2D(df.mu,df.Sigma,color='g',centermarker=False) plt.legend()
def test_linear(): ### generate some noisy spiral data theta = 0.1*np.pi A = 1.02*np.array([[np.cos(theta), -np.sin(theta)],[np.sin(theta),np.cos(theta)]]) B = 0.25*np.eye(2) C = np.eye(2) D = np.eye(2) x = np.zeros((100,2)) x[0] = (0,2) for i in range(1,100): x[i] = A.dot(x[i-1]) + B.dot(np.random.randn(2)) y = (C.dot(x.T) + D.dot(np.random.normal(size=x.shape).T)).T ### run Kalman filters and smoothers (so easy!) initial_distn = Gaussian(np.zeros(2),np.eye(2)) filtered_distns = kalman.filter_generic(A,B,C,D,initial_distn,y) smoothed_distns = kalman.smooth_rts_optimized(A,B,C,D,initial_distn,y) ### plot things filtered_mus = np.array([d.mu for d in filtered_distns]) smoothed_mus = np.array([d.mu for d in smoothed_distns]) plt.plot(y[:,0],y[:,1],'bx-',label='observations') plt.plot(filtered_mus[:,0],filtered_mus[:,1],'g.-',label='filtered') plt.plot(smoothed_mus[:,0],smoothed_mus[:,1],'r.-',label='smoothed') for df,ds in zip(filtered_distns,smoothed_distns): plot_gaussian_2D(df.mu,df.Sigma,color='g',centermarker=False) plot_gaussian_2D(ds.mu,ds.Sigma,color='r',centermarker=False) plt.plot(x[:,0],x[:,1],'ko-.',label='true state',markersize=8) plt.legend()
plt.figure(figsize=(15,10)) # true stateseq plt.subplot(3,1,1) states, durs = util.rle(labels) X,Y = np.meshgrid(np.hstack((0,durs.cumsum())),[1,2]); plt.pcolor(X,Y,states[na,:],edgecolors='k',alpha=0.8); # inferred stateseq states, durs = util.rle(posteriormodel.states.stateseq) X,Y = np.meshgrid(np.hstack((0,durs.cumsum())),[0,1]); plt.pcolor(X,Y,states[na,:],edgecolors='k',alpha=0.8,vmin=0,vmax=Nmax); plt.ylim((0,2)); plt.xlim((0,T)) plt.title('true stateseq (top, colors arbitrary) vs sampled stateseq (bottom)') # observations plt.subplot(3,1,2) plt.title('data and inferred observation distributions') plt.plot(data[:,0],data[:,1],'kx') for state, (c,o) in enumerate(zip(colors,obs_distns)): if np.sum(posteriormodel.states.stateseq == state) > 1: util.plot_gaussian_2D(o.mu,o.sigma,color=c,centermarker=True) # durations plt.subplot(3,1,3) t = np.arange(1,2*N*10) for state,(c,d) in enumerate(zip(colors,posteriormodel.dur_distns)): if np.sum(posteriormodel.states.stateseq == state) > 1: plt.plot(t,d.pmf(t),label='state %d, %s' % (state,d),color=c) plt.hist(durs[states == state],normed=True,color=c,bins=t) plt.legend() plt.title('inferred duration distributions and normalized histograms of sampled durations') plt.show()