• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pymc-devs / pymc3 / 6228

pending completion
6228

push

travis-ci

springcoil
DOCS - Adding docs explaining the behaviour of logp

12075 of 15569 relevant lines covered (77.56%)

1.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.34
/pymc3/distributions/distribution.py
1
import numbers
2✔
2
import numpy as np
2✔
3
import theano.tensor as tt
2✔
4
from theano import function
2✔
5
import theano
2✔
6
from ..memoize import memoize
2✔
7
from ..model import Model, get_named_nodes, FreeRV, ObservedRV
2✔
8
from ..vartypes import string_types
2✔
9

10
__all__ = ['DensityDist', 'Distribution', 'Continuous', 'Discrete',
2✔
11
           'NoDistribution', 'TensorType', 'draw_values']
12

13

14
class _Unpickling(object):
2✔
15
    pass
2✔
16

17

18
class Distribution(object):
2✔
19
    """Statistical distribution"""
20
    def __new__(cls, name, *args, **kwargs):
2✔
21
        if name is _Unpickling:
2✔
22
            return object.__new__(cls)  # for pickle
2✔
23
        try:
2✔
24
            model = Model.get_context()
2✔
25
        except TypeError:
×
26
            raise TypeError("No model on context stack, which is needed to "
×
27
                            "instantiate distributions. Add variable inside "
28
                            "a 'with model:' block, or use the '.dist' syntax "
29
                            "for a standalone distribution.")
30

31
        if isinstance(name, string_types):
2✔
32
            data = kwargs.pop('observed', None)
2✔
33
            if isinstance(data, ObservedRV) or isinstance(data, FreeRV):
2✔
34
                raise TypeError("observed needs to be data but got: {}".format(type(data)))
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
35
            total_size = kwargs.pop('total_size', None)
2✔
36
            dist = cls.dist(*args, **kwargs)
2✔
37
            return model.Var(name, dist, data, total_size)
2✔
38
        else:
39
            raise TypeError("Name needs to be a string but got: {}".format(name))
×
40

41
    def __getnewargs__(self):
2✔
42
        return _Unpickling,
2✔
43

44
    @classmethod
2✔
45
    def dist(cls, *args, **kwargs):
46
        dist = object.__new__(cls)
2✔
47
        dist.__init__(*args, **kwargs)
2✔
48
        return dist
2✔
49

50
    def __init__(self, shape, dtype, testval=None, defaults=(),
2✔
51
                 transform=None, broadcastable=None):
52
        self.shape = np.atleast_1d(shape)
2✔
53
        if False in (np.floor(self.shape) == self.shape):
2✔
54
            raise TypeError("Expected int elements in shape")
×
55
        self.dtype = dtype
2✔
56
        self.type = TensorType(self.dtype, self.shape, broadcastable)
2✔
57
        self.testval = testval
2✔
58
        self.defaults = defaults
2✔
59
        self.transform = transform
2✔
60

61
    def default(self):
2✔
62
        return np.asarray(self.get_test_val(self.testval, self.defaults), self.dtype)
2✔
63

64
    def get_test_val(self, val, defaults):
2✔
65
        if val is None:
2✔
66
            for v in defaults:
2✔
67
                if hasattr(self, v) and np.all(np.isfinite(self.getattr_value(v))):
2✔
68
                    return self.getattr_value(v)
2✔
69
        else:
70
            return self.getattr_value(val)
2✔
71

72
        if val is None:
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
73
            raise AttributeError("%s has no finite default value to use, "
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
74
                                 "checked: %s. Pass testval argument or "
75
                                 "adjust so value is finite."
76
                                 % (self, str(defaults)))
77

78
    def getattr_value(self, val):
2✔
79
        if isinstance(val, string_types):
2✔
80
            val = getattr(self, val)
2✔
81

82
        if isinstance(val, tt.TensorVariable):
2✔
83
            return val.tag.test_value
2✔
84

85
        if isinstance(val, tt.TensorConstant):
2✔
86
            return val.value
2✔
87

88
        return val
2✔
89

90
    def _repr_latex_(self, name=None, dist=None):
2✔
91
        """Magic method name for IPython to use for LaTeX formatting."""
92
        return None
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
93

94
    __latex__ = _repr_latex_
2✔
95

96

97
def TensorType(dtype, shape, broadcastable=None):
2✔
98
    if broadcastable is None:
2✔
99
        broadcastable = np.atleast_1d(shape) == 1
2✔
100
    return tt.TensorType(str(dtype), broadcastable)
2✔
101

102

103
class NoDistribution(Distribution):
2✔
104

105
    def __init__(self, shape, dtype, testval=None, defaults=(),
2✔
106
                 transform=None, parent_dist=None, *args, **kwargs):
107
        super(NoDistribution, self).__init__(shape=shape, dtype=dtype,
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
108
                                             testval=testval, defaults=defaults,
109
                                             *args, **kwargs)
110
        self.parent_dist = parent_dist
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
111

112
    def __getattr__(self, name):
2✔
113
        try:
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
114
            self.__dict__[name]
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
115
        except KeyError:
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
116
            return getattr(self.parent_dist, name)
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
117

118
    def logp(self, x):
2✔
119
        return 0
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
120

121

122
class Discrete(Distribution):
2✔
123
    """Base class for discrete distributions"""
124

125
    def __init__(self, shape=(), dtype=None, defaults=('mode',),
2✔
126
                 *args, **kwargs):
127
        if dtype is None:
2✔
128
            if theano.config.floatX == 'float32':
2✔
129
                dtype = 'int16'
2✔
130
            else:
131
                dtype = 'int64'
×
132
        if dtype != 'int16' and dtype != 'int64':
2✔
133
            raise TypeError('Discrete classes expect dtype to be int16 or int64.')
×
134

135
        if kwargs.get('transform', None) is not None:
2✔
136
            raise ValueError("Transformations for discrete distributions "
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
137
                             "are not allowed.")
138

139
        super(Discrete, self).__init__(
2✔
140
            shape, dtype, defaults=defaults, *args, **kwargs)
141

142

143
class Continuous(Distribution):
2✔
144
    """Base class for continuous distributions"""
145

146
    def __init__(self, shape=(), dtype=None, defaults=('median', 'mean', 'mode'),
2✔
147
                 *args, **kwargs):
148
        if dtype is None:
2✔
149
            dtype = theano.config.floatX
2✔
150
        super(Continuous, self).__init__(
2✔
151
            shape, dtype, defaults=defaults, *args, **kwargs)
152

153

154
class DensityDist(Distribution):
2✔
155
    """Distribution based on a given log density function."""
156

157
    def __init__(self, logp, shape=(), dtype=None, testval=0, *args, **kwargs):
2✔
158
        if dtype is None:
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
159
            dtype = theano.config.floatX
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
160
        super(DensityDist, self).__init__(
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
161
            shape, dtype, testval, *args, **kwargs)
162
        self.logp = logp
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
163

164

165
def draw_values(params, point=None):
2✔
166
    """
167
    Draw (fix) parameter values. Handles a number of cases:
168

169
        1) The parameter is a scalar
170
        2) The parameter is an *RV
171

172
            a) parameter can be fixed to the value in the point
173
            b) parameter can be fixed by sampling from the *RV
174
            c) parameter can be fixed using tag.test_value (last resort)
175

176
        3) The parameter is a tensor variable/constant. Can be evaluated using
177
        theano.function, but a variable may contain nodes which
178

179
            a) are named parameters in the point
180
            b) are *RVs with a random method
181

182
    """
183
    # Distribution parameters may be nodes which have named node-inputs
184
    # specified in the point. Need to find the node-inputs to replace them.
185
    givens = {}
2✔
186
    for param in params:
2✔
187
        if hasattr(param, 'name'):
2✔
188
            named_nodes = get_named_nodes(param)
2✔
189
            if param.name in named_nodes:
2✔
190
                named_nodes.pop(param.name)
2✔
191
            for name, node in named_nodes.items():
2✔
192
                if not isinstance(node, (tt.sharedvar.SharedVariable,
2✔
193
                                         tt.TensorConstant)):
194
                    givens[name] = (node, _draw_value(node, point=point))
2✔
195
    values = []
2✔
196
    for param in params:
2✔
197
        values.append(_draw_value(param, point=point, givens=givens.values()))
2✔
198
    return values
2✔
199

200

201
@memoize
2✔
202
def _compile_theano_function(param, vars, givens=None):
2✔
203
    """Compile theano function for a given parameter and input variables.
204

205
    This function is memoized to avoid repeating costly theano compilations
206
    when repeatedly drawing values, which is done when generating posterior
207
    predictive samples.
208

209
    Parameters
210
    ----------
211
    param : Model variable from which to draw value
212
    vars : Children variables of `param`
213
    givens : Variables to be replaced in the Theano graph
214

215
    Returns
216
    -------
217
    A compiled theano function that takes the values of `vars` as input
218
        positional args
219
    """
220
    return function(vars, param, givens=givens,
2✔
221
                    rebuild_strict=True,
222
                    on_unused_input='ignore',
223
                    allow_input_downcast=True)
224

225

226
def _draw_value(param, point=None, givens=None):
2✔
227
    """Draw a random value from a distribution or return a constant.
228

229
    Parameters
230
    ----------
231
    param : number, array like, theano variable or pymc3 random variable
232
        The value or distribution. Constants or shared variables
233
        will be converted to an array and returned. Theano variables
234
        are evaluated. If `param` is a pymc3 random variables, draw
235
        a new value from it and return that, unless a value is specified
236
        in `point`.
237
    point : dict, optional
238
        A dictionary from pymc3 variable names to their values.
239
    givens : dict, optional
240
        A dictionary from theano variables to their values. These values
241
        are used to evaluate `param` if it is a theano variable.
242
    """
243
    if isinstance(param, numbers.Number):
2✔
244
        return param
2✔
245
    elif isinstance(param, np.ndarray):
2✔
246
        return param
2✔
247
    elif isinstance(param, tt.TensorConstant):
2✔
248
        return param.value
2✔
249
    elif isinstance(param, tt.sharedvar.SharedVariable):
2✔
250
        return param.get_value()
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
251
    elif isinstance(param, tt.TensorVariable):
2✔
252
        if point and hasattr(param, 'model') and param.name in point:
2✔
253
            return point[param.name]
2✔
254
        elif hasattr(param, 'random') and param.random is not None:
2✔
255
            return param.random(point=point, size=None)
2✔
256
        else:
257
            if givens:
2✔
258
                variables, values = list(zip(*givens))
2✔
259
            else:
260
                variables = values = []
2✔
261
            func = _compile_theano_function(param, variables)
2✔
262
            return func(*values)
2✔
263
    else:
264
        raise ValueError('Unexpected type in draw_value: %s' % type(param))
1 only PYTHON_VERSION=2.7 FLOATX='float32' TESTCMD="--durations=10 --ignore=pymc3/tests/test_examples.py --cov-append --ignore=pymc3/tests/test_distributions_random.py --ignore=pymc3/tests/test_variational_inference.py --ignore=pymc3/tests/test_shared.py --ignore=pymc3/tests/test_smc.py --ignore=pymc3/tests/test_updates.py --ignore=pymc3/tests/test_posteriors.py --ignore=pymc3/tests/test_sampling.py" ✔
265

266

267
def broadcast_shapes(*args):
2✔
268
    """Return the shape resulting from broadcasting multiple shapes.
269
    Represents numpy's broadcasting rules.
270

271
    Parameters
272
    ----------
273
    *args : array-like of int
274
        Tuples or arrays or lists representing the shapes of arrays to be broadcast.
275

276
    Returns
277
    -------
278
    Resulting shape or None if broadcasting is not possible.
279
    """
280
    x = list(np.atleast_1d(args[0])) if args else ()
2✔
281
    for arg in args[1:]:
2✔
282
        y = list(np.atleast_1d(arg))
2✔
283
        if len(x) < len(y):
2✔
284
            x, y = y, x
×
285
        x[-len(y):] = [j if i == 1 else i if j == 1 else i if i == j else 0
2✔
286
                       for i, j in zip(x[-len(y):], y)]
287
        if not all(x):
2✔
288
            return None
×
289
    return tuple(x)
2✔
290

291

292
def infer_shape(shape):
2✔
293
    try:
2✔
294
        shape = tuple(shape or ())
2✔
295
    except TypeError:  # If size is an int
2✔
296
        shape = tuple((shape,))
2✔
297
    except ValueError:  # If size is np.array
1 only PYTHON_VERSION=2.7 FLOATX='float32' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pymc3/tests/test_distributions_random.py pymc3/tests/test_shared.py pymc3/tests/test_smc.py pymc3/tests/test_sampling.py" ✔
298
        shape = tuple(shape)
1 only PYTHON_VERSION=2.7 FLOATX='float32' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pymc3/tests/test_distributions_random.py pymc3/tests/test_shared.py pymc3/tests/test_smc.py pymc3/tests/test_sampling.py" ✔
299
    return shape
2✔
300

301

302
def reshape_sampled(sampled, size, dist_shape):
2✔
303
    dist_shape = infer_shape(dist_shape)
2✔
304
    repeat_shape = infer_shape(size)
2✔
305

306
    if np.size(sampled) == 1 or repeat_shape or dist_shape:
2✔
307
        return np.reshape(sampled, repeat_shape + dist_shape)
2✔
308
    else:
309
        return sampled
2✔
310

311

312
def replicate_samples(generator, size, repeats, *args, **kwargs):
2✔
313
    n = int(np.prod(repeats))
2✔
314
    if n == 1:
2✔
315
        samples = generator(size=size, *args, **kwargs)
2✔
316
    else:
317
        samples = np.array([generator(size=size, *args, **kwargs)
1 only PYTHON_VERSION=2.7 FLOATX='float32' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pymc3/tests/test_distributions_random.py pymc3/tests/test_shared.py pymc3/tests/test_smc.py pymc3/tests/test_sampling.py" ✔
318
                            for _ in range(n)])
319
        samples = np.reshape(samples, tuple(repeats) + tuple(size))
1 only PYTHON_VERSION=2.7 FLOATX='float32' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pymc3/tests/test_distributions_random.py pymc3/tests/test_shared.py pymc3/tests/test_smc.py pymc3/tests/test_sampling.py" ✔
320
    return samples
2✔
321

322

323
def generate_samples(generator, *args, **kwargs):
2✔
324
    """Generate samples from the distribution of a random variable.
325

326
    Parameters
327
    ----------
328
    generator : function
329
        Function to generate the random samples. The function is
330
        expected take parameters for generating samples and
331
        a keyword argument `size` which determines the shape
332
        of the samples.
333
        The *args and **kwargs (stripped of the keywords below) will be
334
        passed to the generator function.
335

336
    keyword arguments
337
    ~~~~~~~~~~~~~~~~
338

339
    dist_shape : int or tuple of int
340
        The shape of the random variable (i.e., the shape attribute).
341
    size : int or tuple of int
342
        The required shape of the samples.
343
    broadcast_shape: tuple of int or None
344
        The shape resulting from the broadcasting of the parameters.
345
        If not specified it will be inferred from the shape of the
346
        parameters. This may be required when the parameter shape
347
        does not determine the shape of a single sample, for example,
348
        the shape of the probabilities in the Categorical distribution.
349

350
    Any remaining *args and **kwargs are passed on to the generator function.
351
    """
352
    dist_shape = kwargs.pop('dist_shape', ())
2✔
353
    size = kwargs.pop('size', None)
2✔
354
    broadcast_shape = kwargs.pop('broadcast_shape', None)
2✔
355
    params = args + tuple(kwargs.values())
2✔
356

357
    if broadcast_shape is None:
2✔
358
        broadcast_shape = broadcast_shapes(*[np.atleast_1d(p).shape for p in params
2✔
359
                                             if not isinstance(p, tuple)])
360
    if broadcast_shape == ():
2✔
361
        broadcast_shape = (1,)
×
362

363
    args = tuple(p[0] if isinstance(p, tuple) else p for p in args)
2✔
364
    for key in kwargs:
2✔
365
        p = kwargs[key]
2✔
366
        kwargs[key] = p[0] if isinstance(p, tuple) else p
2✔
367

368
    if np.all(dist_shape[-len(broadcast_shape):] == broadcast_shape):
2✔
369
        prefix_shape = tuple(dist_shape[:-len(broadcast_shape)])
2✔
370
    else:
371
        prefix_shape = tuple(dist_shape)
1 only PYTHON_VERSION=2.7 FLOATX='float32' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pymc3/tests/test_distributions_random.py pymc3/tests/test_shared.py pymc3/tests/test_smc.py pymc3/tests/test_sampling.py" ✔
372

373
    repeat_shape = infer_shape(size)
2✔
374

375
    if broadcast_shape == (1,) and prefix_shape == ():
2✔
376
        if size is not None:
2✔
377
            samples = generator(size=size, *args, **kwargs)
2✔
378
        else:
379
            samples = generator(size=1, *args, **kwargs)
2✔
380
    else:
381
        if size is not None:
2✔
382
            samples = replicate_samples(generator,
1 only PYTHON_VERSION=2.7 FLOATX='float32' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pymc3/tests/test_distributions_random.py pymc3/tests/test_shared.py pymc3/tests/test_smc.py pymc3/tests/test_sampling.py" ✔
383
                                        broadcast_shape,
384
                                        repeat_shape + prefix_shape,
385
                                        *args, **kwargs)
386
        else:
387
            samples = replicate_samples(generator,
2✔
388
                                        broadcast_shape,
389
                                        prefix_shape,
390
                                        *args, **kwargs)
391
    return reshape_sampled(samples, size, dist_shape)
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc