8000 BUG, API: np.random.multivariate_normal behavior with bad covariance matrix by cowlicks · Pull Request #5726 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG, API: np.random.multivariate_normal behavior with bad covariance matrix #5726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 4, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations 8000
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Documentation fix and proper handling of tolerance
  • Loading branch information
Oscar Villellas committed Jan 3, 2017
commit 6d7f14f60e12d200b02fd1f41d2315a5167cc859
25 changes: 15 additions & 10 deletions numpy/random/mtrand/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4356,9 +4356,9 @@ cdef class RandomState:

# Multivariate distributions:
def multivariate_normal(self, mean, cov, size=None, check_valid='warn',
tol=1e-8):
rtol=1e-05, atol=1e-8):
"""
multivariate_normal(mean, cov[, size])
multivariate_normal(mean, cov[, size, check_valid, rtol, atol])

Draw random samples from a multivariate normal distribution.

Expand All @@ -4381,10 +4381,14 @@ cdef class RandomState:
generated, and packed in an `m`-by-`n`-by-`k` arrangement. Because
each sample is `N`-dimensional, the output shape is ``(m,n,k,N)``.
If no shape is specified, a single (`N`-D) sample is returned.
check_valid : 'warn', 'raise', 'ignore'
Behavior when the covariance matrix is not Positive Semi-definite.
tol : float
Tolerance of the singular values in covariance matrix.
check_valid : { 'warn', 'raise', 'ignore' }, optional
Behavior when the covariance matrix is not positive semidefinite.
rtol : float, optional
Relative tolerance to use when checking the singular values in
covariance matrix.
atol : float, optional
Absolute tolerance to use when checking the singular values in
covariance matrix

Returns
-------
Expand Down Expand Up @@ -4500,15 +4504,16 @@ cdef class RandomState:
(u, s, v) = svd(cov)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be the entire next part under
if check_valid != 'ignore':

no reason to check if we 'ignore'

if check_valid != 'ignore':
psd = np.allclose(np.dot(v.T * s, v), cov)
if check_valid != 'warn' and check_valid != 'raise':
raise ValueError("check_valid must equal 'warn', 'raise', or 'ignore'")

psd = np.allclose(np.dot(v.T * s, v), cov, rtol=rtol, atol=atol)
if not psd:
if check_valid == 'warn':
warnings.warn("covariance is not positive-semidefinite.",
RuntimeWarning)
elif check_valid == 'raise':
raise ValueError("covariance is not positive-semidefinite.")
else:
raise ValueError("check_valid must equal 'warn', 'raise', or 'ignore'")
raise ValueError("covariance is not positive-semidefinite.")

x = np.dot(x, np.sqrt(s)[:, None] * v)
x += mean
Expand Down
0