def test_smooth_missing(self): kpm=scipy.io.loadmat(pkg_resources.resource_filename(__name__,'kpm_learn_results')) y = kpm['y'].T # data vector is transposed from KPM F1 = kpm['F1'] H1 = kpm['H1'] Q1 = kpm['Q1'] R1 = kpm['R1'] initx1 = kpm['initx'] initV1 = kpm['initV'] T,os = y.shape # build data with missing observations specified by nan if T>2: y[2,:] = numpy.nan * numpy.ones( (os,) ) if T>12: y[12,:] = numpy.nan * numpy.ones( (os,) ) # build data with missing observations specified by None y_none = [] for yy in y: if numpy.any( numpy.isnan( yy ) ): y_none.append( None ) else: y_none.append( yy ) # get results xsmooth_nan, Vsmooth_nan = adskalman.kalman_smoother(y,F1,H1,Q1,R1,initx1,initV1) xsmooth_none, Vsmooth_none = adskalman.kalman_smoother(y_none,F1,H1,Q1,R1,initx1,initV1) # compare assert numpy.allclose(xsmooth_nan, xsmooth_none) assert numpy.allclose(Vsmooth_nan, Vsmooth_none)
def test_filt_KPM(self): kpm=scipy.io.loadmat(pkg_resources.resource_filename(__name__,'kpm_results')) # process model A = numpy.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]], dtype=numpy.float64) # observation model C = numpy.array([[1, 0, 0, 0], [0, 1, 0, 0]], dtype=numpy.float64) ss=4; os=2 # process covariance Q = 0.1*numpy.eye(ss) # measurement covariance R = 1.0*numpy.eye(os) initx = numpy.array([10, 10, 1, 0],dtype=numpy.float64) initV = 10.0*numpy.eye(ss) x = kpm['x'].T y = kpm['y'].T xfilt, Vfilt = adskalman.kalman_filter(y, A, C, Q, R, initx, initV) assert numpy.allclose(xfilt.T,kpm['xfilt']) assert_3d_vs_kpm_close(Vfilt,kpm['Vfilt']) xsmooth, Vsmooth = adskalman.kalman_smoother(y,A,C,Q,R,initx,initV) assert numpy.allclose(xsmooth.T,kpm['xsmooth']) assert_3d_vs_kpm_close(Vsmooth,kpm['Vsmooth'])
def _test_kalman_murphy_smooth(self,missing_type): Ymissing = numpy.array(self.Y, copy=True) if missing_type=='nan': valid_idx = None elif missing_type=='large': valid_idx = range(len(Ymissing)) else: raise ValueError('unknown missing_type "%s"'%missing_type) bad_idx = range(5,len(Ymissing),10) for i in bad_idx: if missing_type=='nan': Ymissing[i] = numpy.nan*Ymissing[i] elif missing_type=='large': valid_idx.remove(i) Ymissing[i] = 9e999*Ymissing[i] assert len(bad_idx)>5 # make sure some data are actually missing! xsmooth, Vsmooth = adskalman.kalman_smoother(Ymissing,self.A,self.C,self.Q,self.R,self.x0,self.P0, valid_data_idx=valid_idx) dist = numpy.sqrt(numpy.sum((xsmooth-self.X)**2,axis=1)) mean_dist = numpy.mean(dist) #print 'mean_dist',missing_type,mean_dist assert mean_dist < 0.1 # Not sure of exact value, but shouldn't be too large
def kalman_smoother(data, F, H, Q, R, initx, initv, plot=False): os = H.shape[0] ss = F.shape[0] interpolated_data = np.zeros_like(data) for c in range(os): interpolated_data[:,c] = floris_math.interpolate_nan(data[:,c]) y = interpolated_data xsmooth,Vsmooth = adskalman.kalman_smoother(y,F,H,Q,R,initx,initv) if plot: plt.plot(xsmooth[:,0]) plt.plot(y[:,0], '*') return xsmooth,Vsmooth
def test_DRO_smooth(self): kpm=scipy.io.loadmat(pkg_resources.resource_filename(__name__,'kpm_results')) # process model A = numpy.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]], dtype=numpy.float64) # observation model C = numpy.array([[1, 0, 0, 0], [0, 1, 0, 0]], dtype=numpy.float64) ss=4; os=2 # process covariance Q = 0.1*numpy.eye(ss) # measurement covariance R = 1.0*numpy.eye(os) initx = numpy.array([10, 10, 1, 0],dtype=numpy.float64) initV = 10.0*numpy.eye(ss) x = kpm['x'].T y = kpm['y'].T xfilt, Vfilt = adskalman.DROsmooth(y,A,C,Q,R,initx,initV,mode='forward_only') if 0: xfilt_kpm, Vfilt_kpm = adskalman.kalman_filter(y, A, C, Q, R, initx, initV) print 'xfilt.T',xfilt.T print 'xfilt_kpm.T',xfilt_kpm.T print "kpm['xfilt']",kpm['xfilt'] assert numpy.allclose(xfilt.T,kpm['xfilt']) assert_3d_vs_kpm_close(Vfilt,kpm['Vfilt']) xsmooth, Vsmooth = adskalman.DROsmooth(y,A,C,Q,R,initx,initV) if 0: xsmooth_kpm, Vsmooth_kpm = adskalman.kalman_smoother(y,A,C,Q,R,initx,initV) print 'xsmooth.T',xsmooth.T print 'xsmooth_kpm.T',xsmooth_kpm.T print "kpm['xsmooth']",kpm['xsmooth'] print 'Vsmooth',Vsmooth assert numpy.allclose(xsmooth.T[:,:-1],kpm['xsmooth'][:,:-1]) # KPM doesn't update last timestep assert_3d_vs_kpm_close(Vsmooth[:-1],kpm['Vsmooth'][:,:,:-1])
def _test_kalman_murphy_EM(self): # temporarily disabled def returnC(orig): return self.C def returnQ(orig): return self.Q def returnR(orig): return self.R constr_fun_dict = { 'C':returnC, #'Q':returnQ, #'R':returnR, } if 1: print '-'*80,'Murphy' Alearn, Clearn, Qlearn, Rlearn, initx2, initV2, LL = adskalman.learn_kalman( self.Y, self.Abad, self.C, self.Q, self.R, self.x0, self.P0, max_iter=500, verbose=2, constr_fun_dict=constr_fun_dict, thresh=1e-5, ) xsmooth, Vsmooth, VVsmooth, loglik = adskalman.kalman_smoother( self.Y,self.A,self.C,self.Q,self.R,self.x0,self.P0, full_output=True) if 1: print 'best LL',loglik print print '*'*80,'Murphy' print 'A' print self.A print 'Alearn' print Alearn print 'Clearn' print Clearn print 'Qlearn' print Qlearn print 'Rlearn' print Rlearn print '*'*80,'Murphy'
def test_loglik_KPM(self): # this test broke the loglik calculation kpm=scipy.io.loadmat(pkg_resources.resource_filename(__name__,'kpm_learn_results')) y = kpm['y'].T # data vector is transposed from KPM F1 = kpm['F1'] H1 = kpm['H1'] Q1 = kpm['Q1'] R1 = kpm['R1'] initx1 = kpm['initx'] initV1 = kpm['initV'] xfilt, Vfilt, VVfilt, loglik_filt = adskalman.kalman_filter( y, F1, H1, Q1, R1, initx1, initV1,full_output=True) assert numpy.allclose(xfilt, kpm['xfilt'].T) assert_3d_vs_kpm_close(Vfilt, kpm['Vfilt']) assert_3d_vs_kpm_close(VVfilt, kpm['VVfilt']) assert numpy.allclose(loglik_filt, kpm['loglik_filt']) xsmooth, Vsmooth, VVsmooth, loglik_smooth = adskalman.kalman_smoother( y, F1, H1, Q1, R1, initx1, initV1,full_output=True) assert numpy.allclose(xsmooth, kpm['xsmooth'].T) assert_3d_vs_kpm_close(Vsmooth, kpm['Vsmooth']) assert_3d_vs_kpm_close(VVsmooth, kpm['VVsmooth']) assert numpy.allclose(loglik_smooth, kpm['loglik_smooth'])
def test_kalman__murphy1(self): def returnC(orig): return self.C def returnQ(orig): return self.Q def returnR(orig): return self.R constr_fun_dict = { 'C':returnC, 'Q':returnQ, 'R':returnR, } print '-'*80,'Murphy' Alearn, Clearn, Qlearn, Rlearn, initx2, initV2, LL = adskalman.learn_kalman( self.Y, self.Abad, self.C, self.Q, self.R, self.x0, self.P0, max_iter=150, verbose=2, constr_fun_dict=constr_fun_dict, ) xsmooth, Vsmooth, VVsmooth, loglik = adskalman.kalman_smoother( self.Y,self.A,self.C,self.Q,self.R,self.x0,self.P0, full_output=True) print 'best LL',loglik print print '*'*80,'Murphy' print 'A' print self.A print 'Alearn' print Alearn print 'Clearn' print Clearn print 'Qlearn' print Qlearn print 'Rlearn' print Rlearn print '*'*80,'Murphy'
[0,1,0,1], [0,0,1,0], [0,0,0,1]], dtype=numpy.float) H = numpy.array([[1,0,0,0], # observation matrix [0,1,0,0]], dtype=numpy.float) Q = 0.1*numpy.eye(ss) # process noise R = 1.0*numpy.eye(os) # observation noise initx = numpy.array([10,10,1,0],dtype=numpy.float) initV = 10*numpy.eye(ss) T = 15 # This is the data that is generated by Murphy's demo. #seed = 9 #numpy.random.seed(seed) #x,y = adskalman.sample_lds(F,H,Q,R,initx,T) x = numpy.array([[ 10. , 10.78562922, 12.14884733, 13.37768744, 14.70391646, 15.0972105 , 15.27100392, 16.02603256, 16.77868535, 17.35367989, 17.60693265, 17.90492854, 18.8527745 , 19.0826357 , 19.50682031], [ 10. , 10.2018565 , 10.37289444, 10.24726726, 9.41747009, 9.23049962, 8.25498444, 7.93055042, 7.74377694, 6.57500322, 6.09083901, 6.04183098, 6.25299483, 5.86752675, 5.63880611], [ 1. , 1.32750793, 0.89718228, 1.04323621, 0.6012368 , 0.38607908, 0.10314957, 0.58612548, 0.64803301, 0.31990456, 0.51884579, 1.12411301, 0.9584339 , 0.89764825, 0.99435954], [ 0. , -0.13645972, -0.41671164, -0.91084177, -0.29670515, -0.80974329, -0.75982391, -0.28591924, -0.76442545, -0.16191313, 0.32366389, 0.19091798, -0.11169736, -0.20569391, 0.08488225]]).T y = numpy.array([[ 11.49746646, 8.34917862, 12.2345992 , 13.01971219, 12.93696592, 14.91080222, 17.03870584, 14.63065253, 18.12204124, 16.81571886, 18.08187941, 17.91986286, 20.4956051 , 18.90627217, 19.40367059], [ 10.37519837, 11.74461097, 10.10072462, 9.07503471, 9.47889654, 9.65741996, 9.11313015, 7.58503774, 6.4678298 , 5.02218694, 5.79485744, 4.45538921, 6.42789419, 6.45878971, 2.74446057]]).T xsmooth,Vsmooth = adskalman.kalman_smoother(y,F,H,Q,R,initx,initV) print 'xsmooth',xsmooth print 'Vsmooth',Vsmooth