本文整理汇总了Python中scipy.stats.multivariate_normal.rvs方法的典型用法代码示例。如果您正苦于以下问题:Python multivariate_normal.rvs方法的具体用法?Python multivariate_normal.rvs怎么用?Python multivariate_normal.rvs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.stats.multivariate_normal
的用法示例。
在下文中一共展示了multivariate_normal.rvs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_rvs_shape
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def test_rvs_shape(self):
# Check that rvs parses the mean and covariance correctly, and returns
# an array of the right shape
N = 300
d = 4
sample = multivariate_normal.rvs(mean=np.zeros(d), cov=1, size=N)
assert_equal(sample.shape, (N, d))
sample = multivariate_normal.rvs(mean=None,
cov=np.array([[2, .1], [.1, 1]]),
size=N)
assert_equal(sample.shape, (N, 2))
u = multivariate_normal(mean=0, cov=1)
sample = u.rvs(N)
assert_equal(sample.shape, (N, ))
示例2: test_large_sample
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def test_large_sample(self):
# Generate large sample and compare sample mean and sample covariance
# with mean and covariance matrix.
np.random.seed(2846)
n = 3
mean = np.random.randn(n)
M = np.random.randn(n, n)
cov = np.dot(M, M.T)
size = 5000
sample = multivariate_normal.rvs(mean, cov, size)
assert_allclose(numpy.cov(sample.T), cov, rtol=1e-1)
assert_allclose(sample.mean(0), mean, rtol=1e-1)
示例3: test_frozen_matrix_normal
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def test_frozen_matrix_normal(self):
for i in range(1,5):
for j in range(1,5):
M = 0.3 * np.ones((i,j))
U = 0.5 * np.identity(i) + 0.5 * np.ones((i,i))
V = 0.7 * np.identity(j) + 0.3 * np.ones((j,j))
frozen = matrix_normal(mean=M, rowcov=U, colcov=V)
rvs1 = frozen.rvs(random_state=1234)
rvs2 = matrix_normal.rvs(mean=M, rowcov=U, colcov=V,
random_state=1234)
assert_equal(rvs1, rvs2)
X = frozen.rvs(random_state=1234)
pdf1 = frozen.pdf(X)
pdf2 = matrix_normal.pdf(X, mean=M, rowcov=U, colcov=V)
assert_equal(pdf1, pdf2)
logpdf1 = frozen.logpdf(X)
logpdf2 = matrix_normal.logpdf(X, mean=M, rowcov=U, colcov=V)
assert_equal(logpdf1, logpdf2)
示例4: test_array_input
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def test_array_input(self):
# Check array of inputs has the same output as the separate entries.
num_rows = 4
num_cols = 3
M = 0.3 * np.ones((num_rows,num_cols))
U = 0.5 * np.identity(num_rows) + 0.5 * np.ones((num_rows, num_rows))
V = 0.7 * np.identity(num_cols) + 0.3 * np.ones((num_cols, num_cols))
N = 10
frozen = matrix_normal(mean=M, rowcov=U, colcov=V)
X1 = frozen.rvs(size=N, random_state=1234)
X2 = frozen.rvs(size=N, random_state=4321)
X = np.concatenate((X1[np.newaxis,:,:,:],X2[np.newaxis,:,:,:]), axis=0)
assert_equal(X.shape, (2, N, num_rows, num_cols))
array_logpdf = frozen.logpdf(X)
assert_equal(array_logpdf.shape, (2, N))
for i in range(2):
for j in range(N):
separate_logpdf = matrix_normal.logpdf(X[i,j], mean=M,
rowcov=U, colcov=V)
assert_allclose(separate_logpdf, array_logpdf[i,j], 1E-10)
示例5: test_moments
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def test_moments(self):
# Check that the sample moments match the parameters
num_rows = 4
num_cols = 3
M = 0.3 * np.ones((num_rows,num_cols))
U = 0.5 * np.identity(num_rows) + 0.5 * np.ones((num_rows, num_rows))
V = 0.7 * np.identity(num_cols) + 0.3 * np.ones((num_cols, num_cols))
N = 1000
frozen = matrix_normal(mean=M, rowcov=U, colcov=V)
X = frozen.rvs(size=N, random_state=1234)
sample_mean = np.mean(X,axis=0)
assert_allclose(sample_mean, M, atol=0.1)
sample_colcov = np.cov(X.reshape(N*num_rows,num_cols).T)
assert_allclose(sample_colcov, V, atol=0.1)
sample_rowcov = np.cov(np.swapaxes(X,1,2).reshape(
N*num_cols,num_rows).T)
assert_allclose(sample_rowcov, U, atol=0.1)
示例6: test_det_and_ortho
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def test_det_and_ortho(self):
xs = [[ortho_group.rvs(dim)
for i in range(10)]
for dim in range(2,12)]
# Test that abs determinants are always +1
dets = np.array([[np.linalg.det(x) for x in xx] for xx in xs])
assert_allclose(np.fabs(dets), np.ones(dets.shape), rtol=1e-13)
# Test that we get both positive and negative determinants
# Check that we have at least one and less than 10 negative dets in a sample of 10. The rest are positive by the previous test.
# Test each dimension separately
assert_array_less([0]*10, [np.where(d < 0)[0].shape[0] for d in dets])
assert_array_less([np.where(d < 0)[0].shape[0] for d in dets], [10]*10)
# Test that these are orthogonal matrices
for xx in xs:
for x in xx:
assert_array_almost_equal(np.dot(x, x.T),
np.eye(x.shape[0]))
示例7: test_haar
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def test_haar(self):
# Test that the eigenvalues, which lie on the unit circle in
# the complex plane, are uncorrelated.
# Generate samples
dim = 5
samples = 1000 # Not too many, or the test takes too long
np.random.seed(514) # Note that the test is sensitive to seed too
xs = unitary_group.rvs(dim, size=samples)
# The angles "x" of the eigenvalues should be uniformly distributed
# Overall this seems to be a necessary but weak test of the distribution.
eigs = np.vstack(scipy.linalg.eigvals(x) for x in xs)
x = np.arctan2(eigs.imag, eigs.real)
res = kstest(x.ravel(), uniform(-np.pi, 2*np.pi).cdf)
assert_(res.pvalue > 0.05)
示例8: check_pickling
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def check_pickling(distfn, args):
# check that a distribution instance pickles and unpickles
# pay special attention to the random_state property
# save the random_state (restore later)
rndm = distfn.random_state
distfn.random_state = 1234
distfn.rvs(*args, size=8)
s = pickle.dumps(distfn)
r0 = distfn.rvs(*args, size=8)
unpickled = pickle.loads(s)
r1 = unpickled.rvs(*args, size=8)
assert_equal(r0, r1)
# restore the random_state
distfn.random_state = rndm
示例9: GP
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def GP(seq_length=30, num_samples=28*5*100, num_signals=1, scale=0.1, kernel='rbf', **kwargs):
# the shape of the samples is num_samples x seq_length x num_signals
samples = np.empty(shape=(num_samples, seq_length, num_signals))
#T = np.arange(seq_length)/seq_length # note, between 0 and 1
T = np.arange(seq_length) # note, not between 0 and 1
if kernel == 'periodic':
cov = periodic_kernel(T)
elif kernel =='rbf':
cov = rbf_kernel(T.reshape(-1, 1), gamma=scale)
else:
raise NotImplementedError
# scale the covariance
cov *= 0.2
# define the distribution
mu = np.zeros(seq_length)
print(np.linalg.det(cov))
distribution = multivariate_normal(mean=np.zeros(cov.shape[0]), cov=cov)
pdf = distribution.logpdf
# now generate samples
for i in range(num_signals):
samples[:, :, i] = distribution.rvs(size=num_samples)
return samples, pdf
示例10: test_alternatives
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def test_alternatives(self):
""" Test implemented alternative hypotheses. """
np.random.seed(42)
n = 100
a = mvn.rvs(mean=0, cov=1, size=n).reshape((n, 1))
b = mvn.rvs(mean=1, cov=1, size=n).reshape((n, 1))
c = mvn.rvs(mean=-1, cov=1, size=n).reshape((n, 1))
for method in (statistics.score_t_test, statistics.score_hypergeometric_test, statistics.score_mann_whitney):
_, pval_less = method(a, b, alternative=statistics.ALT_LESS)
_, pval_two = method(a, b, alternative=statistics.ALT_TWO)
_, pval_greater = method(a, b, alternative=statistics.ALT_GREATER)
assert np.all(pval_less < pval_two < pval_greater)
assert np.linalg.norm(pval_less + pval_greater - np.array([1.0])) < 1e-8
_, pval_less = method(b, c, alternative=statistics.ALT_LESS)
_, pval_two = method(b, c, alternative=statistics.ALT_TWO)
_, pval_greater = method(b, c, alternative=statistics.ALT_GREATER)
assert np.all(pval_greater < pval_two < pval_less)
assert np.linalg.norm(pval_less + pval_greater - np.array([1.0])) < 1e-8
示例11: test_alternatives_2d
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def test_alternatives_2d(self):
""" Test implemented alternative hypotheses. """
np.random.seed(42)
n = 100
a1 = mvn.rvs(mean=0, cov=1, size=n).reshape((n, 1))
a2 = mvn.rvs(mean=1, cov=1, size=n).reshape((n, 1))
b1 = mvn.rvs(mean=2, cov=1, size=n).reshape((n, 1))
b2 = mvn.rvs(mean=3, cov=1, size=n).reshape((n, 1))
_a = np.hstack((a1, a2))
_b = np.hstack((b1, b2))
for method in (statistics.score_t_test, statistics.score_hypergeometric_test, statistics.score_mann_whitney):
_, pval_less = method(_a, _b, alternative=statistics.ALT_LESS)
_, pval_two = method(_a, _b, alternative=statistics.ALT_TWO)
_, pval_greater = method(_a, _b, alternative=statistics.ALT_GREATER)
assert np.all(np.logical_and(pval_less < pval_two, pval_two < pval_greater))
assert np.linalg.norm(pval_less + pval_greater - np.array([1.0])) < 1e-8
示例12: function
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def function(self, state, noise=False, **kwargs):
"""Model linear function :math:`f_k(x(k),w(k)) = F_k(x_k) + w_k`
Parameters
----------
state: :class:`~.State`
An input state
noise: :class:`numpy.ndarray` or bool
An externally generated random process noise sample (the default is
`False`, in which case no noise will be added
if 'True', the output of :meth:`~.Model.rvs` is added)
Returns
-------
: :class:`State`
The updated State with the model function evaluated.
"""
if isinstance(noise, bool) or noise is None:
if noise:
noise = self.rvs(**kwargs)
else:
noise = 0
return self.matrix(**kwargs) @ state.state_vector + noise
示例13: test_multivariate_normal_rvs_zero_covariance
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def test_multivariate_normal_rvs_zero_covariance(self):
mean = np.zeros(2)
covariance = np.zeros((2, 2))
model = multivariate_normal(mean, covariance, allow_singular=True)
sample = model.rvs()
assert_equal(sample, [0, 0])
示例14: test_default_inputs
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def test_default_inputs(self):
# Check that default argument handling works
num_rows = 4
num_cols = 3
M = 0.3 * np.ones((num_rows,num_cols))
U = 0.5 * np.identity(num_rows) + 0.5 * np.ones((num_rows, num_rows))
V = 0.7 * np.identity(num_cols) + 0.3 * np.ones((num_cols, num_cols))
Z = np.zeros((num_rows, num_cols))
Zr = np.zeros((num_rows, 1))
Zc = np.zeros((1, num_cols))
Ir = np.identity(num_rows)
Ic = np.identity(num_cols)
I1 = np.identity(1)
assert_equal(matrix_normal.rvs(mean=M, rowcov=U, colcov=V).shape,
(num_rows, num_cols))
assert_equal(matrix_normal.rvs(mean=M).shape,
(num_rows, num_cols))
assert_equal(matrix_normal.rvs(rowcov=U).shape,
(num_rows, 1))
assert_equal(matrix_normal.rvs(colcov=V).shape,
(1, num_cols))
assert_equal(matrix_normal.rvs(mean=M, colcov=V).shape,
(num_rows, num_cols))
assert_equal(matrix_normal.rvs(mean=M, rowcov=U).shape,
(num_rows, num_cols))
assert_equal(matrix_normal.rvs(rowcov=U, colcov=V).shape,
(num_rows, num_cols))
assert_equal(matrix_normal(mean=M).rowcov, Ir)
assert_equal(matrix_normal(mean=M).colcov, Ic)
assert_equal(matrix_normal(rowcov=U).mean, Zr)
assert_equal(matrix_normal(rowcov=U).colcov, I1)
assert_equal(matrix_normal(colcov=V).mean, Zc)
assert_equal(matrix_normal(colcov=V).rowcov, I1)
assert_equal(matrix_normal(mean=M, rowcov=U).colcov, Ic)
assert_equal(matrix_normal(mean=M, colcov=V).rowcov, Ir)
assert_equal(matrix_normal(rowcov=U, colcov=V).mean, Z)
示例15: test_1D_is_chisquared
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import rvs [as 别名]
def test_1D_is_chisquared(self):
# The 1-dimensional Wishart with an identity scale matrix is just a
# chi-squared distribution.
# Test variance, mean, entropy, pdf
# Kolgomorov-Smirnov test for rvs
np.random.seed(482974)
sn = 500
dim = 1
scale = np.eye(dim)
df_range = np.arange(1, 10, 2, dtype=float)
X = np.linspace(0.1,10,num=10)
for df in df_range:
w = wishart(df, scale)
c = chi2(df)
# Statistics
assert_allclose(w.var(), c.var())
assert_allclose(w.mean(), c.mean())
assert_allclose(w.entropy(), c.entropy())
# PDF
assert_allclose(w.pdf(X), c.pdf(X))
# rvs
rvs = w.rvs(size=sn)
args = (df,)
alpha = 0.01
check_distribution_rvs('chi2', args, alpha, rvs)