[SciPy-User] rvs and broadcasting

josef.pktd@gmai... josef.pktd@gmai...
Wed Nov 23 20:47:29 CST 2011


rvs in scipy.stats distributions has a nasty broadcasting if location
or scale are arrays and size is not defined for the same shape
http://projects.scipy.org/scipy/ticket/1544

(also https://groups.google.com/group/pystatsmodels/browse_thread/thread/e757d73b2a06b962?hl=en
)

I was playing with two solutions while I was writing a rvs for the
truncated normal.

1) broadcast shape parameters, loc and scale, if they are arrays
produce rvs in that shape, and, if in this case size is not the same
or 1, then raise a ValueError
essentially
    lower, upper, loc, scale = np.broadcast_arrays(lower, upper, loc, scale)
    if (np.size(lower) > 1) and (size != (1,)) and (lower.shape != size):
        raise ValueError('Do you really want this? Then do it yourself.')

2) broadcast shape parameters, loc and scale,  for each of these
create random variables given by size, the return shape is essentially
broadcasted shape concatenated with size, for example

assert_equal(truncnorm_rvs(lower*np.arange(4)[:,None], upper,
                                loc=np.arange(5), scale=1, size=(2,3)).shape,
                     (4, 5, 2, 3))

this version is attached.

Any opinions about which version should be preferred?

(As aside, truncnorm and other distribution with parameter dependent
support might also have other problems than just the broadcasting of
shape and scale.)

Josef
-------------- next part --------------
A non-text attachment was scrubbed...
Name: random_truncnorm_1.py
Type: text/x-python
Size: 5149 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/scipy-user/attachments/20111123/610286ed/attachment.py 


More information about the SciPy-User mailing list