def test_non_matching_length(self): n = 100 data = [np.random.random(size=(n, 2)) for _ in range(3)] weights = [np.random.random(n) for _ in range(3)] weights[0] = weights[0][:-3] with self.assertRaises(ValueError): covariance_lagged(data=data, weights=weights, lag=1) with self.assertRaises(ValueError): covariance_lagged(data, weights=weights[:2])
def test_XX(self): cc = covariance_lagged(data=self.data, c0t=False, lag=self.tau, bessel=False, weights="koopman") cc1 = covariance_lagged(data=self.data, c0t=False, lag=self.tau, bessel=False, weights=self.weight_object) assert np.allclose(cc.mean, self.mx) assert np.allclose(cc.C00_, self.Mxx) assert np.allclose(cc1.mean, self.mx) assert np.allclose(cc1.C00_, self.Mxx)
def test_XXXY_sym_withmean(self): # many passes cc = covariance_lagged(data=self.data, remove_data_mean=False, c0t=True, lag=self.lag, reversible=True, bessel=False, chunksize=self.chunksize) np.testing.assert_allclose(cc.mean, self.m_sym) np.testing.assert_allclose(cc.C00_, self.Mxx_sym) np.testing.assert_allclose(cc.C0t_, self.Mxy_sym)
def test_XXXY_weightobj_sym_meanfree(self): # many passes cc = covariance_lagged(data=self.data, remove_data_mean=True, c0t=True, lag=self.lag, reversible=True, bessel=False, weights=self.wobj, chunksize=self.chunksize) np.testing.assert_allclose(cc.mean, self.m_sym_wobj) np.testing.assert_allclose(cc.C00_, self.Mxx0_sym_wobj) np.testing.assert_allclose(cc.C0t_, self.Mxy0_sym_wobj)
def test_XY_weighted_meanconst(self): cc = covariance_lagged(data=self.data, remove_constant_mean=self.mean_const, c0t=True, weights=self.wobj, lag=self.lag, bessel=False, chunksize=self.chunksize) np.testing.assert_allclose(cc.mean, self.mx_c_wobj) np.testing.assert_allclose(cc.mean_tau, self.my_c_wobj) np.testing.assert_allclose(cc.C00_, self.Mxx_c_wobj) np.testing.assert_allclose(cc.C0t_, self.Mxy_c_wobj)
def test_re_estimate_weight_types(self): # check different types are allowed and re-estimation works x = np.random.random((100, 2)) c = covariance_lagged(lag=1) c.estimate(x, weights=1) c.estimate(x, weights=1.0) c.estimate(x, weights=None) c.estimate(x, weights=x[:,0])
def test_XXXY_weightobj_withmean(self): # many passes cc = covariance_lagged(data=self.data, remove_data_mean=False, c0t=True, lag=self.lag, weights=self.wobj, bessel=False, chunksize=self.chunksize) np.testing.assert_allclose(cc.mean, self.mx_wobj) np.testing.assert_allclose(cc.mean_tau, self.my_wobj) np.testing.assert_allclose(cc.C00_, self.Mxx_wobj) np.testing.assert_allclose(cc.C0t_, self.Mxy_wobj)
def test_XX_removeconstantmean(self): cc = covariance_lagged(data=self.data, c0t=False, lag=self.tau, remove_constant_mean=self.mean_constant, bessel=False, weights="koopman") cc1 = covariance_lagged(data=self.data, c0t=False, lag=self.tau, remove_constant_mean=self.mean_constant, bessel=False, weights=self.weight_object) assert np.allclose(cc.mean, self.mx_c) assert np.allclose(cc.C00_, self.Mxx_c) assert np.allclose(cc1.mean, self.mx_c) assert np.allclose(cc1.C00_, self.Mxx_c)
def test_XXXY_meanfree(self): # many passes cc = covariance_lagged(data=self.data, remove_data_mean=True, c0t=True, lag=self.lag, bessel=False, chunksize=self.chunksize) np.testing.assert_allclose(cc.mean, self.mx) np.testing.assert_allclose(cc.mean_tau, self.my) np.testing.assert_allclose(cc.C00_, self.Mxx0) np.testing.assert_allclose(cc.C0t_, self.Mxy0)
def test_XX_meanconst(self): cc = covariance_lagged(data=self.data, c0t=False, remove_constant_mean=self.mean_const, bessel=False, chunksize=self.chunksize) assert np.allclose(cc.mean, self.mx_c_lag0) assert np.allclose(cc.cov, self.Mxx_c_lag0)
def test_XX_removedatamean(self): cc = covariance_lagged(data=self.data, c0t=False, lag=self.tau, remove_data_mean=True, bessel=False, weights="koopman") cc1 = covariance_lagged(data=self.data, c0t=False, lag=self.tau, remove_data_mean=True, bessel=False, weights=self.weight_object) assert np.allclose(cc.mean, self.mx) assert np.allclose(cc.cov, self.Mxx0) assert np.allclose(cc1.mean, self.mx) assert np.allclose(cc1.cov, self.Mxx0)
def test_XX_withmean(self): # many passes cc = covariance_lagged(data=self.data, c0t=False, remove_data_mean=False, bessel=False, chunksize=self.chunksize) np.testing.assert_allclose(cc.mean, self.mx_lag0) np.testing.assert_allclose(cc.cov, self.Mxx_lag0)
def test_XX_meanfree(self): # many passes cc = covariance_lagged(data=self.data, c0t=False, remove_data_mean=True, bessel=False, chunksize=self.chunksize) assert np.allclose(cc.mean, self.mx_lag0) assert np.allclose(cc.cov, self.Mxx0_lag0)
def test_XX_weighted_meanconst(self): cc = covariance_lagged(data=self.data, c0t=False, remove_constant_mean=self.mean_const, weights=self.wobj, bessel=False, chunksize=self.chunksize) np.testing.assert_allclose(cc.mean, self.mx_c_wobj_lag0) np.testing.assert_allclose(cc.cov, self.Mxx_c_wobj_lag0)
def test_XX_weightobj_meanfree(self): # many passes cc = covariance_lagged(data=self.data, c0t=False, remove_data_mean=True, weights=self.wobj, bessel=False, chunksize=self.chunksize) np.testing.assert_allclose(cc.mean, self.mx_wobj_lag0) np.testing.assert_allclose(cc.cov, self.Mxx0_wobj_lag0)
def test_XY_sym(self): cc = covariance_lagged(data=self.data, lag=self.tau, c0t=True, reversible=True, bessel=False, weights="koopman") cc1 = covariance_lagged(data=self.data, lag=self.tau, c0t=True, reversible=True, bessel=False, weights=self.weight_object) assert np.allclose(cc.mean, self.msym) assert np.allclose(cc.C00_, self.Mxx_sym) assert np.allclose(cc.C0t_, self.Mxy_sym) assert np.allclose(cc1.mean, self.msym) assert np.allclose(cc1.C00_, self.Mxx_sym) assert np.allclose(cc1.C0t_, self.Mxy_sym)
def test_XX_weightobj_withmean(self): # many passes cc = covariance_lagged(data=self.data, c0t=False, remove_data_mean=False, weights=self.wobj, bessel=False, chunksize=self.chunksize) assert np.allclose(cc.mean, self.mx_wobj_lag0) assert np.allclose(cc.cov, self.Mxx_wobj_lag0)
def test_XY(self): cc = covariance_lagged(data=self.data, lag=self.tau, c0t=True, bessel=False, weights="koopman") cc1 = covariance_lagged(data=self.data, lag=self.tau, c0t=True, bessel=False, weights=self.weight_object) assert np.allclose(cc.mean, self.mx) assert np.allclose(cc.mean_tau, self.my) assert np.allclose(cc.cov, self.Mxx) assert np.allclose(cc.cov_tau, self.Mxy) assert np.allclose(cc1.mean, self.mx) assert np.allclose(cc1.mean_tau, self.my) assert np.allclose(cc1.cov, self.Mxx) assert np.allclose(cc1.cov_tau, self.Mxy)
def test_XX_meanconst(self): cc = covariance_lagged(data=self.data, c0t=False, remove_constant_mean=self.mean_const, bessel=False, chunksize=self.chunksize) np.testing.assert_allclose(cc.mean, self.mx_c_lag0) np.testing.assert_allclose(cc.C00_, self.Mxx_c_lag0) cc.column_selection = self.cols_2 cc.estimate(self.data) np.testing.assert_allclose(cc.C00_, self.Mxx_c_lag0[:, self.cols_2])
def test_XY_meanconst(self): cc = covariance_lagged(data=self.data, remove_constant_mean=self.mean_const, c0t=True, lag=self.lag, bessel=False, chunksize=self.chunksize) np.testing.assert_allclose(cc.mean, self.mx_c) np.testing.assert_allclose(cc.mean_tau, self.my_c) np.testing.assert_allclose(cc.cov, self.Mxx_c) np.testing.assert_allclose(cc.cov_tau, self.Mxy_c)
def test_XY_sym_meanconst(self): cc = covariance_lagged(data=self.data, remove_constant_mean=self.mean_const, c0t=True, reversible=True, lag=self.lag, bessel=False, chunksize=self.chunksize) assert np.allclose(cc.mean, self.m_c_sym) assert np.allclose(cc.cov, self.Mxx_c_sym) assert np.allclose(cc.cov_tau, self.Mxy_c_sym)
def test_XY_sym_removedatamean(self): cc = covariance_lagged(data=self.data, lag=self.tau, c0t=True, reversible=True, remove_data_mean=True, bessel=False, weights="koopman") cc1 = covariance_lagged(data=self.data, lag=self.tau, c0t=True, reversible=True, remove_data_mean=True, bessel=False, weights=self.weight_object) assert np.allclose(cc.mean, self.msym) assert np.allclose(cc.cov, self.Mxx0_sym) assert np.allclose(cc.cov_tau, self.Mxy0_sym) assert np.allclose(cc1.mean, self.msym) assert np.allclose(cc1.cov, self.Mxx0_sym) assert np.allclose(cc1.cov_tau, self.Mxy0_sym)
def test_XX_meanfree(self): # many passes cc = covariance_lagged(data=self.data, c0t=False, remove_data_mean=True, bessel=False, chunksize=self.chunksize) np.testing.assert_allclose(cc.mean, self.mx_lag0) np.testing.assert_allclose(cc.C00_, self.Mxx0_lag0) cc.column_selection = self.cols_2 cc.estimate(self.data) np.testing.assert_allclose(cc.C00_, self.Mxx0_lag0[:, self.cols_2])
def test_XXXY_sym_meanfree(self): # many passes cc = covariance_lagged(data=self.data, remove_data_mean=True, c0t=True, lag=self.lag, reversible=True, bessel=False, chunksize=self.chunksize) assert np.allclose(cc.mean, self.m_sym) assert np.allclose(cc.cov, self.Mxx0_sym) assert np.allclose(cc.cov_tau, self.Mxy0_sym)
def test_XXXY_withmean(self): # many passes cc = covariance_lagged(data=self.data, remove_data_mean=False, c0t=True, lag=self.lag, bessel=False, chunksize=self.chunksize) assert np.allclose(cc.mean, self.mx) assert np.allclose(cc.mean_tau, self.my) assert np.allclose(cc.cov, self.Mxx) assert np.allclose(cc.cov_tau, self.Mxy)
def test_XY_removeconstantmean(self): cc = covariance_lagged(data=self.data, lag=self.tau, c0t=True, remove_constant_mean=self.mean_constant, bessel=False, weights="koopman") cc1 = covariance_lagged(data=self.data, lag=self.tau, c0t=True, remove_constant_mean=self.mean_constant, bessel=False, weights="koopman") assert np.allclose(cc.mean, self.mx_c) assert np.allclose(cc.mean_tau, self.my_c) assert np.allclose(cc.C00_, self.Mxx_c) assert np.allclose(cc.C0t_, self.Mxy_c) assert np.allclose(cc1.mean, self.mx_c) assert np.allclose(cc1.mean_tau, self.my_c) assert np.allclose(cc1.C00_, self.Mxx_c) assert np.allclose(cc1.C0t_, self.Mxy_c)
def test_XXXY_weightobj_sym_withmean(self): # many passes cc = covariance_lagged(data=self.data, remove_data_mean=False, c0t=True, lag=self.lag, reversible=True, bessel=False, weights=self.wobj, chunksize=self.chunksize) assert np.allclose(cc.mean, self.m_sym_wobj) assert np.allclose(cc.cov, self.Mxx_sym_wobj) assert np.allclose(cc.cov_tau, self.Mxy_sym_wobj)
def test_XXXY_weightobj_meanfree(self): # many passes cc = covariance_lagged(data=self.data, remove_data_mean=True, c0t=True, lag=self.lag, weights=self.wobj, bessel=False, chunksize=self.chunksize) assert np.allclose(cc.mean, self.mx_wobj) assert np.allclose(cc.mean_tau, self.my_wobj) assert np.allclose(cc.cov, self.Mxx0_wobj) assert np.allclose(cc.cov_tau, self.Mxy0_wobj)
def test_XX_weightobj_withmean(self): # many passes cc = covariance_lagged(data=self.data, c0t=False, remove_data_mean=False, weights=self.wobj, bessel=False, chunksize=self.chunksize) np.testing.assert_allclose(cc.mean, self.mx_wobj_lag0) np.testing.assert_allclose(cc.C00_, self.Mxx_wobj_lag0) cc.column_selection = self.cols_2 cc.estimate(self.data) np.testing.assert_allclose(cc.C00_, self.Mxx_wobj_lag0[:, 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) 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 cov = covariance_lagged(data, lag=3, weights=weights, chunksize=10) assert np.all(cov.C00_ < 1)