-
Notifications
You must be signed in to change notification settings - Fork 54
/
likelihoods.py
276 lines (218 loc) · 9.02 KB
/
likelihoods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# 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 abc
import beartype.typing as tp
from flax import nnx
from jax import vmap
import jax.numpy as jnp
import jax.scipy as jsp
from jaxtyping import Float
import tensorflow_probability.substrates.jax as tfp
from gpjax.distributions import GaussianDistribution
from gpjax.integrators import (
AbstractIntegrator,
AnalyticalGaussianIntegrator,
GHQuadratureIntegrator,
)
from gpjax.parameters import (
PositiveReal,
Static,
)
from gpjax.typing import (
Array,
ScalarFloat,
)
tfb = tfp.bijectors
tfd = tfp.distributions
class AbstractLikelihood(nnx.Module):
r"""Abstract base class for likelihoods.
All likelihoods must inherit from this class and implement the `predict` and
`link_function` methods.
"""
def __init__(
self,
num_datapoints: int,
integrator: AbstractIntegrator = GHQuadratureIntegrator(),
):
"""Initializes the likelihood.
Args:
num_datapoints (int): the number of data points.
integrator (AbstractIntegrator): The integrator to be used for computing expected log
likelihoods. Must be an instance of `AbstractIntegrator`.
"""
self.num_datapoints = num_datapoints
self.integrator = integrator
def __call__(self, *args: tp.Any, **kwargs: tp.Any) -> tfd.Distribution:
r"""Evaluate the likelihood function at a given predictive distribution.
Args:
*args (Any): Arguments to be passed to the likelihood's `predict` method.
**kwargs (Any): Keyword arguments to be passed to the likelihood's
`predict` method.
Returns:
The predictive distribution.
"""
return self.predict(*args, **kwargs)
@abc.abstractmethod
def predict(self, *args: tp.Any, **kwargs: tp.Any) -> tfd.Distribution:
r"""Evaluate the likelihood function at a given predictive distribution.
Args:
*args (Any): Arguments to be passed to the likelihood's `predict` method.
**kwargs (Any): Keyword arguments to be passed to the likelihood's
`predict` method.
Returns:
tfd.Distribution: The predictive distribution.
"""
raise NotImplementedError
@abc.abstractmethod
def link_function(self, f: Float[Array, "..."]) -> tfd.Distribution:
r"""Return the link function of the likelihood function.
Args:
f (Float[Array, "..."]): the latent Gaussian process values.
Returns:
tfd.Distribution: The distribution of observations, y, given values of the
Gaussian process, f.
"""
raise NotImplementedError
def expected_log_likelihood(
self,
y: Float[Array, "N D"],
mean: Float[Array, "N D"],
variance: Float[Array, "N D"],
) -> Float[Array, " N"]:
r"""Compute the expected log likelihood.
For a variational distribution $q(f)\sim\mathcal{N}(m, s)$ and a likelihood
$p(y|f)$, compute the expected log likelihood:
```math
\mathbb{E}_{q(f)}\left[\log p(y|f)\right]
```
Args:
y (Float[Array, 'N D']): The observed response variable.
mean (Float[Array, 'N D']): The variational mean.
variance (Float[Array, 'N D']): The variational variance.
Returns:
ScalarFloat: The expected log likelihood.
"""
log_prob = vmap(lambda f, y: self.link_function(f).log_prob(y))
return self.integrator(
fun=log_prob, y=y, mean=mean, variance=variance, likelihood=self
)
class Gaussian(AbstractLikelihood):
r"""Gaussian likelihood object."""
def __init__(
self,
num_datapoints: int,
obs_stddev: tp.Union[
ScalarFloat, Float[Array, "#N"], PositiveReal, Static
] = 1.0,
integrator: AbstractIntegrator = AnalyticalGaussianIntegrator(),
):
r"""Initializes the Gaussian likelihood.
Args:
num_datapoints (int): the number of data points.
obs_stddev (Union[ScalarFloat, Float[Array, "#N"]]): the standard deviation
of the Gaussian observation noise.
integrator (AbstractIntegrator): The integrator to be used for computing expected log
likelihoods. Must be an instance of `AbstractIntegrator`. For the Gaussian likelihood, this defaults to
the `AnalyticalGaussianIntegrator`, as the expected log likelihood can be computed analytically.
"""
if not isinstance(obs_stddev, (PositiveReal, Static)):
obs_stddev = PositiveReal(jnp.asarray(obs_stddev))
self.obs_stddev = obs_stddev
super().__init__(num_datapoints, integrator)
def link_function(self, f: Float[Array, "..."]) -> tfd.Normal:
r"""The link function of the Gaussian likelihood.
Args:
f (Float[Array, "..."]): Function values.
Returns:
tfd.Normal: The likelihood function.
"""
return tfd.Normal(loc=f, scale=self.obs_stddev.value.astype(f.dtype))
def predict(
self, dist: tp.Union[tfd.MultivariateNormalTriL, GaussianDistribution]
) -> tfd.MultivariateNormalFullCovariance:
r"""Evaluate the Gaussian likelihood.
Evaluate the Gaussian likelihood function at a given predictive
distribution. Computationally, this is equivalent to summing the
observation noise term to the diagonal elements of the predictive
distribution's covariance matrix.
Args:
dist (tfd.Distribution): The Gaussian process posterior,
evaluated at a finite set of test points.
Returns:
tfd.Distribution: The predictive distribution.
"""
n_data = dist.event_shape[0]
cov = dist.covariance()
noisy_cov = cov.at[jnp.diag_indices(n_data)].add(self.obs_stddev.value**2)
return tfd.MultivariateNormalFullCovariance(dist.mean(), noisy_cov)
class Bernoulli(AbstractLikelihood):
def link_function(self, f: Float[Array, "..."]) -> tfd.Distribution:
r"""The probit link function of the Bernoulli likelihood.
Args:
f (Float[Array, "..."]): Function values.
Returns:
tfd.Distribution: The likelihood function.
"""
return tfd.Bernoulli(probs=inv_probit(f))
def predict(self, dist: tfd.Distribution) -> tfd.Distribution:
r"""Evaluate the pointwise predictive distribution.
Evaluate the pointwise predictive distribution, given a Gaussian
process posterior and likelihood parameters.
Args:
dist (tfd.Distribution): The Gaussian process posterior, evaluated
at a finite set of test points.
Returns:
tfd.Distribution: The pointwise predictive distribution.
"""
variance = jnp.diag(dist.covariance())
mean = dist.mean().ravel()
return self.link_function(mean / jnp.sqrt(1.0 + variance))
class Poisson(AbstractLikelihood):
def link_function(self, f: Float[Array, "..."]) -> tfd.Distribution:
r"""The link function of the Poisson likelihood.
Args:
f (Float[Array, "..."]): Function values.
Returns:
tfd.Distribution: The likelihood function.
"""
return tfd.Poisson(rate=jnp.exp(f))
def predict(self, dist: tfd.Distribution) -> tfd.Distribution:
r"""Evaluate the pointwise predictive distribution.
Evaluate the pointwise predictive distribution, given a Gaussian
process posterior and likelihood parameters.
Args:
dist (tfd.Distribution): The Gaussian process posterior, evaluated
at a finite set of test points.
Returns:
tfd.Distribution: The pointwise predictive distribution.
"""
return self.link_function(dist.mean())
def inv_probit(x: Float[Array, " *N"]) -> Float[Array, " *N"]:
r"""Compute the inverse probit function.
Args:
x (Float[Array, "*N"]): A vector of values.
Returns
-------
Float[Array, "*N"]: The inverse probit of the input vector.
"""
jitter = 1e-3 # To ensure output is in interval (0, 1).
return 0.5 * (1.0 + jsp.special.erf(x / jnp.sqrt(2.0))) * (1 - 2 * jitter) + jitter
NonGaussian = tp.Union[Poisson, Bernoulli]
__all__ = [
"AbstractLikelihood",
"NonGaussian",
"Gaussian",
"Bernoulli",
"Poisson",
"inv_probit",
]