20
20
import warnings
21
21
22
22
from functools import singledispatch
23
- from typing import Any , Optional , Sequence , Tuple , Union , cast
23
+ from typing import Any , Iterable , Optional , Sequence , Tuple , Union , cast
24
24
25
25
import numpy as np
26
26
49
49
from pymc .util import _add_future_warning_tag
50
50
51
51
52
- def to_tuple (shape ) :
53
- """Convert ints , arrays, and Nones to tuples
52
+ def to_tuple (shape : Optional [ Union [ None , int , np . ndarray ]]) -> Tuple :
53
+ """Convert integers , arrays, and Nones to tuples.
54
54
55
55
Parameters
56
56
----------
57
- shape: None, int or array-like
58
- Represents the shape to convert to tuple.
57
+ shape : None, int, or array-like
58
+ Represents the shape to convert to a tuple.
59
59
60
60
Returns
61
61
-------
62
- If `shape` is None, returns an empty tuple. If it's an int, (shape,) is
63
- returned. If it is array-like, tuple(shape) is returned.
62
+ tuple
63
+ If `shape` is None, returns an empty tuple. If it's an int, (shape,) is
64
+ returned. If it is array-like, `tuple(shape)` is returned.
65
+
66
+ Examples
67
+ --------
68
+ >>> to_tuple(None)
69
+ ()
70
+ >>> to_tuple(5)
71
+ (5,)
72
+ >>> to_tuple([1, 2, 3])
73
+ (1, 2, 3)
64
74
"""
65
75
if shape is None :
66
76
return tuple ()
@@ -87,42 +97,45 @@ def _check_shape_type(shape):
87
97
return tuple (out )
88
98
89
99
90
- def broadcast_dist_samples_shape (shapes , size = None ):
91
- """Apply shape broadcasting to shape tuples but assuming that the shapes
92
- correspond to draws from random variables, with the `size` tuple possibly
93
- prepended to it. The `size` prepend is ignored to consider if the supplied
94
- `shapes` can broadcast or not. It is prepended to the resulting broadcasted
95
- `shapes`, if any of the shape tuples had the `size` prepend.
100
+ def broadcast_dist_samples_shape (shapes : Iterable [Tuple [int , ...]], size : Optional [int ] = None ) -> Tuple [int , ...]:
101
+ """Apply shape broadcasting to shape tuples for random variables.
96
102
97
103
Parameters
98
104
----------
99
- shapes: Iterable of tuples holding the distribution samples shapes
100
- size: None, int or tuple (optional)
101
- size of the sample set requested.
105
+ shapes : Iterable of tuples
106
+ Tuples holding the distribution samples shapes.
107
+ size : None, int, or tuple, optional
108
+ Size of the sample set requested.
102
109
103
110
Returns
104
111
-------
105
- tuple of the resulting shape
112
+ tuple
113
+ The resulting broadcasted shape.
106
114
107
115
Examples
108
116
--------
109
117
.. code-block:: python
118
+
110
119
size = 100
111
120
shape0 = (size,)
112
121
shape1 = (size, 5)
113
122
shape2 = (size, 4, 5)
114
123
out = broadcast_dist_samples_shape([shape0, shape1, shape2],
115
124
size=size)
116
125
assert out == (size, 4, 5)
126
+
117
127
.. code-block:: python
128
+
118
129
size = 100
119
130
shape0 = (size,)
120
131
shape1 = (5,)
121
132
shape2 = (4, 5)
122
133
out = broadcast_dist_samples_shape([shape0, shape1, shape2],
123
134
size=size)
124
135
assert out == (size, 4, 5)
136
+
125
137
.. code-block:: python
138
+
126
139
size = 100
127
140
shape0 = (1,)
128
141
shape1 = (5,)
@@ -291,7 +304,18 @@ def find_size(
291
304
292
305
293
306
def rv_size_is_none (size : Variable ) -> bool :
294
- """Check whether an rv size is None (ie., pt.Constant([]))"""
307
+ """Check whether the size of a random variable is None.
308
+
309
+ Parameters
310
+ ----------
311
+ size : Variable
312
+ The size variable to check.
313
+
314
+ Returns
315
+ -------
316
+ bool
317
+ True if the size is None (i.e., pt.Constant([])), False otherwise.
318
+ """
295
319
return size .type .shape == (0 ,) # type: ignore [attr-defined]
296
320
297
321
@@ -311,19 +335,21 @@ def change_dist_size(
311
335
312
336
Parameters
313
337
----------
314
- dist:
338
+ dist : TensorVariable
315
339
The old distribution to be resized.
316
- new_size:
340
+ new_size : Union[int, Tuple[int, ...]]
317
341
The new size of the distribution.
318
- expand: bool, optional
319
- If True, `new_size` is prepended to the existing distribution `size`, so that
320
- the final size is equal to (*new_size, *dist.size). Defaults to false.
342
+ expand : bool, optional
343
+ If True, `new_size` is prepended to the existing distribution `size`,
344
+ so that the final size is equal to (*new_size, *dist.size).
345
+ Defaults to False.
321
346
322
347
Returns
323
348
-------
324
- A new distribution variable that is equivalent to the original distribution with
325
- the new size. The new distribution will not reuse the old RandomState/Generator
326
- input, so it will be independent from the original distribution.
349
+ TensorVariable
350
+ A new distribution variable equivalent to the original distribution
351
+ with the new size. The new distribution will not reuse the old
352
+ RandomState/Generator input, making it independent from the original.
327
353
328
354
Examples
329
355
--------
0 commit comments