我们从Python开源项目中,提取了以下28个代码示例,用于说明如何使用numpy.linalg.slogdet()。
def do(self, a, b): d = linalg.det(a) (s, ld) = linalg.slogdet(a) if asarray(a).dtype.type in (single, double): ad = asarray(a).astype(double) else: ad = asarray(a).astype(cdouble) ev = linalg.eigvals(ad) assert_almost_equal(d, multiply.reduce(ev, axis=-1)) assert_almost_equal(s * np.exp(ld), multiply.reduce(ev, axis=-1)) s = np.atleast_1d(s) ld = np.atleast_1d(ld) m = (s != 0) assert_almost_equal(np.abs(s[m]), 1) assert_equal(ld[~m], -inf)
def log_marg_k(self, k): """ Return the log marginal probability of the data vectors assigned to component `k`. The log marginal probability p(X) = p(x_1, x_2, ..., x_N) is returned for the data vectors assigned to component `k`. See (266) in Murphy's bayesGauss notes, p. 21. """ k_N = self.prior.k_0 + self.counts[k] v_N = self.prior.v_0 + self.counts[k] m_N = self.m_N_numerators[k]/k_N S_N = self.S_N_partials[k] - k_N*np.outer(m_N, m_N) i = np.arange(1, self.D + 1, dtype=np.int) return ( - self.counts[k]*self.D/2.*self._cached_log_pi + self.D/2.*math.log(self.prior.k_0) - self.D/2.*math.log(k_N) + self.prior.v_0/2.*slogdet(self.prior.S_0)[1] - v_N/2.*slogdet(S_N)[1] + np.sum( self._cached_gammaln_by_2[v_N + 1 - i] - self._cached_gammaln_by_2[self.prior.v_0 + 1 - i] ) )
def test_zero(self): assert_equal(linalg.det([[0.0]]), 0.0) assert_equal(type(linalg.det([[0.0]])), double) assert_equal(linalg.det([[0.0j]]), 0.0) assert_equal(type(linalg.det([[0.0j]])), cdouble) assert_equal(linalg.slogdet([[0.0]]), (0.0, -inf)) assert_equal(type(linalg.slogdet([[0.0]])[0]), double) assert_equal(type(linalg.slogdet([[0.0]])[1]), double) assert_equal(linalg.slogdet([[0.0j]]), (0.0j, -inf)) assert_equal(type(linalg.slogdet([[0.0j]])[0]), cdouble) assert_equal(type(linalg.slogdet([[0.0j]])[1]), double)
def test_types(self): def check(dtype): x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype) assert_equal(np.linalg.det(x).dtype, dtype) ph, s = np.linalg.slogdet(x) assert_equal(s.dtype, get_real_dtype(dtype)) assert_equal(ph.dtype, dtype) for dtype in [single, double, csingle, cdouble]: yield check, dtype
def log_prior(self, i): """Return the probability of `X[i]` under the prior alone.""" mu = self.prior.m_0 covar = (self.prior.k_0 + 1) / (self.prior.k_0*(self.prior.v_0 - self.D + 1)) * self.prior.S_0 logdet_covar = slogdet(covar)[1] inv_covar = inv(covar) v = self.prior.v_0 - self.D + 1 return self._multivariate_students_t(i, mu, logdet_covar, inv_covar, v)
def _update_logdet_covar_and_inv_covar(self, k): """ Update the covariance terms for component `k`. Based on the `m_N_numerators` and `S_N_partials` terms for the `k`th component, the `logdet_covars` and `inv_covars` terms are updated. """ k_N = self.prior.k_0 + self.counts[k] v_N = self.prior.v_0 + self.counts[k] m_N = self.m_N_numerators[k]/k_N covar = (k_N + 1.)/(k_N*(v_N - self.D + 1.)) * (self.S_N_partials[k] - k_N*np.outer(m_N, m_N)) self.logdet_covars[k] = slogdet(covar)[1] self.inv_covars[k, :, :] = inv(covar) # @profile
def logpdf(x, df, mu, Sigma): """ Marginal log-likelihood of a Student-t Process Parameters ---------- x: array-like Point to be evaluated df: float Degrees of freedom (>2.0) mu: array-like Mean of the process. Sigma: array-like Covariance matrix of the process. Returns ------- logp: float log-likelihood """ d = len(x) x = np.atleast_2d(x) xm = x - mu V = df * Sigma V_inv = np.linalg.inv(V) _, logdet = slogdet(np.pi * V) logz = -gamma(df / 2.0 + d / 2.0) + gamma(df / 2.0) + 0.5 * logdet logp = -0.5 * (df + d) * np.log(1 + np.sum(np.dot(xm, V_inv) * xm, axis=1)) logp = logp - logz return logp[0]