def test_XX_meanconst(self): est = Covariance(lagtime=self.lag, compute_c0t=False, bessels_correction=False) cc = est.fit(self.data - self.mean_const).fetch_model() np.testing.assert_allclose(cc.mean_0, self.mx_c_lag0) np.testing.assert_allclose(cc.cov_00, self.Mxx_c_lag0) cc = est.fit(self.data - self.mean_const, column_selection=self.cols_2).fetch_model() np.testing.assert_allclose(cc.cov_00, self.Mxx_c_lag0[:, self.cols_2])
def test_non_matching_length(self): n = 100 data = [np.random.random(size=(n, 2)) for n in range(3)] data = (data[:-1], data[1:]) weights = [np.random.random(n) for _ in range(3)] weights[0] = weights[0][:-3] with self.assertRaises(ValueError): Covariance(1, compute_c0t=True).fit(data, weights=weights) with self.assertRaises(ValueError): Covariance(1, compute_c0t=True).fit(data, weights=weights[:10])
def test_XX_meanfree(self): # many passes est = Covariance(lagtime=self.lag, compute_c0t=False, remove_data_mean=True, bessels_correction=False) cc = est.fit(self.data).fetch_model() np.testing.assert_allclose(cc.mean_0, self.mx_lag0) np.testing.assert_allclose(cc.cov_00, self.Mxx0_lag0) cc = est.fit(self.data, column_selection=self.cols_2).fetch_model() np.testing.assert_allclose(cc.cov_00, self.Mxx0_lag0[:, self.cols_2])
def test_XY_sym_meanconst(self): est = Covariance(lagtime=self.lag, compute_c0t=True, reversible=True, bessels_correction=False) cc = est.fit(self.Xc_lag0).fetch_model() np.testing.assert_allclose(cc.mean_0, self.m_c_sym) np.testing.assert_allclose(cc.cov_00, self.Mxx_c_sym) np.testing.assert_allclose(cc.cov_0t, self.Mxy_c_sym) cc = est.fit(self.Xc_lag0, column_selection=self.cols_2).fetch_model() np.testing.assert_allclose(cc.cov_00, self.Mxx_c_sym[:, self.cols_2]) np.testing.assert_allclose(cc.cov_0t, self.Mxy_c_sym[:, self.cols_2])
def test_XY_weighted_meanconst(self): est = Covariance(lagtime=self.lag, compute_c0t=True, bessels_correction=False) cc = est.fit(self.Xc_lag0, weights=self.data_weights).fetch_model() np.testing.assert_allclose(cc.mean_0, self.mx_c_wobj) np.testing.assert_allclose(cc.mean_t, self.my_c_wobj) np.testing.assert_allclose(cc.cov_00, self.Mxx_c_wobj) np.testing.assert_allclose(cc.cov_0t, self.Mxy_c_wobj) cc = est.fit(self.Xc_lag0, weights=self.data_weights, column_selection=self.cols_2).fetch_model() np.testing.assert_allclose(cc.cov_00, self.Mxx_c_wobj[:, self.cols_2]) np.testing.assert_allclose(cc.cov_0t, self.Mxy_c_wobj[:, self.cols_2])
def test_XXXY_sym_meanfree(self): # many passes est = Covariance(lagtime=self.lag, remove_data_mean=True, compute_c0t=True, reversible=True, bessels_correction=False) cc = est.fit(self.data, lagtime=self.lag).fetch_model() np.testing.assert_allclose(cc.mean_0, self.m_sym) np.testing.assert_allclose(cc.cov_00, self.Mxx0_sym) np.testing.assert_allclose(cc.cov_0t, self.Mxy0_sym) cc = est.fit(self.data, column_selection=self.cols_2).fetch_model() np.testing.assert_allclose(cc.cov_00, self.Mxx0_sym[:, self.cols_2]) np.testing.assert_allclose(cc.cov_0t, self.Mxy0_sym[:, self.cols_2])
def test_re_estimate_weight_types(self): # check different types are allowed and re-estimation works x = np.random.random((100, 2)) c = Covariance(lagtime=1, compute_c0t=True) c.fit(x, weights=np.ones((len(x), ))).fetch_model() c.fit(x, weights=np.ones((len(x), ))).fetch_model() c.fit(x, weights=None).fetch_model() c.fit(x, weights=x[:, 0]).fetch_model()
def test_XX_weightobj_withmean(self): # many passes est = Covariance(lagtime=self.lag, compute_c0t=False, remove_data_mean=False, bessels_correction=False) cc = est.fit(self.data, n_splits=10, weights=self.data_weights).fetch_model() np.testing.assert_allclose(cc.mean_0, self.mx_wobj_lag0) np.testing.assert_allclose(cc.cov_00, self.Mxx_wobj_lag0) cc = est.fit(self.data, column_selection=self.cols_2, weights=self.data_weights).fetch_model() np.testing.assert_allclose(cc.cov_00, self.Mxx_wobj_lag0[:, self.cols_2])
def test_XXXY_weightobj_withmean(self): # many passes est = Covariance(lagtime=self.lag, remove_data_mean=False, compute_c0t=True, bessels_correction=False) cc = est.fit(self.data, weights=self.data_weights).fetch_model() np.testing.assert_allclose(cc.mean_0, self.mx_wobj) np.testing.assert_allclose(cc.mean_t, self.my_wobj) np.testing.assert_allclose(cc.cov_00, self.Mxx_wobj) np.testing.assert_allclose(cc.cov_0t, self.Mxy_wobj) cc = est.fit(self.data, weights=self.data_weights, column_selection=self.cols_2).fetch_model() np.testing.assert_allclose(cc.cov_00, self.Mxx_wobj[:, self.cols_2]) np.testing.assert_allclose(cc.cov_0t, self.Mxy_wobj[:, self.cols_2])
def test_XXXY_withmean(self): # many passes est = Covariance(lagtime=self.lag, remove_data_mean=False, compute_c0t=True, bessels_correction=False) cc = est.fit(self.data, n_splits=1).fetch_model() assert not cc.bessels_correction np.testing.assert_allclose(cc.mean_0, self.mx) np.testing.assert_allclose(cc.mean_t, self.my) np.testing.assert_allclose(cc.cov_00, self.Mxx) np.testing.assert_allclose(cc.cov_0t, self.Mxy) cc = est.fit(self.data, n_splits=1, column_selection=self.cols_2).fetch_model() np.testing.assert_allclose(cc.cov_00, self.Mxx[:, self.cols_2]) np.testing.assert_allclose(cc.cov_0t, self.Mxy[:, self.cols_2])
def test_XXXY_weightobj_meanfree(self): #TODO: tests do not pass for n_splits > 1! # many passes est = Covariance(lagtime=self.lag, remove_data_mean=True, compute_c0t=True, bessels_correction=False) cc = est.fit(self.data, weights=self.data_weights, n_splits=1).fetch_model() np.testing.assert_allclose(cc.mean_0, self.mx_wobj) np.testing.assert_allclose(cc.mean_t, self.my_wobj) np.testing.assert_allclose(cc.cov_00, self.Mxx0_wobj) np.testing.assert_allclose(cc.cov_0t, self.Mxy0_wobj) cc = est.fit(self.data, weights=self.data_weights, column_selection=self.cols_2, n_splits=1).fetch_model() np.testing.assert_allclose(cc.cov_00, self.Mxx0_wobj[:, self.cols_2]) np.testing.assert_allclose(cc.cov_0t, self.Mxy0_wobj[:, self.cols_2])
def test_weights_close_to_zero(self): n = 1000 data = [np.random.random(size=(n, 2)) for _ in range(5)] # create some artificial correlations data[0][:, 0] *= np.random.randint(n) data = np.asarray(data) weights = [np.ones(n, dtype=np.float32) for _ in range(5)] # omit the first trajectory by setting a weight close to zero. weights[0][:] = 1E-44 weights = np.asarray(weights) est = Covariance(lagtime=1, compute_c0t=True) for data_traj, weights_traj in zip(data, weights): est.partial_fit((data_traj[:-3], data_traj[3:]), weights=weights_traj[:-3]) cov = est.fetch_model() # cov = covariance_lagged(data, lag=3, weights=weights, chunksize=10) assert np.all(cov.cov_00 < 1)