本文整理汇总了Python中scipy.stats.multivariate_normal.logpdf方法的典型用法代码示例。如果您正苦于以下问题:Python multivariate_normal.logpdf方法的具体用法?Python multivariate_normal.logpdf怎么用?Python multivariate_normal.logpdf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.stats.multivariate_normal
的用法示例。
在下文中一共展示了multivariate_normal.logpdf方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_matches_multivariate
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def test_matches_multivariate(self):
# Check that the pdfs match those obtained by vectorising and
# treating as a multivariate normal.
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)
X = frozen.rvs(random_state=1234)
pdf1 = frozen.pdf(X)
logpdf1 = frozen.logpdf(X)
vecX = X.T.flatten()
vecM = M.T.flatten()
cov = np.kron(V,U)
pdf2 = multivariate_normal.pdf(vecX, mean=vecM, cov=cov)
logpdf2 = multivariate_normal.logpdf(vecX, mean=vecM, cov=cov)
assert_allclose(pdf1, pdf2, rtol=1E-10)
assert_allclose(logpdf1, logpdf2, rtol=1E-10)
示例2: test_array_input
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [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)
示例3: test_frozen_dirichlet
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def test_frozen_dirichlet(self):
np.random.seed(2846)
n = np.random.randint(1, 32)
alpha = np.random.uniform(10e-10, 100, n)
d = dirichlet(alpha)
assert_equal(d.var(), dirichlet.var(alpha))
assert_equal(d.mean(), dirichlet.mean(alpha))
assert_equal(d.entropy(), dirichlet.entropy(alpha))
num_tests = 10
for i in range(num_tests):
x = np.random.uniform(10e-10, 100, n)
x /= np.sum(x)
assert_equal(d.pdf(x[:-1]), dirichlet.pdf(x[:-1], alpha))
assert_equal(d.logpdf(x[:-1]), dirichlet.logpdf(x[:-1], alpha))
示例4: GP
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [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
示例5: changepoint_pdf
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def changepoint_pdf(Y, cov_ms, cov_Ms):
"""
"""
seq_length = Y.shape[0]
logpdf = []
for (i, m) in enumerate(range(int(seq_length/2), seq_length-1)):
Y_m = Y[:m, 0]
Y_M = Y[m:, 0]
M = seq_length - m
# generate mean function for second part
Ymin = np.min(Y_m)
initial_val = Y_m[-1]
if Ymin > 1:
final_val = (1.0 - M/seq_length)*Ymin
else:
final_val = (1.0 + M/seq_length)*Ymin
mu_M = np.linspace(initial_val, final_val, M)
# ah yeah
logpY_m = multivariate_normal.logpdf(Y_m, mean=np.zeros(m), cov=cov_ms[i])
logpY_M = multivariate_normal.logpdf(Y_M, mean=mu_M, cov=cov_Ms[i])
logpdf_m = logpY_m + logpY_M
logpdf.append(logpdf_m)
return logpdf
示例6: logpdf
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def logpdf(x, mean=None, cov=1, allow_singular=True):
"""
Computes the log of the probability density function of the normal
N(mean, cov) for the data x. The normal may be univariate or multivariate.
Wrapper for older versions of scipy.multivariate_normal.logpdf which
don't support support the allow_singular keyword prior to verion 0.15.0.
If it is not supported, and cov is singular or not PSD you may get
an exception.
`x` and `mean` may be column vectors, row vectors, or lists.
"""
if mean is not None:
flat_mean = np.asarray(mean).flatten()
else:
flat_mean = None
flat_x = np.asarray(x).flatten()
if _support_singular:
return multivariate_normal.logpdf(flat_x, flat_mean, cov, allow_singular)
return multivariate_normal.logpdf(flat_x, flat_mean, cov)
示例7: predict
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def predict(self, X):
X.interpolate(inplace=True)
X.bfill(inplace=True)
self.model.eval()
input_data, target_data = self._input_and_target_data_eval(X)
predictions = self.model(input_data)
errors, stacked_preds = self._calc_errors(predictions, target_data, return_stacked_predictions=True)
if self.details:
self.prediction_details.update({'predictions_mean': np.pad(
stacked_preds.mean(axis=3).squeeze(0).T, ((0, 0), (self.len_in + self.len_out - 1, 0)),
'constant', constant_values=np.nan)})
self.prediction_details.update({'errors_mean': np.pad(
errors.mean(axis=3).reshape(-1), (self.len_in + self.len_out - 1, 0),
'constant', constant_values=np.nan)})
norm = errors.reshape(errors.shape[0] * errors.shape[1], X.shape[-1] * self.len_out)
scores = -multivariate_normal.logpdf(norm, mean=self.mean, cov=self.cov, allow_singular=True)
scores = np.pad(scores, (self.len_in + self.len_out - 1, 0), 'constant', constant_values=np.nan)
return scores
示例8: test_logpdf
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def test_logpdf(self):
# Check that the log of the pdf is in fact the logpdf
np.random.seed(1234)
x = np.random.randn(5)
mean = np.random.randn(5)
cov = np.abs(np.random.randn(5))
d1 = multivariate_normal.logpdf(x, mean, cov)
d2 = multivariate_normal.pdf(x, mean, cov)
assert_allclose(d1, np.log(d2))
示例9: test_logpdf_default_values
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def test_logpdf_default_values(self):
# Check that the log of the pdf is in fact the logpdf
# with default parameters Mean=None and cov = 1
np.random.seed(1234)
x = np.random.randn(5)
d1 = multivariate_normal.logpdf(x)
d2 = multivariate_normal.pdf(x)
# check whether default values are being used
d3 = multivariate_normal.logpdf(x, None, 1)
d4 = multivariate_normal.pdf(x, None, 1)
assert_allclose(d1, np.log(d2))
assert_allclose(d3, np.log(d4))
示例10: test_frozen
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def test_frozen(self):
# The frozen distribution should agree with the regular one
np.random.seed(1234)
x = np.random.randn(5)
mean = np.random.randn(5)
cov = np.abs(np.random.randn(5))
norm_frozen = multivariate_normal(mean, cov)
assert_allclose(norm_frozen.pdf(x), multivariate_normal.pdf(x, mean, cov))
assert_allclose(norm_frozen.logpdf(x),
multivariate_normal.logpdf(x, mean, cov))
assert_allclose(norm_frozen.cdf(x), multivariate_normal.cdf(x, mean, cov))
assert_allclose(norm_frozen.logcdf(x),
multivariate_normal.logcdf(x, mean, cov))
示例11: test_exception_singular_cov
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def test_exception_singular_cov(self):
np.random.seed(1234)
x = np.random.randn(5)
mean = np.random.randn(5)
cov = np.ones((5, 5))
e = np.linalg.LinAlgError
assert_raises(e, multivariate_normal, mean, cov)
assert_raises(e, multivariate_normal.pdf, x, mean, cov)
assert_raises(e, multivariate_normal.logpdf, x, mean, cov)
assert_raises(e, multivariate_normal.cdf, x, mean, cov)
assert_raises(e, multivariate_normal.logcdf, x, mean, cov)
示例12: test_numpy_rvs_shape_compatibility
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def test_numpy_rvs_shape_compatibility(self):
np.random.seed(2846)
alpha = np.array([1.0, 2.0, 3.0])
x = np.random.dirichlet(alpha, size=7)
assert_equal(x.shape, (7, 3))
assert_raises(ValueError, dirichlet.pdf, x, alpha)
assert_raises(ValueError, dirichlet.logpdf, x, alpha)
dirichlet.pdf(x.T, alpha)
dirichlet.pdf(x.T[:-1], alpha)
dirichlet.logpdf(x.T, alpha)
dirichlet.logpdf(x.T[:-1], alpha)
示例13: test_alpha_with_zeros
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def test_alpha_with_zeros(self):
np.random.seed(2846)
alpha = [1.0, 0.0, 3.0]
# don't pass invalid alpha to np.random.dirichlet
x = np.random.dirichlet(np.maximum(1e-9, alpha), size=7).T
assert_raises(ValueError, dirichlet.pdf, x, alpha)
assert_raises(ValueError, dirichlet.logpdf, x, alpha)
示例14: test_data_with_zeros
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def test_data_with_zeros(self):
alpha = np.array([1.0, 2.0, 3.0, 4.0])
x = np.array([0.1, 0.0, 0.2, 0.7])
dirichlet.pdf(x, alpha)
dirichlet.logpdf(x, alpha)
alpha = np.array([1.0, 1.0, 1.0, 1.0])
assert_almost_equal(dirichlet.pdf(x, alpha), 6)
assert_almost_equal(dirichlet.logpdf(x, alpha), np.log(6))
示例15: test_data_with_zeros_and_small_alpha
# 需要导入模块: from scipy.stats import multivariate_normal [as 别名]
# 或者: from scipy.stats.multivariate_normal import logpdf [as 别名]
def test_data_with_zeros_and_small_alpha(self):
alpha = np.array([1.0, 0.5, 3.0, 4.0])
x = np.array([0.1, 0.0, 0.2, 0.7])
assert_raises(ValueError, dirichlet.pdf, x, alpha)
assert_raises(ValueError, dirichlet.logpdf, x, alpha)