Example #1
0
    def test_small_data(self):
        # pylint: disable=no-self-use
        """
        Tests that all methods return valid data.
        Valid data is defined as not all values equal to 0.
        """
        # Initialize parameters.
        dims = 3
        etas = 2
        n_samples = 1
        beta_params = (50, 2)

        # Fixed samples for each method.
        cvine_sample = [[[1.0, -0.44642099, 0.02443349],
                         [-0.44642099, 1.0, -0.72701905],
                         [0.02443349, -0.72701905, 1.0]]]
        ext_onion_sample = [[[1.0, 0.78932386, -0.56877586],
                             [0.78932386, 1.0, -0.47447423],
                             [-0.56877586, -0.47447423, 1.0]]]
        dvine_sample = [[[1.0, 0.30937182, 0.78298863],
                         [0.30937182, 1.0, 0.63411242],
                         [0.78298863, 0.63411242, 1.0]]]
        dvine_beta_sample = [[[1.0, 0.9743718, 0.8824407],
                              [0.9743718, 1.0, 0.86273572],
                              [0.8824407, 0.86273572, 1.0]]]
        cvine_beta_sample = [[[1.0, 0.9282246, 0.95633512],
                              [0.9282246, 1.0, 0.92777712],
                              [0.95633512, 0.92777712, 1.0]]]

        # Test all methods.
        corr_mats = sample_from_cvine(dim=dims, eta=etas, n_samples=n_samples)
        np.testing.assert_array_almost_equal(corr_mats, cvine_sample, 5)
        corr_mats = sample_from_ext_onion(dim=dims,
                                          eta=etas,
                                          n_samples=n_samples)
        np.testing.assert_array_almost_equal(corr_mats, ext_onion_sample, 5)
        corr_mats = sample_from_dvine(dim=dims, n_samples=n_samples)
        np.testing.assert_array_almost_equal(corr_mats, dvine_sample, 5)

        # Test fixed beta parameters for D-vine.
        corr_mats = sample_from_dvine(dim=dims,
                                      n_samples=n_samples,
                                      beta_dist_fixed=beta_params)
        np.testing.assert_array_almost_equal(corr_mats, dvine_beta_sample, 5)

        # Test fixed beta parameters for C-vine.
        corr_mats = sample_from_cvine(dim=dims,
                                      eta=etas,
                                      n_samples=n_samples,
                                      beta_dist_fixed=beta_params)
        np.testing.assert_array_almost_equal(corr_mats, cvine_beta_sample, 5)
Example #2
0
    def test_data_returned(self):
        """
        Tests that data generated from all methods exists for multiple dimensions and
        parameters.
        """

        # Test all methods.
        for func in [
                sample_from_cvine, sample_from_ext_onion, sample_from_dvine
        ]:
            if func == sample_from_dvine:
                corr_mat = func(dim=self.dims[0], n_samples=self.n_samples[0])
            else:
                corr_mat = func(dim=self.dims[0],
                                eta=self.etas[0],
                                n_samples=self.n_samples[0])

            self.assertTrue(self._data_exists(corr_mat),
                            msg="{} failed".format(func.__name__))

        # Test fixed beta parameters for D-vine.
        corr_mat = sample_from_dvine(dim=self.dims[0],
                                     n_samples=self.n_samples[0],
                                     beta_dist_fixed=self.beta_params)
        self.assertTrue(self._data_exists(corr_mat))

        # Test fixed beta parameters for C-vine.
        corr_mat = sample_from_cvine(dim=self.dims[0],
                                     n_samples=self.n_samples[0],
                                     beta_dist_fixed=self.beta_params)
        self.assertTrue(self._data_exists(corr_mat))
Example #3
0
    def test_matrix_is_symmetrical(self):
        """
        Tests that the matrices from all methods generated are symmetrical.
        """

        # Test all methods.
        for func in [
                sample_from_cvine, sample_from_ext_onion, sample_from_dvine
        ]:
            for i in range(self.n_generated):
                if func == sample_from_dvine:
                    corr_mats = func(dim=self.dims[i],
                                     n_samples=self.n_samples[i])
                else:
                    corr_mats = func(dim=self.dims[i],
                                     eta=self.etas[i],
                                     n_samples=self.n_samples[i])

                self.assertTrue(
                    self._data_is_symmetric(corr_mats, self.dims[i]),
                    msg="{} failed".format(func.__name__),
                )

        # Test fixed beta parameters for D-vine.
        corr_mats = sample_from_dvine(dim=self.dims[0],
                                      n_samples=self.n_samples[0],
                                      beta_dist_fixed=self.beta_params)
        self.assertTrue(self._data_is_symmetric(corr_mats, self.dims[0]))

        # Test fixed beta parameters for C-vine.
        corr_mats = sample_from_cvine(dim=self.dims[0],
                                      n_samples=self.n_samples[0],
                                      beta_dist_fixed=self.beta_params)
        self.assertTrue(self._data_is_symmetric(corr_mats, self.dims[0]))
Example #4
0
    def test_data_generated_is_correctly_bounded(self):
        """
        Tests that the data generated from all methods is bounded to the range
        -1 <= data <= 1 which corresponds to any correlation matrix bounds,
        and that the diagonal is always equal to 1s.
        """

        # Test all methods.
        for func in [
                sample_from_cvine, sample_from_ext_onion, sample_from_dvine
        ]:
            for i in range(self.n_generated):
                if func == sample_from_dvine:
                    corr_mats = func(dim=self.dims[i],
                                     n_samples=self.n_samples[i])
                else:
                    corr_mats = func(dim=self.dims[i],
                                     eta=self.etas[i],
                                     n_samples=self.n_samples[i])

                self.assertTrue(
                    self._data_correctly_bounded(corr_mats, self.dims[i]),
                    msg="{} failed".format(func.__name__),
                )

        # Test fixed beta parameters for D-vine.
        corr_mats = sample_from_dvine(dim=self.dims[0],
                                      n_samples=self.n_samples[0],
                                      beta_dist_fixed=self.beta_params)
        self.assertTrue(self._data_correctly_bounded(corr_mats, self.dims[0]))

        # Test fixed beta parameters for C-vine.
        corr_mats = sample_from_cvine(dim=self.dims[0],
                                      eta=self.etas[0],
                                      n_samples=self.n_samples[0],
                                      beta_dist_fixed=self.beta_params)
        self.assertTrue(self._data_correctly_bounded(corr_mats, self.dims[0]))
Example #5
0
    def test_correct_shapes(self):
        """
        Tests that data generated from all methods exists for multiple dimensions and
        parameters.
        """
        # Test all methods.
        for func in [
                sample_from_cvine, sample_from_ext_onion, sample_from_dvine
        ]:
            for i in range(self.n_generated):
                if func == sample_from_dvine:
                    corr_mat = func(dim=self.dims[i],
                                    n_samples=self.n_samples[i])
                else:
                    corr_mat = func(dim=self.dims[i],
                                    eta=self.etas[i],
                                    n_samples=self.n_samples[i])

                self.assertTrue(
                    corr_mat.shape == (self.n_samples[i], self.dims[i],
                                       self.dims[i]),
                    msg="{} failed".format(func.__name__),
                )

        # Test fixed beta parameters for D-vine.
        corr_mat = sample_from_dvine(dim=self.dims[0],
                                     n_samples=self.n_samples[0],
                                     beta_dist_fixed=self.beta_params)
        self.assertTrue(corr_mat.shape == (self.n_samples[0], self.dims[0],
                                           self.dims[0]))

        # Test fixed beta parameters for C-vine.
        corr_mat = sample_from_cvine(dim=self.dims[0],
                                     n_samples=self.n_samples[0],
                                     beta_dist_fixed=self.beta_params)
        self.assertTrue(corr_mat.shape == (self.n_samples[0], self.dims[0],
                                           self.dims[0]))