forked from pymc-devs/pymc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistribution.py
444 lines (355 loc) · 14.5 KB
/
distribution.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# Copyright 2020 The PyMC Developers
#
# 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 contextvars
import inspect
import multiprocessing
import sys
import types
import warnings
from abc import ABCMeta
from typing import TYPE_CHECKING
import dill
from aesara.tensor.random.op import RandomVariable
from aesara.tensor.random.var import RandomStateSharedVariable
from pymc3.distributions import _logcdf, _logp
if TYPE_CHECKING:
from typing import Optional, Callable
import aesara
import aesara.graph.basic
import aesara.tensor as at
from pymc3.util import UNSET, get_repr_for_variable
from pymc3.vartypes import string_types
__all__ = [
"DensityDist",
"Distribution",
"Continuous",
"Discrete",
"NoDistribution",
]
vectorized_ppc = contextvars.ContextVar(
"vectorized_ppc", default=None
) # type: contextvars.ContextVar[Optional[Callable]]
PLATFORM = sys.platform
class _Unpickling:
pass
class DistributionMeta(ABCMeta):
def __new__(cls, name, bases, clsdict):
# Forcefully deprecate old v3 `Distribution`s
if "random" in clsdict:
def _random(*args, **kwargs):
warnings.warn(
"The old `Distribution.random` interface is deprecated.",
DeprecationWarning,
stacklevel=2,
)
return clsdict["random"](*args, **kwargs)
clsdict["random"] = _random
rv_op = clsdict.setdefault("rv_op", None)
rv_type = None
if isinstance(rv_op, RandomVariable):
rv_type = type(rv_op)
new_cls = super().__new__(cls, name, bases, clsdict)
if rv_type is not None:
# Create dispatch functions
class_logp = clsdict.get("logp")
if class_logp:
@_logp.register(rv_type)
def logp(op, var, rvs_to_values, *dist_params, **kwargs):
value_var = rvs_to_values.get(var, var)
return class_logp(value_var, *dist_params, **kwargs)
class_logcdf = clsdict.get("logcdf")
if class_logcdf:
@_logcdf.register(rv_type)
def logcdf(op, var, rvs_to_values, *dist_params, **kwargs):
value_var = rvs_to_values.get(var, var)
return class_logcdf(value_var, *dist_params, **kwargs)
# Register the Aesara `RandomVariable` type as a subclass of this
# `Distribution` type.
new_cls.register(rv_type)
return new_cls
class Distribution(metaclass=DistributionMeta):
"""Statistical distribution"""
rv_class = None
rv_op = None
def __new__(cls, name, *args, **kwargs):
try:
from pymc3.model import Model
model = Model.get_context()
except TypeError:
raise TypeError(
"No model on context stack, which is needed to "
"instantiate distributions. Add variable inside "
"a 'with model:' block, or use the '.dist' syntax "
"for a standalone distribution."
)
rng = kwargs.pop("rng", None)
if rng is None:
rng = model.next_rng()
if not isinstance(name, string_types):
raise TypeError(f"Name needs to be a string but got: {name}")
data = kwargs.pop("observed", None)
total_size = kwargs.pop("total_size", None)
dims = kwargs.pop("dims", None)
if "shape" in kwargs:
raise DeprecationWarning("The `shape` keyword is deprecated; use `size`.")
testval = kwargs.pop("testval", None)
if testval is not None:
warnings.warn(
"The `testval` argument is deprecated; use `initval`.",
DeprecationWarning,
stacklevel=2,
)
initval = kwargs.pop("initval", testval)
transform = kwargs.pop("transform", UNSET)
rv_out = cls.dist(*args, rng=rng, **kwargs)
if testval is not None:
rv_out.tag.test_value = testval
return model.register_rv(
rv_out, name, data, total_size, dims=dims, transform=transform, initval=initval
)
@classmethod
def dist(cls, dist_params, rng=None, **kwargs):
testval = kwargs.pop("testval", None)
if testval is not None:
warnings.warn(
"The `testval` argument is deprecated. "
"Use `initval` to set initial values for a `Model`; "
"otherwise, set test values on Aesara parameters explicitly "
"when attempting to use Aesara's test value debugging features.",
DeprecationWarning,
stacklevel=2,
)
rv_var = cls.rv_op(*dist_params, rng=rng, **kwargs)
if (
rv_var.owner
and isinstance(rv_var.owner.op, RandomVariable)
and isinstance(rng, RandomStateSharedVariable)
and not getattr(rng, "default_update", None)
):
# This tells `aesara.function` that the shared RNG variable
# is mutable, which--in turn--tells the `FunctionGraph`
# `Supervisor` feature to allow in-place updates on the variable.
# Without it, the `RandomVariable`s could not be optimized to allow
# in-place RNG updates, forcing all sample results from compiled
# functions to be the same on repeated evaluations.
new_rng = rv_var.owner.outputs[0]
rv_var.update = (rng, new_rng)
rng.default_update = new_rng
return rv_var
def _distr_parameters_for_repr(self):
"""Return the names of the parameters for this distribution (e.g. "mu"
and "sigma" for Normal). Used in generating string (and LaTeX etc.)
representations of Distribution objects. By default based on inspection
of __init__, but can be overwritten if necessary (e.g. to avoid including
"sd" and "tau").
"""
return inspect.getfullargspec(self.__init__).args[1:]
def _distr_name_for_repr(self):
return self.__class__.__name__
def _str_repr(self, name=None, dist=None, formatting="plain"):
"""
Generate string representation for this distribution, optionally
including LaTeX markup (formatting='latex').
Parameters
----------
name : str
name of the distribution
dist : Distribution
the distribution object
formatting : str
one of { "latex", "plain", "latex_with_params", "plain_with_params" }
"""
if dist is None:
dist = self
if name is None:
name = "[unnamed]"
supported_formattings = {"latex", "plain", "latex_with_params", "plain_with_params"}
if not formatting in supported_formattings:
raise ValueError(f"Unsupported formatting ''. Choose one of {supported_formattings}.")
param_names = self._distr_parameters_for_repr()
param_values = [
get_repr_for_variable(getattr(dist, x), formatting=formatting) for x in param_names
]
if "latex" in formatting:
param_string = ",~".join(
[fr"\mathit{{{name}}}={value}" for name, value in zip(param_names, param_values)]
)
if formatting == "latex_with_params":
return r"$\text{{{var_name}}} \sim \text{{{distr_name}}}({params})$".format(
var_name=name, distr_name=dist._distr_name_for_repr(), params=param_string
)
return r"$\text{{{var_name}}} \sim \text{{{distr_name}}}$".format(
var_name=name, distr_name=dist._distr_name_for_repr()
)
else:
# one of the plain formattings
param_string = ", ".join(
[f"{name}={value}" for name, value in zip(param_names, param_values)]
)
if formatting == "plain_with_params":
return f"{name} ~ {dist._distr_name_for_repr()}({param_string})"
return f"{name} ~ {dist._distr_name_for_repr()}"
def __str__(self, **kwargs):
try:
return self._str_repr(formatting="plain", **kwargs)
except:
return super().__str__()
def _repr_latex_(self, *, formatting="latex_with_params", **kwargs):
"""Magic method name for IPython to use for LaTeX formatting."""
return self._str_repr(formatting=formatting, **kwargs)
__latex__ = _repr_latex_
class NoDistribution(Distribution):
def __init__(
self,
shape,
dtype,
initval=None,
defaults=(),
parent_dist=None,
*args,
**kwargs,
):
super().__init__(
shape=shape, dtype=dtype, initval=initval, defaults=defaults, *args, **kwargs
)
self.parent_dist = parent_dist
def __getattr__(self, name):
# Do not use __getstate__ and __setstate__ from parent_dist
# to avoid infinite recursion during unpickling
if name.startswith("__"):
raise AttributeError("'NoDistribution' has no attribute '%s'" % name)
return getattr(self.parent_dist, name)
def logp(self, x):
"""Calculate log probability.
Parameters
----------
x: numeric
Value for which log-probability is calculated.
Returns
-------
TensorVariable
"""
return at.zeros_like(x)
def _distr_parameters_for_repr(self):
return []
class Discrete(Distribution):
"""Base class for discrete distributions"""
def __new__(cls, name, *args, **kwargs):
if kwargs.get("transform", None):
raise ValueError("Transformations for discrete distributions")
return super().__new__(cls, name, *args, **kwargs)
class Continuous(Distribution):
"""Base class for continuous distributions"""
class DensityDist(Distribution):
"""Distribution based on a given log density function.
A distribution with the passed log density function is created.
Requires a custom random function passed as kwarg `random` to
enable prior or posterior predictive sampling.
"""
def __init__(
self,
logp,
shape=(),
dtype=None,
initval=0,
random=None,
wrap_random_with_dist_shape=True,
check_shape_in_random=True,
*args,
**kwargs,
):
"""
Parameters
----------
logp: callable
A callable that has the following signature ``logp(value)`` and
returns an Aesara tensor that represents the distribution's log
probability density.
shape: tuple (Optional): defaults to `()`
The shape of the distribution. The default value indicates a scalar.
If the distribution is *not* scalar-valued, the programmer should pass
a value here.
dtype: None, str (Optional)
The dtype of the distribution.
initval: number or array (Optional)
The ``initval`` of the RV's tensor that follow the ``DensityDist``
distribution.
args, kwargs: (Optional)
These are passed to the parent class' ``__init__``.
Examples
--------
.. code-block:: python
with pm.Model():
mu = pm.Normal('mu',0,1)
normal_dist = pm.Normal.dist(mu, 1)
pm.DensityDist(
'density_dist',
normal_dist.logp,
observed=np.random.randn(100),
)
trace = pm.sample(100)
.. code-block:: python
with pm.Model():
mu = pm.Normal('mu', 0 , 1)
normal_dist = pm.Normal.dist(mu, 1, shape=3)
dens = pm.DensityDist(
'density_dist',
normal_dist.logp,
observed=np.random.randn(100, 3),
shape=3,
)
prior = pm.sample_prior_predictive(10)['density_dist']
assert prior.shape == (10, 100, 3)
"""
if dtype is None:
dtype = aesara.config.floatX
super().__init__(shape, dtype, initval, *args, **kwargs)
self.logp = logp
if type(self.logp) == types.MethodType:
if PLATFORM != "linux":
warnings.warn(
"You are passing a bound method as logp for DensityDist, this can lead to "
"errors when sampling on platforms other than Linux. Consider using a "
"plain function instead, or subclass Distribution."
)
elif type(multiprocessing.get_context()) != multiprocessing.context.ForkContext:
warnings.warn(
"You are passing a bound method as logp for DensityDist, this can lead to "
"errors when sampling when multiprocessing cannot rely on forking. Consider using a "
"plain function instead, or subclass Distribution."
)
self.rand = random
self.wrap_random_with_dist_shape = wrap_random_with_dist_shape
self.check_shape_in_random = check_shape_in_random
def __getstate__(self):
# We use dill to serialize the logp function, as this is almost
# always defined in the notebook and won't be pickled correctly.
# Fix https://github.com/pymc-devs/pymc3/issues/3844
try:
logp = dill.dumps(self.logp)
except RecursionError as err:
if type(self.logp) == types.MethodType:
raise ValueError(
"logp for DensityDist is a bound method, leading to RecursionError while serializing"
) from err
else:
raise err
vals = self.__dict__.copy()
vals["logp"] = logp
return vals
def __setstate__(self, vals):
vals["logp"] = dill.loads(vals["logp"])
self.__dict__ = vals
def _distr_parameters_for_repr(self):
return []