Example #1
0
    def test_mcmc_with_other_prior(self):
        test_rel = relationship.Relationship(utils.straight_line, TEST_X,
                                             TEST_Y)
        test_rel.max_likelihood('mini')

        def other_prior():
            """
            Another potential prior.
            """
            priors = []
            for i, variable in enumerate(test_rel.variables):
                loc = variable.n
                scale = 1
                priors.append(norm(loc=loc, scale=scale))
            return priors

        actual_results = sampling.mcmc(test_rel,
                                       prior_function=other_prior,
                                       n_burn=10,
                                       n_samples=10)
        assert_equal(
            isinstance(actual_results["distributions"][0], Distribution), True)
        assert_equal(
            isinstance(actual_results["distributions"][1], Distribution), True)
        assert_equal(actual_results["distributions"][0].size, 500)
        assert_equal(actual_results["distributions"][1].size, 500)
Example #2
0
 def test_ln_likelihood(self):
     test_rel = relationship.Relationship(utils.straight_line, TEST_X,
                                          TEST_Y)
     expected_lnl = -3
     actual_lnl = optimize.ln_likelihood([1., 0.], test_rel.function,
                                         test_rel.x, test_rel.y)
     assert_almost_equal(actual_lnl, expected_lnl, decimal=0)
Example #3
0
 def test_nested_sampling(self):
     test_rel = relationship.Relationship(utils.straight_line,
                                          TEST_X,
                                          TEST_Y,
                                          bounds=((0, 10), (-1, 1)))
     actual_results = sampling.nested_sampling(test_rel, maxiter=100)
     assert_equal(isinstance(actual_results, dict), True)
     assert_equal(isinstance(actual_results["logz"][-1], float), True)
     assert_equal(isinstance(actual_results["logzerr"][-1], float), True)
Example #4
0
 def test_negative_lnl(self):
     test_rel = relationship.Relationship(utils.straight_line, TEST_X,
                                          TEST_Y)
     expected_negtive_lnl = 3
     actual_negative_lnl = optimize.negative_lnl([1., 0.],
                                                 test_rel.function,
                                                 test_rel.x, test_rel.y)
     assert_almost_equal(actual_negative_lnl,
                         expected_negtive_lnl,
                         decimal=0)
Example #5
0
 def test_mcmc_with_variable_median_zero(self):
     test_rel = relationship.Relationship(utils.straight_line, TEST_X,
                                          TEST_Y)
     test_rel.variables[0] = Distribution(np.zeros((7)))
     actual_results = sampling.mcmc(test_rel, n_burn=10, n_samples=10)
     assert_equal(
         isinstance(actual_results["distributions"][0], Distribution), True)
     assert_equal(
         isinstance(actual_results["distributions"][1], Distribution), True)
     assert_equal(actual_results["distributions"][0].size, 500)
     assert_equal(actual_results["distributions"][1].size, 500)
Example #6
0
 def test_mcmc(self):
     test_rel = relationship.Relationship(utils.straight_line, TEST_X,
                                          TEST_Y)
     test_rel.max_likelihood('mini')
     actual_results = sampling.mcmc(test_rel, n_burn=10, n_samples=10)
     assert_equal(
         isinstance(actual_results["distributions"][0], Distribution), True)
     assert_equal(
         isinstance(actual_results["distributions"][1], Distribution), True)
     assert_equal(actual_results["distributions"][0].size, 500)
     assert_equal(actual_results["distributions"][1].size, 500)
Example #7
0
    def test_nested_sampling_b_with_other_prior(self):
        test_rel = relationship.Relationship(utils.straight_line,
                                             TEST_X,
                                             TEST_Y,
                                             bounds=((0, 10), (-1, 1)))
        test_rel.max_likelihood('mini')

        def other_prior():
            """
            Another potential prior.
            """
            priors = []
            for i, variable in enumerate(test_rel.variables):
                loc = variable.n
                scale = 1
                priors.append(norm(loc=loc, scale=scale))
            return priors

        actual_results = sampling.nested_sampling(test_rel,
                                                  prior_function=other_prior,
                                                  maxiter=100)
        assert_equal(isinstance(actual_results, dict), True)
        assert_equal(isinstance(actual_results["logz"][-1], float), True)
        assert_equal(isinstance(actual_results["logzerr"][-1], float), True)
Example #8
0
 def test_max_lnlikelihood(self):
     test_rel = relationship.Relationship(utils.straight_line, TEST_X,
                                          TEST_Y)
     actual_best_variables = optimize.max_ln_likelihood(test_rel, 'mini')
     assert_almost_equal(actual_best_variables, np.array([1, 0]), decimal=0)