def test_NKDE_log_pdf(self): X, Y = np.random.normal(size=(500, 2)), np.random.normal(size=(500, 2)) model = NeighborKernelDensityEstimation() model.fit(X, Y) x, y = np.random.normal(size=(100, 2)), np.random.normal(size=(100, 2)) prob = model.pdf(x, y) log_prob = model.log_pdf(x, y) self.assertLessEqual(np.mean(np.abs(prob - np.exp(log_prob))), 0.001)
def generate_report(): econ_density = EconDensity() X, Y = econ_density.simulate(n_samples=1000) nke = NeighborKernelDensityEstimation() nke.fit_by_cv(X, Y) n_samples = 500 X_test = np.asarray([1 for _ in range(n_samples)]) Y_test = np.linspace(0, 8, num=n_samples) Z = nke.pdf(X_test, Y_test)
def test_NKDE_with_2d_gaussian(self): X = np.random.uniform(-1, 1, size=4000) Y = (2 + X) * np.random.normal(size=4000) + 2 * X for weighted in [True, False]: model = NeighborKernelDensityEstimation(epsilon=0.3, weighted=weighted) model.fit(X, Y) y = np.linspace(-5, 5, num=100) x = np.ones(100) * 0 p_est = model.pdf(x, y) p_true = norm.pdf(y, loc=0, scale=2) self.assertLessEqual(np.mean(np.abs(p_true - p_est)), 0.1) y = np.linspace(-5, 5, num=100) x = -np.ones(100) * 0.5 p_est = model.pdf(x, y) p_true = norm.pdf(y, loc=-1, scale=1.5) self.assertLessEqual(np.mean(np.abs(p_true - p_est)), 0.1)
def test_NKDE_with_4d_gaussian(self): mu = 5 std = 2.0 X = np.random.normal(loc=mu, scale=std, size=(4000, 2)) Y = np.random.normal(loc=mu, scale=std, size=(4000, 2)) model = NeighborKernelDensityEstimation(epsilon=0.3) model.fit(X, Y) y = np.random.uniform(low=[1.0, 1.0], high=[9.0, 9.0], size=(500, 2)) x = np.ones(shape=(500, 2)) * mu p_est = model.pdf(x, y) p_true = stats.multivariate_normal.pdf(y, mean=np.ones(2) * mu, cov=std**2) self.assertLessEqual(np.mean(np.abs(p_true - p_est)), 0.1)