def test_plugable():
    """ Tests for the pluggable form """
    d1 = Distribution("AB", [0.5, 0.5])
    d2 = Distribution("BC", [0.5, 0.5])
    f = jensen_divergence(renyi_entropy)
    val1 = f([d1, d2], order=1)
    val2 = f([d1, d2], [0.5, 0.5], 1)
    val3 = JSD([d1, d2])
    assert val1 == pytest.approx(val2)
    assert val1 == pytest.approx(val3)
    assert val2 == pytest.approx(val3)
def test_jsd5():
    """ Test that JSD fails when more weights than dists are given """
    d1 = Distribution("AB", [0.5, 0.5])
    d2 = Distribution("BC", [0.5, 0.5])
    with pytest.raises(ditException):
        JSD([d1, d2], [0.1, 0.6, 0.3])
def test_jsd4():
    """ Test the JSD with half-overlapping distributions with weights """
    d1 = Distribution("AB", [0.5, 0.5])
    d2 = Distribution("BC", [0.5, 0.5])
    jsd = JSD([d1, d2], [0.25, 0.75])
    assert jsd == pytest.approx(0.40563906222956625)
def test_jsd3():
    """ Test the JSD with disjoint distributions """
    d1 = Distribution("AB", [0.5, 0.5])
    d2 = Distribution("CD", [0.5, 0.5])
    jsd = JSD([d1, d2])
    assert jsd == pytest.approx(1.0)
def test_jsd2():
    """ Test the JSD with half-overlapping distributions """
    d1 = Distribution("AB", [0.5, 0.5])
    d2 = Distribution("BC", [0.5, 0.5])
    jsd = JSD([d1, d2])
    assert jsd == pytest.approx(0.5)
def test_jsd1():
    """ Test the JSD of a distribution with itself """
    d1 = Distribution("AB", [0.5, 0.5])
    jsd = JSD([d1, d1])
    assert jsd == pytest.approx(0)
def test_jsd0():
    """ Test the JSD of a distribution but with weights misspecified."""
    d1 = Distribution("AB", [0.5, 0.5])
    with pytest.raises(ditException):
        JSD(d1, d1)
def test_jsd3():
    """ Test the JSD with disjoint distributions """
    d1 = Distribution("AB", [0.5, 0.5])
    d2 = Distribution("CD", [0.5, 0.5])
    jsd = JSD([d1, d2])
    assert_almost_equal(jsd, 1.0)
def test_jsd2():
    """ Test the JSD with half-overlapping distributions """
    d1 = Distribution("AB", [0.5, 0.5])
    d2 = Distribution("BC", [0.5, 0.5])
    jsd = JSD([d1, d2])
    assert_almost_equal(jsd, 0.5)
def test_jsd1():
    """ Test the JSD of a distribution with itself """
    d1 = Distribution("AB", [0.5, 0.5])
    jsd = JSD([d1, d1])
    assert_almost_equal(jsd, 0)