Beispiel #1
0
def test_whitening_on_whitened():
    data = np.random.normal(size=(1000, 50))
    from sklearn.decomposition import PCA
    data = PCA(whiten=True).fit_transform(data)
    cov = Covariance().fit(data).fetch_model()
    whitened = cov.whiten(data)
    np.testing.assert_array_almost_equal(whitened, data)
Beispiel #2
0
 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])
Beispiel #3
0
def test_multiple_fetch():
    # checks that the model instance does not change when the estimator was not updated
    data = np.random.normal(size=(5000, 3))
    est = Covariance(5, compute_c00=True, compute_c0t=False, compute_ctt=False)
    m1 = est.fit(data).model
    m2 = est.model
    m3 = est.partial_fit(np.random.normal(size=(50, 3))).model
    np.testing.assert_(m1 is m2)
    np.testing.assert_(m1 is not m3)
    np.testing.assert_(m2 is not m3)
Beispiel #4
0
    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])
Beispiel #5
0
 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])
Beispiel #6
0
 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])
Beispiel #7
0
 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])
Beispiel #8
0
 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])
Beispiel #9
0
 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()
Beispiel #10
0
 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])
Beispiel #11
0
 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])
Beispiel #12
0
 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])
Beispiel #13
0
    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)
Beispiel #14
0
 def test_XXXY_weightobj_meanfree(self):
     for n_splits in [1, 2, 3, 4, 5, 6, 7]:
         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=n_splits).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=n_splits).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])
Beispiel #15
0
def test_weights_incompatible():
    data = np.random.normal(size=(5000, 3))
    est = Covariance(5)
    with np.testing.assert_raises(ValueError):
        est.fit(data, weights=np.arange(10))  # incompatible shape

    with np.testing.assert_raises(ValueError):
        est.fit(data, weights=np.ones((len(data), 2)))  # incompatible shape
Beispiel #16
0
def test_weights():
    weights = np.concatenate([np.ones((1001, )) * 1e-16, np.ones((3999, ))])
    np.testing.assert_equal(len(weights), 5000)
    data = np.random.normal(size=(5000, 2))
    cov = Covariance(lagtime=5,
                     compute_c00=True,
                     compute_c0t=True,
                     compute_ctt=False)
    model = cov.fit(data, weights=weights, n_splits=64).fetch_model()
    model2 = cov.fit(data[1002:], weights=weights[1002:],
                     n_splits=55).fetch_model()
    np.testing.assert_array_almost_equal(model.cov_00,
                                         model2.cov_00,
                                         decimal=2)
    np.testing.assert_array_almost_equal(model.cov_0t,
                                         model2.cov_0t,
                                         decimal=2)
    np.testing.assert_array_almost_equal(model.mean_0,
                                         model2.mean_0,
                                         decimal=2)
    np.testing.assert_array_almost_equal(model.mean_t,
                                         model2.mean_t,
                                         decimal=2)
Beispiel #17
0
def test_whitening():
    data = np.random.normal(size=(5000, 50))
    data = Covariance().fit(data).fetch_model().whiten(data)
    cov = Covariance().fit(data).fetch_model()
    np.testing.assert_array_almost_equal(cov.cov_00, np.eye(50), decimal=2)
    np.testing.assert_array_almost_equal(cov.mean_0, np.zeros_like(cov.mean_0))