def test1_KMN_with_2d_gaussian_noise_y(self):
        X, Y = self.get_samples(std=0.5)

        with tf.Session():
            model_no_noise = KernelMixtureNetwork("kmn_no_noise_y",
                                                  1,
                                                  1,
                                                  n_centers=5,
                                                  x_noise_std=None,
                                                  y_noise_std=None)
            model_no_noise.fit(X, Y)
            var_no_noise = model_no_noise.covariance(
                x_cond=np.array([[2]]))[0][0][0]

            model_noise = KernelMixtureNetwork("kmn_noise_y",
                                               1,
                                               1,
                                               n_centers=5,
                                               x_noise_std=None,
                                               y_noise_std=1)
            model_noise.fit(X, Y)
            var_noise = model_noise.covariance(x_cond=np.array([[2]]))[0][0][0]

            print("Training w/o noise:", var_no_noise)
            print("Training w/ noise:", var_noise)

            self.assertGreaterEqual(var_noise - var_no_noise, 0.1)
    def test9_data_normalization(self):
        np.random.seed(24)
        mean = -80
        std = 7
        data = np.random.normal([mean, mean, mean, mean], std, size=(4000, 4))
        X = data[:, 0:2]
        Y = data[:, 2:4]

        with tf.Session():
            model = KernelMixtureNetwork("kmn_data_normalization_2",
                                         2,
                                         2,
                                         n_centers=5,
                                         x_noise_std=None,
                                         y_noise_std=None,
                                         data_normalization=True,
                                         n_training_epochs=2000,
                                         random_seed=22,
                                         keep_edges=False,
                                         train_scales=True,
                                         weight_normalization=True,
                                         init_scales=np.array([1.0]))

            model.fit(X, Y)
            cond_mean = model.mean_(Y)
            print(np.mean(cond_mean))
            mean_diff = np.abs(mean - np.mean(cond_mean))
            self.assertLessEqual(mean_diff, np.abs(mean) * 0.1)

            cond_cov = np.mean(model.covariance(Y), axis=0)
            print(cond_cov)
            self.assertGreaterEqual(cond_cov[0][0], std**2 * 0.7)
            self.assertLessEqual(cond_cov[0][0], std**2 * 1.3)
            self.assertGreaterEqual(cond_cov[1][1], std**2 * 0.7)
            self.assertLessEqual(cond_cov[1][1], std**2 * 1.3)
    def test_KMN_with_2d_gaussian_sampling(self):
        np.random.seed(22)
        X, Y = self.get_samples(mu=5)

        import time
        t = time.time()
        model = KernelMixtureNetwork("kmn_sampling",
                                     1,
                                     1,
                                     center_sampling_method='k_means',
                                     n_centers=5,
                                     n_training_epochs=1000,
                                     data_normalization=True)
        print("time to build model:", time.time() - t)
        t = time.time()

        model.fit(X, Y)
        print("time to fit model:", time.time() - t)

        x_cond = 5 * np.ones(shape=(2000000, 1))
        _, y_sample = model.sample(x_cond)
        print(np.mean(y_sample), np.std(y_sample))
        self.assertAlmostEqual(np.mean(y_sample),
                               float(model.mean_(x_cond[1])),
                               places=1)
        self.assertAlmostEqual(np.std(y_sample),
                               float(model.covariance(x_cond[1])),
                               places=1)

        x_cond = np.ones(shape=(400000, 1))
        x_cond[0, 0] = 5.0
        _, y_sample = model.sample(x_cond)
        self.assertAlmostEqual(np.mean(y_sample),
                               float(model.mean_(x_cond[1])),
                               places=1)
        self.assertAlmostEqual(np.std(y_sample),
                               float(np.sqrt(model.covariance(x_cond[1]))),
                               places=1)