Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85272
| References | <9a4636dd-5e9a-4c24-ae1c-ac5b447b3039@googlegroups.com> <mb07bj$of7$2@dont-email.me> <11497cc2-97e0-4af0-9adb-a8f9955ae774@googlegroups.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2015-02-05 14:00 -0700 |
| Subject | Re: Monte Carlo probability calculation in Python |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.18496.1423170069.18130.python-list@python.org> (permalink) |
On Thu, Feb 5, 2015 at 12:25 PM, Paul Moore <p.f.moore@gmail.com> 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?
At the point memory becomes an issue you can partially roll it back
into a loop. For example, deal the bridge hands 10000 times in a loop
of 100.
> 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).
Building on Rob's example:
def monopoly(throws, per=2, rerolls=3, sides=6):
all_dice = np.random.randint(1, sides+1, size=(throws, rerolls, per))
doubles = all_dice[...,0] == all_dice[...,1]
three_doubles = doubles[:,0] & doubles[:,1] & doubles[:,2]
return all_dice.sum(axis=2), doubles, three_doubles
This returns a (throws x rerolls) array of the sum of each roll, a
(throws x rerolls) array of booleans indicating whether the roll was a
double or not, and a throws-long array of booleans indicating whether
three doubles were rolled.
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