Skip to content

Commit 259613f

Browse files
ericmjlColCarroll
authored andcommitted
Multivariate floating point precision fix (#2470)
* fixed multivariate * multivariate fix for pvals > 1 * updated multivariate with hack * bugfix * float64 patch to multivariate's multinomial * reinstated p.squeeze(), accidentally took it out * updated based on failed test * further minor fixes to multinomial * Force push to restart Travis
1 parent 43c7778 commit 259613f

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

pymc3/distributions/multivariate.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,18 +503,22 @@ def __init__(self, n, p, *args, **kwargs):
503503
self.mode = tt.cast(tround(self.mean), 'int32')
504504

505505
def _random(self, n, p, size=None):
506+
original_dtype = p.dtype
507+
# Set float type to float64 for numpy. This change is related to numpy issue #8317 (https://github.com/numpy/numpy/issues/8317)
508+
p = p.astype('float64')
509+
# Now, re-normalize all of the values in float64 precision. This is done inside the conditionals
506510
if size == p.shape:
507511
size = None
508-
509512
if p.ndim == 1:
513+
p = p / p.sum()
510514
randnum = np.random.multinomial(n, p.squeeze(), size=size)
511515
elif p.ndim == 2:
516+
p = p / p.sum(axis=1, keepdims=True)
512517
randnum = np.asarray([np.random.multinomial(n, pp, size=size) for pp in p])
513518
else:
514519
raise ValueError('Outcome probabilities must be 1- or 2-dimensional '
515520
'(supplied `p` has {} dimensions)'.format(p.ndim))
516-
517-
return randnum
521+
return randnum.astype(original_dtype)
518522

519523
def random(self, point=None, size=None):
520524
n, p = draw_values([self.n, self.p], point=point)

0 commit comments

Comments
 (0)