Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85271
| From | Rob Gaddi <rgaddi@technologyhighland.invalid> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Monte Carlo probability calculation in Python |
| Date | 2015-02-05 20:56 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <mb0ldd$of7$3@dont-email.me> (permalink) |
| References | <9a4636dd-5e9a-4c24-ae1c-ac5b447b3039@googlegroups.com> <mb07bj$of7$2@dont-email.me> <11497cc2-97e0-4af0-9adb-a8f9955ae774@googlegroups.com> |
On Thu, 05 Feb 2015 11:25:42 -0800, Paul Moore wrote: > On Thursday, 5 February 2015 16:57:07 UTC, Rob Gaddi wrote: >> You don't need the whole scipy stack, numpy will let you do everything >> you want. The trick to working in numpy is to parallelize your >> problem; >> you don't do a thing a thousand times; you do it on a thousand-length >> array. For example: >> >> def dice(throws, per, sides=6): >> """Return an array throws long of rolls of (per)d(sides).""" >> all_dice = np.random.randint(1, sides+1, size=(throws, per)) >> return all_dice.sum(axis=1) > > Thanks, that's a help. I see the principle, but a couple of questions. > With bigger problems (deal 52 cards into bridge hands a million times, > for example) would memory become an issue? Also, how do you handle > things that don't fit into the built-in numpy operations? (For example, > Monopoly - roll 2 dice and take the sum, unless you roll a double, in > which case reroll, but if you roll 3 doubles you fail - return NaN in > that case). > > As examples of a few more problems I'd use this for: > > 1. Roll 4 dice, and sum the largest 3. > 2. Roll 9 dice, count how many different numbers you get. > 3. Deal 4 hands of 13 cards from a full deck, determine the length of > the longest suit anyone has. > 4. Monopoly rolls (see above, 2 dice but reroll doubles up to 3 times) > > All of these are reasonably easy to write as Python functions, but it > becomes progressively harder (for a non-numpy expert) to know how to > convert them into parallelizable numpy primitives. > > I'll have a play with the approach you suggest though, and see how it > handles some of the more complex problems my friend was showing off with > :-) (his worst example was a horrendous expression to score Cribbage > hands, that if I read his note correctly took about a minute to simulate > 10,000,000 deals) > > Paul A lot of your examples can be dealt by the numpy fancy indexing tricks, which is really the heart of numpy. A comparison on an array of numbers gives an array of bools, which can then be used to index the original array. The Monopoly example, for instance. all_dice = np.random.randint(1, 7, size=(throws, 2)) doubles = all_dice[all_dice[:,0] == all_dice[:,1]] doubles is now a writeable view into the original all_dice array, so you could reroll them by saying something like. doubles[:,:] = np.random.randint(1, 7, size=doubles.shape) Memory will eventually become a concern, but in that case you could always run it in parts and aggregate the parts. I wouldn't sweat it until you get into 100s of millions of tries. Even then, you could make some improvements by forcing the dtypes to be uint8, though there may be a performance penalty. Numpy is fantastically powerful once you can wrap your head around it; I use it for a ton of things I used to use Matlab for. The tutorial at http://wiki.scipy.org/Tentative_NumPy_Tutorial will go into lots of depth for you. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Monte Carlo probability calculation in Python Paul Moore <p.f.moore@gmail.com> - 2015-02-05 08:20 -0800
Re: Monte Carlo probability calculation in Python Joel Goldstick <joel.goldstick@gmail.com> - 2015-02-05 11:27 -0500
Re: Monte Carlo probability calculation in Python Paul Moore <p.f.moore@gmail.com> - 2015-02-05 08:33 -0800
Re: Monte Carlo probability calculation in Python Sturla Molden <sturla.molden@gmail.com> - 2015-02-07 12:30 +0000
Re: Monte Carlo probability calculation in Python Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-02-05 16:56 +0000
Re: Monte Carlo probability calculation in Python Paul Moore <p.f.moore@gmail.com> - 2015-02-05 11:25 -0800
Re: Monte Carlo probability calculation in Python Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-02-05 20:56 +0000
Re: Monte Carlo probability calculation in Python Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-05 14:00 -0700
Re: Monte Carlo probability calculation in Python Paul Moore <p.f.moore@gmail.com> - 2015-02-05 15:02 -0800
Re: Monte Carlo probability calculation in Python Paul Moore <p.f.moore@gmail.com> - 2015-02-06 14:54 -0800
Re: Monte Carlo probability calculation in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-07 10:49 +1100
Re: Monte Carlo probability calculation in Python Paul Moore <p.f.moore@gmail.com> - 2015-02-09 13:57 -0800
Re: Monte Carlo probability calculation in Python Paul Moore <p.f.moore@gmail.com> - 2015-02-10 06:40 -0800
csiph-web