Пример #1
0
 def df_dtheta():
   k, dk = cov.scale(theta[0], cov.se(theta[1])).df_theta(X, Y)
   assert_allclose(k[0][0], f())
   assert s2*dk[0] == k, 's2*dk[0]=%r k=%r' % (s2*dk[0], k)
   if x == y:
     assert all(np.all(dki == 0) for dki in dk[1:]), 'dk=%r' % (dk,)
   else:
     assert all(np.all(dki != 0) for dki in dk[1:])
   h, dh = cov.se(theta[1]).df_theta(X, Y)
   assert k == s2*h, 'k=%r s2*h=%r' % (k, s2*h)
   assert all(dki == s2*dhi for dki, dhi in zip(dk[1:], dh))
   return dk
Пример #2
0
def testTwoSamples_low_covariance(seed):
    np_rng = npr.RandomState(seed)
    obs_inputs = np.array([1.3, -2.0, 0.0])
    obs_outputs = np.array([5.0, 2.3, 8.0])
    in_lo_cov = np.array([1.4, -20.0])
    sigma = 2.1
    l = 1.8
    observations = OrderedDict(zip(obs_inputs, obs_outputs))

    mean = gp.mean_const(0.)
    covariance = cov.scale(sigma**2, cov.se(l**2))

    # Fix n = 200, not higher even if we are doing a long-running
    # inference quality test, because we're trying to show that a
    # Pearson r^2 test of independence will fail to reject the null
    # hypothesis that the GP at two points with low covariance are
    # simply independent.  If we raised the number of samples
    # significantly higher, the test is likely to reject the null
    # hypothesis.
    n = 200
    lo_cov_x = []
    lo_cov_y = []
    for i in range(n):
        x, y = gp._gp_sample(mean, covariance, observations, in_lo_cov, np_rng)
        lo_cov_x.append(x)
        lo_cov_y.append(y)
    return reportPearsonIndependence(lo_cov_x, lo_cov_y)
Пример #3
0
def testOneSample(seed):
    np_rng = npr.RandomState(seed)
    obs_inputs = np.array([1.3, -2.0, 0.0])
    obs_outputs = np.array([5.0, 2.3, 8.0])
    test_input = 1.4
    expect_mu = 4.6307
    expect_sig = 0.0027
    sigma = 2.1
    l = 1.8
    observations = OrderedDict(zip(obs_inputs, obs_outputs))

    mean = gp.mean_const(0.)
    covariance = cov.scale(sigma**2, cov.se(l**2))

    # _gp_sample(..., test_input) should be normally distributed with
    # mean expect_mu.
    n = default_num_samples(4)

    def sample():
        s = gp._gp_sample(mean, covariance, observations, [test_input], np_rng)
        return s[0]

    samples = np.array([sample() for _ in xrange(n)])
    assert samples.shape == (n, )
    return reportKnownGaussian(expect_mu, np.sqrt(expect_sig), samples)
Пример #4
0
 def df_dtheta():
   k, dk = cov.se(theta[0]).df_theta(X, Y)
   assert_allclose(k[0][0], f())
   if x == y:
     assert all(np.all(dki == 0) for dki in dk)
   else:
     assert all(np.all(dki != 0) for dki in dk)
   return dk
Пример #5
0
def testNormalParameters():
    obs_inputs = np.array([1.3, -2.0, 0.0])
    obs_outputs = np.array([5.0, 2.3, 8.0])
    test_inputs = np.array([1.4, -3.2])
    expect_mu = np.array([4.6307, -0.9046])
    expect_sig = np.array([[0.0027, -0.0231], [-0.0231, 1.1090]])
    sigma = 2.1
    l = 1.8
    observations = OrderedDict(zip(obs_inputs, obs_outputs))

    mean = gp.mean_const(0.)
    covariance = cov.scale(sigma**2, cov.se(l**2))
    actual_mu, actual_sig = gp._gp_mvnormal(mean, covariance, observations,
                                            test_inputs)
    np.testing.assert_almost_equal(actual_mu, expect_mu, decimal=4)
    np.testing.assert_almost_equal(actual_sig, expect_sig, decimal=4)
Пример #6
0
def test_se():
  for x in [-1, 0, 1]:
    X = np.array([x])
    for y in [-1, 0, 1]:
      Y = np.array([y])
      for l2 in [.01, 1, 10, 100]:
        check_ddx(cov.se(l2), x, Y)
        theta = [l2]
        def f():
          return cov.se(theta[0]).f(X, Y)[0][0]
        def df_dtheta():
          k, dk = cov.se(theta[0]).df_theta(X, Y)
          assert_allclose(k[0][0], f())
          if x == y:
            assert all(np.all(dki == 0) for dki in dk)
          else:
            assert all(np.all(dki != 0) for dki in dk)
          return dk
        check_ddtheta(f, df_dtheta, theta)
Пример #7
0
def interpret_covariance_kernel(ast):
    if ast[0]['value'] == 'gp_cov_bump':
        min_tolerance = ast[1]['value']
        max_tolerance = ast[2]['value']
        return covariance.bump(min_tolerance, max_tolerance)
    elif ast[0]['value'] == 'gp_cov_scale':
        s2 = ast[1]['value']
        K = interpret_covariance_kernel(ast[2])
        return covariance.scale(s2, K)
    elif ast[0]['value'] == 'gp_cov_wn':
        s2 = ast[1]['value']
        K = covariance.bump(1e-9, 1e-11)
        return covariance.scale(s2, K)
    elif ast[0]['value'] == 'gp_cov_const':
        c = ast[1]['value']
        return covariance.const(c)
    elif ast[0]['value'] == 'gp_cov_linear':
        c = ast[1]['value']
        return covariance.linear(c)
    elif ast[0]['value'] == 'gp_cov_se':
        l2 = ast[1]['value']
        return covariance.se(l2)
    elif ast[0]['value'] == 'gp_cov_periodic':
        l2 = ast[1]['value']
        T = ast[2]['value']
        return covariance.periodic(l2, T)
    elif ast[0]['value'] == 'gp_cov_sum':
        K = interpret_covariance_kernel(ast[1])
        H = interpret_covariance_kernel(ast[2])
        return covariance.sum(K, H)
    elif ast[0]['value'] == 'gp_cov_product':
        K = interpret_covariance_kernel(ast[1])
        H = interpret_covariance_kernel(ast[2])
        return covariance.product(K, H)
    elif ast[0]['value'] == 'gp_cov_cp':
        location = ast[1]['value']
        scale = ast[2]['value']
        K = interpret_covariance_kernel(ast[3])
        H = interpret_covariance_kernel(ast[4])
        return change_point(location, scale, K, H)
    else:
        assert False, 'Failed to interpret AST'
Пример #8
0
def test_sum_se_bump():
  for x in [-1, 0, 1]:
    X = np.array([x])
    for y in [-1, 0, 1]:
      Y = np.array([y])
      for l2 in [.1, 1, 10]:
        for mint in [.4, 1.4, 2.4]:
          for maxt in [.6, 1.6, 2.6]:
            if maxt < mint:
              continue
            check_ddx(cov.sum(cov.se(l2), cov.bump(mint, maxt)), x, Y)
            theta = [l2, mint, maxt]
            def f():
              K = cov.se(theta[0])
              H = cov.bump(theta[1], theta[2])
              return cov.sum(K, H).f(X, Y)[0][0]
            def df_dtheta():
              K = cov.se(theta[0])
              H = cov.bump(theta[1], theta[2])
              k, dk = cov.sum(K, H).df_theta(X, Y)
              assert_allclose(k[0][0], f())
              return dk
            check_ddtheta(f, df_dtheta, theta)
Пример #9
0
def test_se_scaled():
  for x in [-1, 0, 1]:
    X = np.array([x])
    for y in [-1, 0, 1]:
      Y = np.array([y])
      for s2 in [.1, 1, 10, 100]:
        for l2 in [.1, 1, 10]:
          check_ddx(cov.scale(s2, cov.se(l2)), x, Y)
          theta = [s2, l2]
          def f():
            return cov.scale(theta[0], cov.se(theta[1])).f(X, Y)[0][0]
          def df_dtheta():
            k, dk = cov.scale(theta[0], cov.se(theta[1])).df_theta(X, Y)
            assert_allclose(k[0][0], f())
            assert s2*dk[0] == k, 's2*dk[0]=%r k=%r' % (s2*dk[0], k)
            if x == y:
              assert all(np.all(dki == 0) for dki in dk[1:]), 'dk=%r' % (dk,)
            else:
              assert all(np.all(dki != 0) for dki in dk[1:])
            h, dh = cov.se(theta[1]).df_theta(X, Y)
            assert k == s2*h, 'k=%r s2*h=%r' % (k, s2*h)
            assert all(dki == s2*dhi for dki, dhi in zip(dk[1:], dh))
            return dk
          check_ddtheta(f, df_dtheta, theta)
Пример #10
0
 def df_dtheta():
   K = cov.se(theta[0])
   H = cov.bump(theta[1], theta[2])
   k, dk = cov.sum(K, H).df_theta(X, Y)
   assert_allclose(k[0][0], f())
   return dk
Пример #11
0
 def f():
   K = cov.se(theta[0])
   H = cov.bump(theta[1], theta[2])
   return cov.sum(K, H).f(X, Y)[0][0]
Пример #12
0
 def f():
   return cov.scale(theta[0], cov.se(theta[1])).f(X, Y)[0][0]
Пример #13
0
 def f():
   return cov.se(theta[0]).f(X, Y)[0][0]