Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85258
| References | <9a4636dd-5e9a-4c24-ae1c-ac5b447b3039@googlegroups.com> |
|---|---|
| Date | 2015-02-05 11:27 -0500 |
| Subject | Re: Monte Carlo probability calculation in Python |
| From | Joel Goldstick <joel.goldstick@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.18492.1423153674.18130.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On Thu, Feb 5, 2015 at 11:20 AM, Paul Moore <p.f.moore@gmail.com> wrote: > I'm interested in prototyping a Monte Carlo type simulation algorithm in > Python. The background is that a friend has written a similar program in > C++, and I'm interested in seeing if I can achieve something comparable in > a much better language :-) > > The basic job of the program will be to simulate games of chance - so > we'll have random inputs (die rolls, card draws, etc) and repeatedly > simulate calculating a "result". Based on the results of the simulation, > the idea is to estimate the probability of a given result. > > So, to give a very specific example: > > import random > > def die(n, sides=6): > total = sum(random.randint(1, sides) for i in range(n)) > return total > > def simulate(n, test): > "Run the simulation N times, returning the probability that TEST is > true" > successes = 0 > for i in range(n): > if test(): > successes = successes + 1 > return successes/n > > def check_3d6_gt_15(): > return die(3) > 15 > > if __name__ == '__main__': > print(simulate(100000, check_3d6_gt_15)) > > Obviously, this is going to run ridiculously slowly as the number of > simulations or the complexity of the calculation increases, but this gives > the idea. > > My immediate instinct is that somewhere in the scipy stack, there will be > a module that does this sort of thing efficiently, but I don't really know > where to look - my understanding of the maths involved is very much at the > naive level above, so I'm not sure what terms I should be searching for, or > how to frame a query. > > Can anyone give me some pointers as to where I should go to find out more > about this sort of task? Either more general theory (that would help me ask > the right questions!) or specific packages or techniques I should be using > in Python/numpy would be fantastic. > > Any help would be gratefully accepted - surely nobody wants to see Python > beaten by a C++ program??? :-) > > Thanks, > Paul > -- > https://mail.python.org/mailman/listinfo/python-list > have you googled "python monte carlo"? -- Joel Goldstick http://joelgoldstick.com
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