예제 #1
0
    def test_wrong_number_of_noises(self):
        def me(prm):
            return np.array(prm - 10)

        # That should work:
        correct_noise = bayem.Gamma(1, 2)
        info = bayem.vba(me, bayem.MVN(7, 12), correct_noise)
        self.assertTrue(info.success)

        # Providing a wrong dimension in the noise pattern should fail.
        wrong_noise = bayem.Gamma((1, 1), (2, 2))
        with self.assertRaises(Exception):
            bayem.vba(me, bayem.MVN(7, 12), wrong_noise)
예제 #2
0
    def test_given_mu(self):
        """
        We infer the gamma distribution of the noise precision for a
        given, fixed parameter mu. This is done by setting a prior with
        a very high precision.

        For a super noninformative noise prior (shape=0, scale->inf), the
        analytic solution for the INVERSE gamma distribution for the noise
        VARIANCE reads
        """
        a = n / 2
        b = np.sum((data - prior_mean) ** 2) / 2

        # The parameters for the corresponding gamma distribution for the
        # PRECISION then read (a, 1/b)

        big_but_not_nan = 1e20
        prior = bayem.MVN(prior_mean, big_but_not_nan)
        gamma = bayem.Gamma(shape=1.0e-20, scale=big_but_not_nan)

        result = bayem.vba(model_error, prior, gamma, update_noise=True)
        self.assertTrue(result.success)

        gamma = result.noise
        self.assertAlmostEqual(gamma.shape, a)
        self.assertAlmostEqual(gamma.scale, 1 / b)
예제 #3
0
def test_custom_objects():
    data = {}
    data["parameter_prior"] = bayem.MVN(
        mean=np.r_[1, 2, 3],
        precision=np.diag([1, 2, 3]),
        parameter_names=["A", "B", "C"],
    )

    data["noise_prior"] = bayem.Gamma()
    data["non bayes thing"] = {"best number": 6174.0}

    string = bayem.save_json(data)
    loaded = bayem.load_json(string)

    A, B = data["parameter_prior"], loaded["parameter_prior"]
    CHECK = np.testing.assert_array_equal
    assert A.name == B.name
    assert A.parameter_names == B.parameter_names
    CHECK(A.mean, B.mean)
    CHECK(A.precision, B.precision)

    C, D = data["noise_prior"], loaded["noise_prior"]
    assert C.name == D.name
    assert C.shape == D.shape
    assert C.scale == D.scale
예제 #4
0
    def test_inconsistent_noise(self):
        def dict_model_error(numbers):
            return {"A": np.ones(5), "B": np.zeros(5)}

        param0 = bayem.MVN()
        noise0 = {"A": bayem.Gamma(1, 1), "B": bayem.Gamma(2, 2)}

        # You may provide a single update_noise flag
        result = bayem.vba(dict_model_error, param0, noise0, update_noise=False)
        self.assert_gamma_equal(result.noise["A"], noise0["A"])
        self.assert_gamma_equal(result.noise["B"], noise0["B"])

        # Alternatively, you can provide a dict containing _all_ noise keys
        result = bayem.vba(
            dict_model_error, param0, noise0, update_noise={"A": True, "B": False}
        )
        self.assert_not_gamma_equal(result.noise["A"], noise0["A"])
        self.assert_gamma_equal(result.noise["B"], noise0["B"])

        # There will be an error, if you forget one
        with self.assertRaises(KeyError):
            bayem.vba(dict_model_error, param0, noise0, update_noise={"A": True})
예제 #5
0
        test_img = imread(test_img_name)
        ref_img = imread(ref_img_name)

        assert np.linalg.norm(test_img - ref_img) == pytest.approx(0)


np.random.seed(6174)
t = np.linspace(1, 2, 10)
noise = np.random.normal(0, 0.42, len(t))


def f(x):
    return t * x[0]**2 - t * 9 + noise


info = bayem.vba(f, x0=bayem.MVN([2], [0.5]), noise0=bayem.Gamma(1, 2))


def test_pair_plot(generate_ref_img=False):
    visu.pair_plot(info, show=False)
    compare_plt("ref_pair_plot.png", generate_ref_img=generate_ref_img)


def test_trace_plot(generate_ref_img=False):
    visu.result_trace(info, show=False)
    compare_plt("ref_trace_plot.png", generate_ref_img=generate_ref_img)


if __name__ == "__main__":
    generate = True
    test_pair_plot(generate_ref_img=generate)
예제 #6
0
def test_print():
    print(bayem.Gamma(42, 2))
예제 #7
0
def test_from_mean_and_sd():
    gamma_ref = bayem.Gamma(6174, 42)

    gamma = bayem.Gamma.FromMeanStd(gamma_ref.mean, gamma_ref.std)
    assert gamma == gamma_ref
예제 #8
0
def test_sd():
    scale, shape = 42, 6174
    gamma = bayem.Gamma(shape=shape, scale=scale)

    assert gamma.mean == pytest.approx(shape * scale)
    assert gamma.std**2 == pytest.approx(shape * scale**2)  # variance