-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
ENH: add exponentiation of a covariance function with a scalar #3852
Merged
twiecki
merged 3 commits into
pymc-devs:master
from
tirthasheshpatel:gp/cov/exponentiation
Apr 6, 2020
Merged
Changes from 2 commits
Commits
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
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 |
---|---|---|
|
@@ -13,9 +13,11 @@ | |
# limitations under the License. | ||
|
||
import numpy as np | ||
import theano | ||
import theano.tensor as tt | ||
from functools import reduce | ||
from operator import mul, add | ||
from numbers import Number | ||
|
||
__all__ = [ | ||
"Constant", | ||
|
@@ -34,6 +36,7 @@ | |
"Coregion", | ||
"ScaledCov", | ||
"Kron", | ||
"Exponentiated", | ||
] | ||
|
||
|
||
|
@@ -100,6 +103,22 @@ def __radd__(self, other): | |
def __rmul__(self, other): | ||
return self.__mul__(other) | ||
|
||
def __pow__(self, other): | ||
if( | ||
isinstance(other, theano.compile.SharedVariable) and | ||
other.get_value().squeeze().shape == () | ||
): | ||
other = tt.squeeze(other) | ||
return Exponentiated(self, other) | ||
elif isinstance(other, Number): | ||
return Exponentiated(self, other) | ||
elif np.asarray(other).squeeze().shape == (): | ||
other = np.squeeze(other) | ||
return Exponentiated(self, other) | ||
|
||
raise ValueError("A covariance function can only be exponentiated by a scalar value") | ||
|
||
|
||
def __array_wrap__(self, result): | ||
""" | ||
Required to allow radd/rmul by numpy arrays. | ||
|
@@ -117,6 +136,19 @@ def __array_wrap__(self, result): | |
raise RuntimeError | ||
|
||
|
||
class Exponentiated(Covariance): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe move this down to be near |
||
def __init__(self, kernel, power): | ||
self.kernel = kernel | ||
self.power = power | ||
super().__init__( | ||
input_dim=self.kernel.input_dim, | ||
active_dims=self.kernel.active_dims | ||
) | ||
|
||
def __call__(self, X, Xs=None, diag=False): | ||
return self.kernel(X, Xs, diag=diag) ** self.power | ||
|
||
|
||
class Combination(Covariance): | ||
def __init__(self, factor_list): | ||
input_dim = max( | ||
|
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
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.
Since
Exponentiated
isn't necessarily user-facing because you added__pow__
, it probably shouldn't be included in__all__
.