def log_likelihood(x, mu, Sigma): x = np.atleast_2d(x) mu = np.atleast_2d(mu) Sigma = np.atleast_2d(Sigma) sign, Slogdet = sl.slogdet(Sigma) Sinv = sl.inv(Sigma) return -.5 * x.shape[1] * np.log(2. * np.pi) - .5 * Slogdet - .5 * ( (x - mu).T) * Sinv * (x - mu)
def log_prior(mu, Sigma, nu0=None, kappa0=None, Sigma0=None, mu0=None, N=1000): nu0 = N + 1 kappa0 = 1 Sigma0 = np.ones((mu.shape[0], mu.shape[0])) mu0 = np.ones((mu.shape[0], 1)) sign, Slogdet = sl.slogdet(Sigma) Sinv = sl.inv(Sigma) return -.5 (nu0 + mu.shape[0] + 2.) * Slogdet - .5 * kappa0 * ( mu - mu0) ^ T * Sinv * (mu - mu0) - .5 * np.trace(Sinv * Sigma0)
def compute_d(M): """Compute the average rate of change of matrix M, where M is the matrix for either a discrete or continuous Markov Chain.""" if isP(M): return -0.25*(linalg.slogdet(M)[1]) elif isQ(M): return -0.25*mean(diagonal(M)) else: raise MatCalcExcep("compute_d given a matrix that was not a correct probability or rate matrix")
def loglikelihood(self, z: np.ndarray, ekfstate: GaussParams, sensor_state: Dict[str, Any] = None) -> float: """Calculate the log likelihood of ekfstate at z in sensor_state""" # we need this function in IMM, PDA and IMM-PDA exercises # not necessary for tuning in EKF exercise v, S = self.innovation(z, ekfstate, sensor_state=sensor_state) ll = -0.5 * (self.NIS(z, ekfstate) + la.slogdet(S)) return ll
def test_works_with_underflow_case(self): for np_dtype, atol in [(np.float32, 0.05), (np.float64, 1e-5), (np.complex64, 0.05), (np.complex128, 1e-5)]: with self.subTest(np_dtype=np_dtype, atol=atol): matrix = (np.eye(20) * 1e-6).astype(np_dtype) sign_np, log_abs_det_np = np.linalg.slogdet(matrix) with self.session(): sign_tf, log_abs_det_tf = linalg.slogdet(matrix) self.assertAllClose(log_abs_det_np, self.evaluate(log_abs_det_tf), atol=atol) self.assertAllClose(sign_np, self.evaluate(sign_tf), atol=atol)
def test_works_with_five_different_random_pos_def_matrices(self): for n in range(1, 6): for np_dtype, atol in [(np.float32, 0.05), (np.float64, 1e-5), (np.complex64, 0.05), (np.complex128, 1e-5)]: with self.subTest(n=n, np_dtype=np_dtype, atol=atol): matrix = _RandomPDMatrix(n, self.rng, np_dtype) sign_np, log_abs_det_np = np.linalg.slogdet(matrix) with self.session(): sign_tf, log_abs_det_tf = linalg.slogdet(matrix) self.assertAllClose(log_abs_det_np, self.evaluate(log_abs_det_tf), atol=atol) self.assertAllClose(sign_np, self.evaluate(sign_tf), atol=atol)
def getDeterminant(K): f, logDet = linalg.slogdet(K) return f * exp(logDet)