[go: up one dir, main page]

Skip to content

Commit

Permalink
suggested change to correct small eigenvalues in covariance
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedeltalima committed Nov 22, 2019
1 parent 8170c84 commit a7cfee2
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/qinfer/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,12 @@ def particle_covariance_mtx(cls, weights, locations):
# positive-semidefinite covariance matrix. If a negative eigenvalue
# is produced, we should warn the caller of this.
assert np.all(np.isfinite(cov))
if not np.all(la.eig(cov)[0] >= 0):
vals, vecs = la.eig(cov)
small_vals = abs(vals) < 1e-12
if np.any(small_vals):
vals[small_vals] = 0
warnings.warn('Numerical error in covariance estimation causing positive semidefinite violation.', ApproximationWarning)

return (vecs * vals) @ vecs.T.conj()
return cov

def est_mean(self):
Expand Down Expand Up @@ -1007,12 +1010,12 @@ def n_rvs(self):

def sample(self, n=1):
return self.dist.rvs(size=n)[:, np.newaxis]

class DirichletDistribution(Distribution):
r"""
The dirichlet distribution, whose pdf at :math:`x` is proportional to
:math:`\prod_i x_i^{\alpha_i-1}`.
:param alpha: The list of concentration parameters.
"""
def __init__(self, alpha):
Expand All @@ -1021,7 +1024,7 @@ def __init__(self, alpha):
raise ValueError('The input alpha must be a 1D list of concentration parameters.')

self._dist = st.dirichlet(alpha=self.alpha)

@property
def alpha(self):
return self._alpha
Expand Down

0 comments on commit a7cfee2

Please sign in to comment.