Re: [Numpy-discussion] [SciPy-User] vectorized version of 'multinomial' sampling function
On Tue, Oct 13, 2009 at 7:59 PM, David Warde-Farley
On 13-Oct-09, at 5:01 PM, per freem wrote:
hi all,
i have a series of probability vector that i'd like to feed into multinomial to get an array of vector outcomes back. for example, given:
p = array([[ 0.9 , 0.05, 0.05], [ 0.05, 0.05, 0.9 ]])
i'd like to call multinomial like this:
multinomial(1, p)
to get a vector of multinomial samplers, each using the nth list in 'p'. something like:
array([[1, 0, 0], [0, 0 1]]) in this case. is this possible? it seems like 'multinomial' takes only a one dimensional array. i could write this as a "for" loop of course but i prefer a vectorized version since speed is crucial for me here.
thanks very much.
Your best bet is probably to copy the pyrex/Cython code for multinomial in numpy/random/mtrand/mtrand.pyx, and add the functionality you want there. If you do it right (i.e. type your loop indices) then it should be fast.
David _______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
Hi David thanks for your reply. i am not sure how to do this though -- is the vectorized version i would write in pyrex/cython simply going to iterate through this vector of vectors and do the operation? will that really be efficient? is there some other library that can do vectorized multinomial like i described? i really am not sure how to write this cython.
On Tue, Oct 13, 2009 at 23:27, per freem
thanks for your reply. i am not sure how to do this though -- is the vectorized version i would write in pyrex/cython simply going to iterate through this vector of vectors and do the operation? will that really be efficient?
Yes because the iteration, if written correctly, will be in C. This is all that "vectorization" means in this context. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (2)
-
per freem
-
Robert Kern