Пример #1
0
def makegp(params):
    kernel_time = lgp.ExpQuad(scale=params['time_scale'], dim='time')
    kernel_label = lgp.ExpQuad(scale=label_scale, dim='label')
    gp = lgp.GP(kernel_time * kernel_label)
    gp.addx(x, 'data', deriv=(data_deriv, 'time'))
    gp.addx(np.array([(0, 0)], dtype=x.dtype), 'fixed_point')
    return gp
Пример #2
0
def test_incompatible_dtypes():
    gp = lgp.GP(lgp.ExpQuad())
    gp.addx(0, 0)
    with pytest.raises(TypeError):
        gp.addx(np.zeros(1, 'd,d'), 1)

    gp = lgp.GP(lgp.ExpQuad())
    gp.addx(np.zeros(1, 'd,d'), 0)
    # gp.addx(np.zeros(1, 'i,i'), 1) # succeeds only if numpy >= 1.23
    with pytest.raises(TypeError):
        gp.addx(np.zeros(1, 'd,d,d'), 2)
Пример #3
0
def test_none_key():
    gp = lgp.GP(lgp.ExpQuad())
    with pytest.raises(ValueError):
        gp.addx({None: 0})

    gp = lgp.GP(lgp.ExpQuad())
    gp.addx(0, 0)
    with pytest.raises(ValueError):
        gp.addtransf({0: 1}, None)

    gp = lgp.GP(lgp.ExpQuad())
    with pytest.raises(ValueError):
        gp.addcov({None: 1})
Пример #4
0
def test_kernelop():
    gp = lgp.GP()
    gp.addproc(lgp.ExpQuad(), 'a')
    f = lambda x: x
    gp.addproc(lgp.ExpQuad() * lgp.Rescaling(stdfun=f), 'b1')
    gp.addkernelop('rescale', f, 'b2', 'a')
    x = np.arange(20)
    gp.addx(x, 'x1', proc='b1')
    gp.addx(x, 'x2', proc='b2')
    prior = gp.prior(['x1', 'x2'], raw=True)
    util.assert_equal(prior['x1', 'x1'], prior['x2', 'x2'])
    util.assert_equal(prior['x2', 'x1'], prior['x1', 'x2'].T)
    util.assert_equal(prior['x1', 'x2'], np.zeros(2 * x.shape))
Пример #5
0
def test_key_already_used():
    gp = lgp.GP(lgp.ExpQuad())
    gp.addx(0, 0)
    with pytest.raises(KeyError):
        gp.addx(0, 0)

    gp = lgp.GP(lgp.ExpQuad())
    gp.addx(0, 0)
    with pytest.raises(KeyError):
        gp.addtransf({0: 1}, 0)

    gp = lgp.GP(lgp.ExpQuad())
    gp.addx(0, 0)
    with pytest.raises(KeyError):
        gp.addcov(1, 0)
Пример #6
0
def test_proclintransf_checks():
    gp = lgp.GP()
    gp.addproc(lgp.ExpQuad(), 0)
    gp.addproc(lgp.ExpQuad(), 1)
    with pytest.raises(KeyError):
        gp.addproclintransf(lambda f, g: lambda x: f(x) + g(x), [0, 1], 0)
    with pytest.raises(KeyError):
        gp.addproclintransf(lambda f, g: lambda x: f(x) + g(x), [0, 2], 2)
    with pytest.raises(RuntimeError):
        gp.addproclintransf(lambda f, g: lambda x: 1, [0, 1], 2, checklin=True)
    with pytest.raises(RuntimeError):
        gp.addproclintransf(lambda f, g: lambda x: 1 + f(x) + g(x), [0, 1],
                            2,
                            checklin=True)
    gp.addproclintransf(lambda f, g: lambda x: 1 + f(x) + g(x), [0, 1], 2)
Пример #7
0
def test_addcov_wrong_blocks():
    gp = lgp.GP(lgp.ExpQuad())
    with pytest.raises(ValueError):
        gp.addcov(np.zeros((1, 1, 1)), 0)
    with pytest.raises(ValueError):
        gp.addcov(np.zeros((1, 2, 2, 1)), 0)
    with pytest.raises(ValueError):
        gp.addcov(np.random.randn(10, 10), 0)
    with pytest.raises(KeyError):
        gp.addcov({
            (0, 0): 1,
            (0, 1): 0,
        })
    with pytest.raises(ValueError):
        gp.addcov({
            (0, 0): np.ones((2, 2)),
            (1, 1): np.ones((3, 3)),
            (0, 1): np.ones((3, 2)),
        })
    with pytest.raises(ValueError):
        gp.addcov({
            (0, 0): np.ones((2, 2)),
            (1, 1): np.ones((3, 3)),
            (0, 1): np.ones((2, 3)),
            (1, 0): np.zeros((3, 2)),
        })
Пример #8
0
def test_no_checksym():
    covs = []
    for checksym in [False, True]:
        gp = lgp.GP(lgp.ExpQuad(), checksym=checksym)
        gp.addx({'x': np.arange(20)})
        covs.append(gp.prior('x', raw=True))
    util.assert_equal(*covs)
Пример #9
0
def makegp(params):
    kernel = lgp.Cauchy(scale=params['time_scale'], dim='time', beta=2)
    kernel *= lgp.ExpQuad(scale=params['label_scale'], dim='label')
    gp = lgp.GP(kernel)
    x['time'] = jnp.array([time, time - params['delay']])
    gp.addx(x, 'A')
    return gp
Пример #10
0
def test_prior_raw_shape():
    gp = lgp.GP(lgp.ExpQuad())
    gp.addx(np.arange(20).reshape(2, 10), 'x')
    cov = gp.prior(raw=True)
    assert cov['x', 'x'].shape == (2, 10, 2, 10)

    cov = gp.prior('x', raw=True)
    assert cov.shape == (2, 10, 2, 10)
Пример #11
0
def test_addprocderiv():
    gp = lgp.GP(lgp.ExpQuad())
    gp.addprocderiv(1, 'a')
    x = np.arange(20)
    gp.addx(x, 0, proc='a')
    gp.addx(x, 1, deriv=1)
    prior = gp.prior(raw=True)
    util.assert_equal(prior[0, 0], prior[1, 1])
    util.assert_equal(prior[0, 0], prior[0, 1])
Пример #12
0
def test_default_kernel():
    gp = lgp.GP(lgp.ExpQuad())
    gp.addproc(key='a')
    x = np.arange(20)
    gp.addx(x, 'x1')
    gp.addx(x, 'x2', proc='a')
    prior = gp.prior(raw=True)
    util.assert_equal(prior['x1', 'x1'], prior['x2', 'x2'])
    util.assert_equal(prior['x1', 'x2'], np.zeros(2 * x.shape))
Пример #13
0
def test_nonsense_tensors():
    gp = lgp.GP(lgp.ExpQuad())
    gp.addx(0, 0)
    with pytest.raises(TypeError):
        gp.addtransf({0: 'a'}, 1)
    with pytest.raises(ValueError):
        gp.addtransf({0: np.inf}, 1)
    gp.addx([0, 1], 1)
    with pytest.raises(ValueError):
        gp.addtransf({1: [1, 2, 3]}, 2)
Пример #14
0
def test_lintransf_matmul():
    gp = lgp.GP(lgp.ExpQuad())
    x = np.arange(20)
    gp.addx(x, 0)
    m = np.random.randn(30, len(x))
    gp.addlintransf(lambda x: m @ x, [0], 1)
    prior = gp.prior([0, 1], raw=True)
    np.testing.assert_allclose(m @ prior[0, 0] @ m.T, prior[1, 1])
    np.testing.assert_allclose(m @ prior[0, 1], prior[1, 1])
    np.testing.assert_allclose(prior[1, 0] @ m.T, prior[1, 1])
Пример #15
0
def test_double_pred():
    n = 50
    gp = lgp.GP(lgp.ExpQuad())
    ax, bx = np.random.randn(2, n)
    gp.addx(ax, 'a')
    gp.addx(bx, 'b')
    m = np.random.randn(n, n)
    ay = gvar.gvar(np.random.randn(n), m.T @ m)
    m1, cov1 = gp.predfromdata({'a': ay}, 'b', raw=True)
    m2, cov2 = gp.predfromfit(gp.predfromdata({'a': ay}, ['a']), 'b', raw=True)
    assert_close(m2, m1)
    assert_close_cov(cov2, cov1, 1e-3, 1e-6)
Пример #16
0
def test_addprocxtransf():
    gp = lgp.GP()
    f = lambda x: x**2
    gp.addproc(lgp.ExpQuad(), 0)
    gp.addprocxtransf(f, 'a', 0)
    gp.addproclintransf(lambda g: lambda x: g(f(x)), [0], 'b')
    x = np.linspace(0, 4, 20)
    gp.addx(x, 0, proc='a')
    gp.addx(x, 1, proc='b')
    gp.addx(f(x), 2, proc=0)
    prior = gp.prior(raw=True)
    for i in range(3):
        for j in range(3):
            util.assert_equal(prior[0, 0], prior[i, j])