def test_Lp_generalized_normal(): """ Tests :meth:`bet.Lp_generalized_samples.Lp_generalized_normal` This test only verifies the mean, but not the variance. """ # 1D nptest.assert_allclose(np.mean(lp.Lp_generalized_normal(1, 1000), 0), np.zeros((1,)), atol=1e-1) # 2D nptest.assert_allclose(np.mean(lp.Lp_generalized_normal(2, 1000), 0), np.zeros((2,)), atol=1e-1) # 3D nptest.assert_allclose(np.mean(lp.Lp_generalized_normal(3, 1000), 0), np.zeros((3,)), atol=1e-1)
def test_Lp_generalized_uniform(): """ Tests :meth:`bet.Lp_generalized_samples.Lp_generalized_uniform` This test only verifies the mean, but not the variance. """ # 1D x = lp.Lp_generalized_uniform(1, 1000) nptest.assert_allclose(np.mean(x, 0), np.zeros((1,)), atol=1e-1) assert np.all(np.logical_and(x <= 1., x >= -1)) # 2D p = 1 x = lp.Lp_generalized_uniform(2, 1000, p) verify_norm_and_mean(x, 1.0, p) p = 2 x = lp.Lp_generalized_uniform(2, 1000, p) verify_norm_and_mean(x, 1.0, p) p = 3 x = lp.Lp_generalized_uniform(2, 1000, p) verify_norm_and_mean(x, 1.0, p) p = np.inf x = lp.Lp_generalized_uniform(2, 1000, p) verify_norm_and_mean(x, 1.0, p) # 3D p = 1 x = lp.Lp_generalized_uniform(3, 1000, p) verify_norm_and_mean(x, 1.0, p) p = 2 x = lp.Lp_generalized_uniform(3, 1000, p) verify_norm_and_mean(x, 1.0, p) p = 3 x = lp.Lp_generalized_uniform(3, 1000, p) verify_norm_and_mean(x, 1.0, p) p = np.inf x = lp.Lp_generalized_uniform(3, 1000, p) verify_norm_and_mean(x, 1.0, p)
def sample_lp_ball(input_set, num_close, radius, p_num=2): r""" Pick num_close points in a the Lp ball of length 2*``radii_vec`` around a point in the input space, do this for each point in centers. If this box extends outside of the domain of the input space, we sample the intersection. :param input_set: The input sample set. Make sure the attribute ``_values`` is not ``None`` :type input_set: :class:`~bet.sample.sample_set` :param int num_close: Number of points in each cluster :param radii_vec: Each side of the box will have length ``2*radii_vec[i]`` :type radii_vec: :class:`numpy.ndarray` of shape (``input_dim``,) :param float p_num: :math:`0 < p \leq \infty`, p for the lp norm where infinity is ``numpy.inf`` :rtype: :class:`~bet.sample.sample_set` :returns: Centers and clusters of samples near each center (values are :class:`numpy.ndarray` of shape ((``num_close+1``)*``num_centers``, ``input_dim``)) """ if input_set.get_values() is None: raise ValueError("You must have values to use this method.") input_dim = input_set.get_dim() centers = input_set.get_values() num_centers = input_set.check_num() input_domain = input_set.get_domain() cluster_set = sample.sample_set(input_dim) if input_domain is not None: cluster_set.set_domain(input_domain) cluster_set.set_values(centers) for i in xrange(num_centers): in_bounds = 0 inflate = 1.0 while in_bounds < num_close: # sample uniformly new_cluster = lpsam.Lp_generalized_uniform(input_dim, num_close * inflate, p_num, radius, centers[i, :]) # check bounds if input_domain is not None: cluster_set.update_bounds(num_close * inflate) left = np.all(np.greater_equal(new_cluster, cluster_set._left), axis=1) right = np.all(np.less_equal(new_cluster, cluster_set._right), axis=1) inside = np.logical_and(left, right) in_bounds = np.sum(inside) new_cluster = new_cluster[inside, :] # increase inflate inflate *= 10.0 else: in_bounds = num_close if in_bounds > num_close: new_cluster = new_cluster[:num_close, :] cluster_set.append_values(new_cluster) # reset bounds cluster_set._left = None cluster_set._right = None cluster_set._width = None return cluster_set