def test_cosine_sample_shape(seed, allclose): """"Tests that CosineSimilarity sample has correct shape.""" # sampling (n, d) should be the exact same as sampling (n*d,) n = 3 d = 4 dist = CosineSimilarity(2) a = dist.sample(n, d, rng=np.random.RandomState(seed)) b = dist.sample(n * d, rng=np.random.RandomState(seed)) assert allclose(a.flatten(), b)
def test_cosine_similarity(d, rng): """Tests CosineSimilarity sampling.""" num_samples = 2500 num_bins = 8 # Check that it gives a single dimension from UniformHypersphere exp_dist = UniformHypersphere(surface=True) act_dist = CosineSimilarity(d) exp = exp_dist.sample(num_samples, d, rng=rng)[:, 0] act = act_dist.sample(num_samples, rng=rng) exp_hist, _ = np.histogram(exp, bins=num_bins) act_hist, _ = np.histogram(act, bins=num_bins) assert np.all(np.abs(np.asfarray(exp_hist - act_hist) / num_samples) < 0.15)