-
Notifications
You must be signed in to change notification settings - Fork 435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding heteroskedastic tests #1508
Merged
st--
merged 9 commits into
GPflow:st/heteroskedastic
from
joshuacoales-pio:joshua.coales/heteroskedastic-tests
Jun 18, 2020
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
33fa6cd
Adding some tests for heteroskedastic likelihood public methods
joshuacoales-pio ffc86b4
Fixing notebook typo
joshuacoales-pio 2903a38
Bit of neatness, moving test constants into a class
joshuacoales-pio 77909b6
Fixing a copypaste issue in conditional variance test
joshuacoales-pio c29f731
Skipping failing tests
joshuacoales-pio 4ae9781
Fixing skip annotation
joshuacoales-pio 61d39a5
Using g_var constant
joshuacoales-pio 3927efd
PR changes as suggested by st--
joshuacoales-pio 9f22008
Fixing formatting issues
joshuacoales-pio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
98 changes: 98 additions & 0 deletions
98
tests/gpflow/likelihoods/test_heteroskedastic_constant_variance.py
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright 2017-2020 the GPflow authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import numpy as np | ||
import pytest | ||
import tensorflow as tf | ||
import tensorflow_probability as tfp | ||
|
||
import gpflow | ||
from gpflow.likelihoods.heteroskedastic import HeteroskedasticTFPDistribution | ||
|
||
tf.random.set_seed(99012) | ||
|
||
|
||
class Data: | ||
g_var = 0.345 | ||
rng = np.random.RandomState(123) | ||
N = 5 | ||
Y = rng.randn(N, 1) | ||
# single "GP" (for the mean): | ||
f_mean = rng.randn(N, 1) | ||
f_var = rng.uniform(0.1, 1.0, (N, 1)) # must be positive | ||
equivalent_f2 = np.log(np.sqrt(g_var)) | ||
f2_mean = np.full((N, 1), equivalent_f2) | ||
f2_var = np.zeros((N, 1)) | ||
F2_mean = np.c_[f_mean, f2_mean] | ||
F2_var = np.c_[f_var, f2_var] | ||
|
||
|
||
def test_log_prob(): | ||
""" | ||
heteroskedastic likelihood where the variance parameter is always constant | ||
giving the same answers for variational_expectations, predict_mean_and_var, | ||
etc as the regular Gaussian likelihood | ||
""" | ||
l1 = gpflow.likelihoods.Gaussian(variance=Data.g_var) | ||
l2 = HeteroskedasticTFPDistribution(tfp.distributions.Normal) | ||
np.testing.assert_allclose( | ||
l1.log_prob(Data.f_mean, Data.Y), l2.log_prob(Data.F2_mean, Data.Y), | ||
) | ||
|
||
|
||
def test_variational_expectations(): | ||
# Create likelihoods | ||
l1 = gpflow.likelihoods.Gaussian(variance=Data.g_var) | ||
l2 = HeteroskedasticTFPDistribution(tfp.distributions.Normal) | ||
np.testing.assert_allclose( | ||
l1.variational_expectations(Data.f_mean, Data.f_var, Data.Y), | ||
l2.variational_expectations(Data.F2_mean, Data.F2_var, Data.Y), | ||
) | ||
|
||
|
||
def test_predict_mean_and_var(): | ||
l1 = gpflow.likelihoods.Gaussian(variance=Data.g_var) | ||
l2 = HeteroskedasticTFPDistribution(tfp.distributions.Normal) | ||
np.testing.assert_allclose( | ||
l1.predict_mean_and_var(Data.f_mean, Data.f_var), | ||
l2.predict_mean_and_var(Data.F2_mean, Data.F2_var), | ||
) | ||
|
||
|
||
@pytest.mark.skip("Conditional mean is not implemented in heteroskedastic likelihood") | ||
def test_conditional_mean(): | ||
l1 = gpflow.likelihoods.Gaussian(variance=Data.g_var) | ||
l2 = HeteroskedasticTFPDistribution(tfp.distributions.Normal) | ||
np.testing.assert_allclose( | ||
l1.conditional_mean(Data.f_mean), l2.conditional_mean(Data.F2_mean), | ||
) | ||
|
||
|
||
@pytest.mark.skip("Conditional variance is not implemented in heteroskedastic likelihood") | ||
def test_conditional_variance(): | ||
l1 = gpflow.likelihoods.Gaussian(variance=Data.g_var) | ||
l2 = HeteroskedasticTFPDistribution(tfp.distributions.Normal) | ||
np.testing.assert_allclose( | ||
l1.conditional_variance(Data.f_mean), l2.conditional_variance(Data.F2_mean), | ||
) | ||
|
||
|
||
@pytest.mark.skip("Currently broken as it returns the sum over outputs when given multiple outputs") | ||
def test_predict_log_density(): | ||
l1 = gpflow.likelihoods.Gaussian(variance=Data.g_var) | ||
l2 = HeteroskedasticTFPDistribution(tfp.distributions.Normal) | ||
np.testing.assert_allclose( | ||
l1.predict_log_density(Data.f_mean, Data.f_var, Data.Y), | ||
l2.predict_log_density(Data.F2_mean, Data.f2_var, Data.Y), | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to fix the code to make this test pass or merge it as is and I can change the code (and reactivate the test)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be easier for you to fix, than for me to fix. I would rather merge it as is, such that you can change the code and reactivate the test