Beispiel #1
0
def test_kl_simple():
    # verified by hand
    # Dhat(P||Q) = \log m/(n-1) + d / n  \sum_{i=1}^n \log \nu_k(i)/rho_k(i)
    x = np.reshape([0., 1, 3], (3, 1))
    y = np.reshape([.2, 1.2, 3.2, 7.2], (4, 1))

    n = x.shape[0]
    m = y.shape[0]

    x_to_y = np.log(
        m /
        (n - 1)) + 1 / n * (np.log(1.2 / 3) + np.log(.8 / 2) + np.log(1.8 / 3))
    y_to_x = np.log(n / (m - 1)) + 1 / m * (np.log(.8 / 3) + np.log(1.2 / 2) +
                                            np.log(2.2 / 3) + np.log(6.2 / 6))

    # NOTE: clamping makes this test useless.
    x_to_y = max(x_to_y, 0)
    y_to_x = max(y_to_x, 0)

    res = estimate_divs(Features([x, y]), specs=['kl'], Ks=[2]).squeeze()

    assert res[0, 0] == 0
    assert res[1, 1] == 0
    assert np.allclose(res[1, 0], y_to_x), "{} vs {}".format(res[1, 0], y_to_x)
    assert np.allclose(res[0, 1], x_to_y), "{} vs {}".format(res[0, 1], x_to_y)
Beispiel #2
0
def check_div(feats, expected, specs, Ks, name, min_dist=None, **args):
    capturer = capture_output(True, True, merge=False)
    with capturer:
        ds = estimate_divs(feats,
                           specs=specs,
                           Ks=Ks,
                           min_dist=min_dist,
                           **args)

    argstr = ', '.join('{}={}'.format(k, v) for k, v in iteritems(args))

    for spec_i, spec in enumerate(specs):
        for K_i, K in enumerate(Ks):
            calc = ds[:, :, spec_i, K_i]
            exp = expected[:, :, spec_i, K_i]

            diff = np.abs(calc - exp)
            i, j = np.unravel_index(np.argmax(diff), calc.shape)
            msg = "bad results for {}:{}, K={}\n".format(name, spec, K) + \
                  "(max diff {} = |{} - {}| at {},{})".format(
                      diff[i, j], calc[i, j], exp[i, j], i, j)

            f = partial(assert_close, calc, exp, atol=5e-5, msg=msg)
            f.description = \
                "divs: {} - {}, K={} - {}".format(name, spec, K, argstr)
            yield f,
Beispiel #3
0
def test_js_simple():
    # verified by hand
    x = np.reshape([0, 1, 3], (3, 1))
    y = np.reshape([.2, 1.2, 3.2, 6.2], (4, 1))

    mix_ent = np.log(2) + np.log(3) + psi(2) \
        + (np.log(.2) + np.log(.8) + np.log(1.8) - psi(1) - 2*psi(2)) / 6 \
        + (np.log(.2) + np.log(2) + np.log(3.2) - psi(1) - 3*psi(2)) / 8
    x_ent = np.log(2) + (np.log(3) + np.log(2) + np.log(3)) / 3
    y_ent = np.log(3) + (np.log(3) + np.log(2) + np.log(3) + np.log(5)) / 4
    right_js = mix_ent - (x_ent + y_ent) / 2
    expected = np.array([[0, right_js], [right_js, 0]])
    # TODO: what about clamping???

    est = estimate_divs(Features([x, y]), specs=['js'], Ks=[2],
                        status_fn=None).squeeze()
    assert_close(est, expected, atol=5e-5, msg="JS estimate not as expected")
Beispiel #4
0
def test_js_simple():
    # verified by hand
    x = np.reshape([0, 1, 3], (3, 1))
    y = np.reshape([.2, 1.2, 3.2, 6.2], (4, 1))

    mix_ent = np.log(2) + np.log(3) + psi(2) \
        + (np.log(.2) + np.log(.8) + np.log(1.8) - psi(1) - 2*psi(2)) / 6 \
        + (np.log(.2) + np.log(2) + np.log(3.2) - psi(1) - 3*psi(2)) / 8
    x_ent = np.log(2) + (np.log(3) + np.log(2) + np.log(3)) / 3
    y_ent = np.log(3) + (np.log(3) + np.log(2) + np.log(3) + np.log(5)) / 4
    right_js = mix_ent - (x_ent + y_ent) / 2
    expected = np.array([[0, right_js], [right_js, 0]])
    # TODO: what about clamping???

    est = estimate_divs(Features([x, y]), specs=['js'], Ks=[2],
                        status_fn=None).squeeze()
    assert_close(est, expected, atol=5e-5, msg="JS estimate not as expected")
Beispiel #5
0
def check_div(feats, expected, specs, Ks, name, min_dist=None, **args):
    capturer = capture_output(True, True, merge=False)
    with capturer:
        ds = estimate_divs(feats, specs=specs, Ks=Ks, min_dist=min_dist, **args)

    argstr = ', '.join('{}={}'.format(k, v) for k, v in iteritems(args))

    for spec_i, spec in enumerate(specs):
        for K_i, K in enumerate(Ks):
            calc = ds[:, :, spec_i, K_i]
            exp = expected[:, :, spec_i, K_i]

            diff = np.abs(calc - exp)
            i, j = np.unravel_index(np.argmax(diff), calc.shape)
            msg = "bad results for {}:{}, K={}\n".format(name, spec, K) + \
                  "(max diff {} = |{} - {}| at {},{})".format(
                      diff[i, j], calc[i, j], exp[i, j], i, j)

            f = partial(assert_close, calc, exp, atol=5e-5, msg=msg)
            f.description = \
                "divs: {} - {}, K={} - {}".format(name, spec, K, argstr)
            yield f,
Beispiel #6
0
def test_kl_simple():
    # verified by hand
    # Dhat(P||Q) = \log m/(n-1) + d / n  \sum_{i=1}^n \log \nu_k(i)/rho_k(i)
    x = np.reshape([0., 1, 3], (3, 1))
    y = np.reshape([.2, 1.2, 3.2, 7.2], (4, 1))

    n = x.shape[0]
    m = y.shape[0]

    x_to_y = np.log(m / (n-1)) + 1/n * (
        np.log(1.2 / 3) + np.log(.8 / 2) + np.log(1.8 / 3))
    y_to_x = np.log(n / (m-1)) + 1/m * (
        np.log(.8 / 3) + np.log(1.2 / 2) + np.log(2.2 / 3) + np.log(6.2 / 6))

    # NOTE: clamping makes this test useless.
    x_to_y = max(x_to_y, 0)
    y_to_x = max(y_to_x, 0)

    res = estimate_divs(Features([x, y]), specs=['kl'], Ks=[2]).squeeze()

    assert res[0, 0] == 0
    assert res[1, 1] == 0
    assert np.allclose(res[1, 0], y_to_x), "{} vs {}".format(res[1, 0], y_to_x)
    assert np.allclose(res[0, 1], x_to_y), "{} vs {}".format(res[0, 1], x_to_y)