class MHMultivariateLognormalTargetMock (MetropolisHastings): def __init__ (self, theta): super ().__init__ (theta, verbose=False) n = theta.get_size () mu = np.ones (n) Sigma = np.eye (n) / 10 self.target_distribution = MultivariateLognormal (mu, Sigma) def _calc_log_likelihood (self, t): t_values = t.get_values () l = self.target_distribution.pdf (t_values) return np.log (l) def _create_jump_dist (self, theta_t): n = theta_t.get_size () S = np.eye (n) / 100 variances = S.diagonal () theta_values = np.array (theta_t.get_values ()) mu = np.log (theta_values) - variances / 2 return MultivariateLognormal (mu, S) def _calc_mh_ratio (self, new_t, new_l, old_t, old_l): J_gv_new = self._create_jump_dist (new_t) J_gv_old = self._create_jump_dist (old_t) p_old_gv_new = J_gv_new.pdf (old_t.get_values ()) p_new_gv_old = J_gv_old.pdf (new_t.get_values ()) l_ratio = np.exp (new_l - old_l) r = (l_ratio) * (p_old_gv_new / p_new_gv_old) return r
def test_get_pdf_of_zero_prob_point (self): """ Tests if we can get the pdf of a point with pdf equal to zero. """ mu = [0, 0, 0] S = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] X = MultivariateLognormal (mu, S) x = [1, 0, 1] analytic = 0 assert (abs (X.pdf (x) - analytic) < 1e-4)
def test_get_pdf_of_small_prob_point (self): """ Tests if we can get the pdf of a point with small pdf value. """ mu = [0, 0, 0] S = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] X = MultivariateLognormal (mu, S) x = [1e-230, 1e-120, 1e-130] analytic = 0 assert (abs (X.pdf (x) - analytic) < 1e-4)
def test_get_pdf (self): """ Tests if we can get a point of the probability density function of this random variable. """ mu = [0, 0, 0] S = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] X = MultivariateLognormal (mu, S) x = [1, 1, 1] analytic = 1 / np.sqrt ((2 * np.pi) ** 3 ) assert (abs (X.pdf (x) - analytic) < 1e-4)