Skip to content

Commit 13b766f

Browse files
committed
BUG, API: Add bypass for covariance matirx checking.
In np.random.multivariate_normal allow a bypass of the positive semi-definitness (PSD) check of the covariance matrix with a keyword flag. Also raise a warning if the cov mat is not symmetric. This can be bypassed with a kw flag too.
1 parent a4cd474 commit 13b766f

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

‎numpy/random/mtrand/mtrand.pyx‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,7 +4210,8 @@ cdef class RandomState:
42104210
return discd_array(self.internal_state, rk_logseries, size, op, self.lock)
42114211

42124212
# Multivariate distributions:
4213-
def multivariate_normal(self, mean, cov, size=None):
4213+
def multivariate_normal(self, mean, cov, size=None, cov_psd=True,
4214+
cov_sym=True):
42144215
"""
42154216
multivariate_normal(mean, cov[, size])
42164217
@@ -4235,6 +4236,13 @@ cdef class RandomState:
42354236
generated, and packed in an `m`-by-`n`-by-`k` arrangement. Because
42364237
each sample is `N`-dimensional, the output shape is ``(m,n,k,N)``.
42374238
If no shape is specified, a single (`N`-D) sample is returned.
4239+
cov_psd : bool, optional
4240+
Check if the covariance matrix is positive semi-definite (PSD).
4241+
True by default. If True and the covariance matrix is not PSD an
4242+
error is raised.
4243+
cov_sym : bool, optional
4244+
Check if the covariance matrix is symmetric. True by default. If
4245+
True and the covariance matrix is not symmetric an error is raised.
42384246
42394247
Returns
42404248
-------
@@ -4320,11 +4328,11 @@ cdef class RandomState:
43204328
shape = size
43214329

43224330
if len(mean.shape) != 1:
4323-
raise ValueError("mean must be 1 dimensional")
4331+
raise ValueError("mean must be 1 dimensional")
43244332
if (len(cov.shape) != 2) or (cov.shape[0] != cov.shape[1]):
4325-
raise ValueError("cov must be 2 dimensional and square")
4333+
raise ValueError("cov must be 2 dimensional and square")
43264334
if mean.shape[0] != cov.shape[0]:
4327-
raise ValueError("mean and cov must have same length")
4335+
raise ValueError("mean and cov must have same length")
43284336

43294337
# Compute shape of output and create a matrix of independent
43304338
# standard normally distributed random numbers. The matrix has rows
@@ -4347,9 +4355,14 @@ cdef class RandomState:
43474355
# not zero. We continue to use the SVD rather than Cholesky in
43484356
# order to preserve current outputs. Note that symmetry has not
43494357
# been checked.
4358+
4359+
if cov_sym and not np.array_equal(cov, cov.T):
4360+
warnings.warn("covariance is not positive-semidefinite.",
4361+
RuntimeWarning)
4362+
43504363
(u, s, v) = svd(cov)
43514364
neg = (np.sum(u.T * v, axis=1) < 0) & (s > 0)
4352-
if np.any(neg):
4365+
if cov_psd and np.any(neg):
43534366
s[neg] = 0.
43544367
warnings.warn("covariance is not positive-semidefinite.",
43554368
RuntimeWarning)

0 commit comments

Comments
 (0)