-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42038de
commit 1843724
Showing
3 changed files
with
18 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# | ||
# author: Jungtaek Kim ([email protected]) | ||
# last updated: July 8, 2024 | ||
# last updated: July 9, 2024 | ||
# | ||
"""test_covariance""" | ||
|
||
|
@@ -12,9 +12,6 @@ | |
from bayeso.utils import utils_covariance | ||
|
||
|
||
TEST_EPSILON = 1e-7 | ||
|
||
|
||
def test_choose_fun_cov_typing(): | ||
annos = package_target.choose_fun_cov.__annotations__ | ||
|
||
|
@@ -258,7 +255,7 @@ def test_cov_se(): | |
cov_ = package_target.cov_se(X, Xp, cur_hyps["lengthscales"], cur_hyps["signal"]) | ||
print(cov_) | ||
truth_cov_ = 0.22313016014842987 | ||
np.testing.assert_allclose(cov_[0], truth_cov_) | ||
np.testing.assert_allclose(cov_[0, 0], truth_cov_) | ||
|
||
X = np.array([[1.0, 2.0, 0.0]]) | ||
Xp = np.array([[2.0, 1.0, 1.0], [0.0, 0.0, 0.0]]) | ||
|
@@ -363,7 +360,7 @@ def test_cov_matern32(): | |
) | ||
print(cov_) | ||
truth_cov_ = 0.19914827347145583 | ||
np.testing.assert_allclose(cov_[0], truth_cov_) | ||
np.testing.assert_allclose(cov_[0, 0], truth_cov_) | ||
|
||
X = np.array([[1.0, 2.0, 0.0]]) | ||
Xp = np.array([[2.0, 1.0, 1.0], [0.0, 0.0, 0.0]]) | ||
|
@@ -481,13 +478,7 @@ def test_cov_matern52(): | |
package_target.cov_matern52( | ||
np.zeros((1, 2)), np.zeros((1, 2)), np.array([1.0, 1.0]), 1 | ||
) | ||
assert ( | ||
np.abs( | ||
package_target.cov_matern52(np.zeros((1, 2)), np.zeros((1, 2)), 1.0, 0.1)[0] | ||
- 0.01 | ||
) | ||
< TEST_EPSILON | ||
) | ||
np.testing.assert_allclose(package_target.cov_matern52(np.zeros((1, 2)), np.zeros((1, 2)), 1.0, 0.1)[0, 0], 0.01) | ||
|
||
X = np.array([[1.0, 2.0, 0.0]]) | ||
Xp = np.array([[2.0, 1.0, 1.0]]) | ||
|
@@ -497,7 +488,7 @@ def test_cov_matern52(): | |
) | ||
print(cov_) | ||
truth_cov_ = 0.20532087608359792 | ||
assert np.abs(cov_[0] - truth_cov_) < TEST_EPSILON | ||
np.testing.assert_allclose(cov_[0, 0], truth_cov_) | ||
|
||
X = np.array([[1.0, 2.0, 0.0]]) | ||
Xp = np.array([[2.0, 1.0, 1.0], [0.0, 0.0, 0.0]]) | ||
|
@@ -508,7 +499,7 @@ def test_cov_matern52(): | |
) | ||
print(cov_) | ||
truth_cov_ = np.array([[0.20532088, 0.09657724]]) | ||
assert np.all(np.abs(cov_[0] - truth_cov_) < TEST_EPSILON) | ||
np.testing.assert_allclose(cov_, truth_cov_) | ||
|
||
|
||
def test_grad_cov_matern52_typing(): | ||
|
@@ -568,8 +559,7 @@ def test_grad_cov_matern52(): | |
[[0.0, 1.04798822, 0.57644039, 0.0], [0.02, 2.00002, 0.0, 0.0]], | ||
] | ||
) | ||
|
||
assert np.all(np.abs(truth_grad_cov_ - grad_cov_) < TEST_EPSILON) | ||
np.testing.assert_allclose(grad_cov_, truth_grad_cov_) | ||
|
||
num_hyps = X_train.shape[1] + 1 | ||
grad_cov_ = package_target.grad_cov_matern52( | ||
|
@@ -586,8 +576,7 @@ def test_grad_cov_matern52(): | |
[[1.04798822, 0.57644039, 0.0], [2.00002, 0.0, 0.0]], | ||
] | ||
) | ||
|
||
assert np.all(np.abs(truth_grad_cov_ - grad_cov_) < TEST_EPSILON) | ||
np.testing.assert_allclose(grad_cov_, truth_grad_cov_) | ||
|
||
|
||
def test_cov_set_typing(): | ||
|
@@ -653,19 +642,7 @@ def test_cov_set(): | |
np.array([1.0, 1.0, 1.0]), | ||
1, | ||
) | ||
assert ( | ||
np.abs( | ||
package_target.cov_set( | ||
str_cov, | ||
np.zeros((num_instances, num_dim)), | ||
np.zeros((num_instances, num_dim)), | ||
1.0, | ||
0.1, | ||
) | ||
- 0.01 | ||
) | ||
< TEST_EPSILON | ||
) | ||
np.testing.assert_allclose(package_target.cov_set(str_cov, np.zeros((num_instances, num_dim)), np.zeros((num_instances, num_dim)), 1.0, 0.1), 0.01) | ||
|
||
bx = np.array([[1.0, 2.0, 0.0], [2.0, 1.0, 0.0]]) | ||
bxp = np.array([[2.0, 1.0, 1.0], [2.0, 2.0, 2.0]]) | ||
|
@@ -675,7 +652,7 @@ def test_cov_set(): | |
) | ||
print(cov_) | ||
truth_cov_ = 0.23061736638896702 | ||
assert np.abs(cov_ - truth_cov_) < TEST_EPSILON | ||
np.testing.assert_allclose(cov_, truth_cov_) | ||
|
||
|
||
def test_cov_main_typing(): | ||
|
@@ -891,15 +868,15 @@ def test_grad_cov_main(): | |
|
||
print(grad_cov_) | ||
truth_grad_cov_ = np.array([[[2.00002, 0.0, 0.0]]]) | ||
assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) | ||
np.testing.assert_allclose(grad_cov_, truth_grad_cov_) | ||
|
||
grad_cov_ = package_target.grad_cov_main( | ||
"se", np.ones((1, 2)), np.ones((1, 2)), cur_hyps, False | ||
) | ||
|
||
print(grad_cov_) | ||
truth_grad_cov_ = np.array([[[0.02, 2.00002, 0.0, 0.0]]]) | ||
assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) | ||
np.testing.assert_allclose(grad_cov_, truth_grad_cov_) | ||
|
||
cur_hyps["lengthscales"] = 1.0 | ||
grad_cov_ = package_target.grad_cov_main( | ||
|
@@ -908,23 +885,23 @@ def test_grad_cov_main(): | |
|
||
print(grad_cov_) | ||
truth_grad_cov_ = np.array([[[0.02, 0.73577888, 0.73577888]]]) | ||
assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) | ||
np.testing.assert_allclose(grad_cov_, truth_grad_cov_) | ||
|
||
grad_cov_ = package_target.grad_cov_main( | ||
"matern32", np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, False | ||
) | ||
|
||
print(grad_cov_) | ||
truth_grad_cov_ = np.array([[[0.02, 0.59566154, 0.51802578]]]) | ||
assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) | ||
np.testing.assert_allclose(grad_cov_, truth_grad_cov_) | ||
|
||
grad_cov_ = package_target.grad_cov_main( | ||
"matern32", np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, True | ||
) | ||
|
||
print(grad_cov_) | ||
truth_grad_cov_ = np.array([[[0.59566154, 0.51802578]]]) | ||
assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) | ||
np.testing.assert_allclose(grad_cov_, truth_grad_cov_) | ||
|
||
grad_cov_ = package_target.grad_cov_main( | ||
"matern52", np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, False | ||
|
@@ -935,4 +912,4 @@ def test_grad_cov_main(): | |
print(np.squeeze(grad_cov_)[1]) | ||
print(np.squeeze(grad_cov_)[2]) | ||
truth_grad_cov_ = np.array([[[0.02, 0.6345867279080876, 0.5872865507000906]]]) | ||
assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) | ||
np.testing.assert_allclose(grad_cov_, truth_grad_cov_) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# | ||
# author: Jungtaek Kim ([email protected]) | ||
# last updated: October 13, 2021 | ||
# author: Jungtaek Kim ([email protected]) | ||
# last updated: July 9, 2024 | ||
# | ||
"""test_trees""" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters