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)
X, Y = density_simulator.simulate(n_samples=3000)
""" fit density model """
model = KernelMixtureNetwork("KDE_demo",
                             ndim_x=1,
                             ndim_y=1,
                             n_centers=50,
                             x_noise_std=0.2,
                             y_noise_std=0.1,
                             random_seed=22)
model.fit(X, Y)
""" query the conditional pdf and cdf"""
x_cond = np.zeros((1, 1))
y_query = np.ones((1, 1)) * 0.1
prob = model.pdf(x_cond, y_query)
cum_prob = model.cdf(x_cond, y_query)
""" compute conditional moments & VaR  """
x_cond = np.zeros((1, 1))

mean = model.mean_(x_cond)[0][0]
std = model.std_(x_cond)[0][0]
skewness = model.skewness(x_cond)[0]
VaR = model.value_at_risk(x_cond, alpha=0.01)[0]

print("Mean:", mean)
print("Std:", std)
print("Skewness:", skewness)
print("Value-at-Risk", VaR)
""" plot the fitted distribution """
x_cond_plot = np.array([-0.5, 0, 0.5])
model.plot2d(x_cond_plot, ylim=(-0.25, 0.15), show=True)