def testNormalParameters(): r''' Training data: :math:`\mathbf{x} =[1.3, {-2}, 0]^T` :math:`\mathbf{y} =[5, 2.3, 8.2]^T` Unseen data: :math:`\mathbf{x}^* =[1.4, {-3.2} ]^T` We provide a squared-exponential covariance matrix with :math:`\sigma = 2.1` and :math:`\ell=1.8`. ''' x = np.array([[1.3,-2,0]]).T z = np.array([[5,2.3,8]]).T y = 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 = collections.OrderedDict([(x[i][0], z[i][0])for i in range(x.shape[0])]) gp_class = gp.GP(lambda x:0,covs.squared_exponential(sigma,l),observations,True) actual_mu,actual_sig = gp_class.getNormal(y) np.testing.assert_almost_equal(actual_mu, expect_mu, decimal=4) np.testing.assert_almost_equal(actual_sig, expect_sig, decimal=4)
def testTwoSamples(): r''' Training data: :math:`\mathbf{x} =[1.3, {-2}, 0]^T` :math:`\mathbf{y} =[5, 2.3, 8]^T` Unseen data with low covariance: :math:`\mathbf{x}^* =[1.4,-20]` Unseen data with high covariance: :math:`\mathbf{x}^* =[1.4,1.5]` We provide a squared-exponential covariance matrix with :math:`\sigma = 2.1` and :math:`\ell=1.8`. We compute Pearson's r and test for significance to check whether covariance is high with two datapoints where we expect it and where we do not expect. ''' x = np.array([[1.3,-2,0]]).T z = np.array([[5,2.3,8]]).T y_low_cov =[1.4,-20] y_high_cov = [1.4,1.5] sigma = 2.1 l = 1.8 observations = collections.OrderedDict([(x[i][0], z[i][0])for i in range(x.shape[0])]) gp_class = gp.GP(lambda x:0,covs.squared_exponential(sigma,l),observations,True) n = 200 # number of samples drawn low_cov_x = [] low_cov_y = [] high_cov_x = [] high_cov_y = [] high_cov_samples = [] for i in range(n): x,y=gp_class.sample(y_low_cov) low_cov_x.append(x) low_cov_y.append(y) high_cov_samples.append(gp_class.sample(y_high_cov)) x,y=gp_class.sample(y_high_cov) high_cov_x.append(x) high_cov_y.append(y) high_cov_samples.append(gp_class.sample(y_high_cov)) (test_result, p_value)=stats.pearsonr(high_cov_x,high_cov_y) assert(p_value<0.05) (test_result, p_value)=stats.pearsonr(low_cov_x,low_cov_y) assert(p_value>=0.05)
def test_squaredExponential(): r''' Tests the squared-exponential covariance SE :math:`= \sigma^2 \exp{-\frac{(x-x^\prime)^2}{2\ell^2}}` ''' l = 1.8 sigma = 2.1 f = covs.squared_exponential(sigma,l) cov_train,cov_test = apply_cov(f) expect_cov_train = np.array([[4.4100, 0.8215, 3.3976],[ 0.8215,4.4100, 2.3788],[ 3.3976, 2.3788,4.4100]]) expect_cov_test = np.array([[4.4100, 0.1938],[ 0.8215, 3.5313],[ 3.3976, 0.9081]]) np.testing.assert_almost_equal(cov_train, expect_cov_train, decimal=4) np.testing.assert_almost_equal(cov_test, expect_cov_test, decimal=4)
def testOneSample(): r''' Training data: :math:`\mathbf{x} =[1.3, {-2}, 0]^T` :math:`\mathbf{y} =[5, 2.3, 8.2]^T` Unseen data: :math:`x^* =1.4` We provide a squared-exponential covariance matrix with :math:`\sigma = 2.1` and :math:`\ell=1.8`. We use a one-sample t-test to test if the samples are not different from a Gaussian with the mean that was analytically computed by hand. We also add some sanity checks in the end. Future applications of this smoke test may reveal it to be too conservative. ''' x = np.array([[1.3,-2,0]]).T z = np.array([[5,2.3,8]]).T y = 1.4 expect_mu= np.array([[4.6307]]) sigma = 2.1 l = 1.8 observations = collections.OrderedDict([(x[i][0], z[i][0])for i in range(x.shape[0])]) gp_class = gp.GP(lambda x:0,covs.squared_exponential(sigma,l),observations,True) n = 200 # number of samples drawn samples =[] for i in range(n): samples.append(gp_class.sample(y)) (test_result, p_value)=stats.ttest_1samp(samples,expect_mu) assert(p_value>=0.05) (test_result, p_value)=stats.ttest_1samp(samples,0) assert(p_value<0.05) (test_result, p_value)=stats.ttest_1samp(np.random.uniform(0,1,n),expect_mu) assert(p_value<0.05) (test_result, p_value)=stats.ttest_1samp(np.random.normal(10,1,n),n) # sanity check assert(p_value<0.05)