def test_distributions_mgd_random_sample(): mu = numpy.array([5., 1.]) cov = numpy.eye(2) d = MultivariateGaussianDistribution(mu, cov) x = numpy.array([[5.441227, 0.66913], [7.430771, 0.747908], [5.10961, 2.582481]]) assert_array_almost_equal(d.sample(3, random_state=5), x) assert_raises(AssertionError, assert_array_almost_equal, d.sample(5), x)
def test_distributions_mgd_random_sample(): mu = numpy.array([5., 1.]) cov = numpy.eye(2) d = MultivariateGaussianDistribution(mu, cov) x = numpy.array([[5.441227, 0.66913 ], [7.430771, 0.747908], [5.10961 , 2.582481]]) assert_array_almost_equal(d.sample(3, random_state=5), x) assert_raises(AssertionError, assert_array_almost_equal, d.sample(5), x)
def sample(self, n=10): try: X1 = np.stack(self.model.sample(int(np.round(0.8 * n)))) # np.round 取最近的整数 # 从多元高斯分布中采样8个点 if self.dim == 2: mean = np.mean(X1, axis=0) # 由X1的采样点计算均值 std = np.diag([1.0, 1.0]) gmm = MultivariateGaussianDistribution(mean, std) # 构建一个新的多元高斯分布 X2 = np.stack(gmm.sample(int(np.round(0.1 * n)))) # 采样1个点 mean = np.mean(X1, axis=0) std = np.diag([1e-3, 1e-3]) # 标准差不同了 gmm = MultivariateGaussianDistribution(mean, std) X3 = np.stack(gmm.sample(int(np.round(0.1 * n)))) # 又采样了一个点 else: mean = np.mean(X1, axis=0) std = np.diag([1.0, 1.0, 1e-3]) gmm = MultivariateGaussianDistribution(mean, std) X2 = np.stack(gmm.sample(int(np.round(0.1 * n)))) mean = np.mean(X1, axis=0) std = np.diag([1e-3, 1e-3, 10.0]) gmm = MultivariateGaussianDistribution(mean, std) X3 = np.stack(gmm.sample(int(np.round(0.1 * n)))) X = np.concatenate((X1, X2, X3)) # 多尺度采样? except ValueError: print("exception caught on sampling") if self.dim == 2: mean = np.zeros(self.dim) std = np.diag([1.0, 1.0]) gmm = MultivariateGaussianDistribution(mean, std) X = gmm.sample(int(n)) else: mean = np.zeros(self.dim) std = np.diag([1.0, 1.0, 5.0]) gmm = MultivariateGaussianDistribution(mean, std) X = gmm.sample(int(n)) return X
def sample(self, n=10): try: X1 = np.stack(self.model.sample(int(np.round(0.8 * n)))) if self.dim == 2: mean = np.mean(X1, axis=0) std = np.diag([1.0, 1.0]) gmm = MultivariateGaussianDistribution(mean, std) X2 = np.stack(gmm.sample(int(np.round(0.1 * n)))) mean = np.mean(X1, axis=0) std = np.diag([1e-3, 1e-3]) gmm = MultivariateGaussianDistribution(mean, std) X3 = np.stack(gmm.sample(int(np.round(0.1 * n)))) else: mean = np.mean(X1, axis=0) std = np.diag([1.0, 1.0, 1e-3]) gmm = MultivariateGaussianDistribution(mean, std) X2 = np.stack(gmm.sample(int(np.round(0.1 * n)))) mean = np.mean(X1, axis=0) std = np.diag([1e-3, 1e-3, 10.0]) gmm = MultivariateGaussianDistribution(mean, std) X3 = np.stack(gmm.sample(int(np.round(0.1 * n)))) X = np.concatenate((X1, X2, X3)) except ValueError: print("exception caught on sampling") if self.dim == 2: mean = np.zeros(self.dim) std = np.diag([1.0, 1.0]) gmm = MultivariateGaussianDistribution(mean, std) X = gmm.sample(int(n)) else: mean = np.zeros(self.dim) std = np.diag([1.0, 1.0, 5.0]) gmm = MultivariateGaussianDistribution(mean, std) X = gmm.sample(int(n)) return X