Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'example:': 0.03; 'algorithm': 0.04; 'scipy': 0.05; 'subject:Python': 0.06; 'c++,': 0.07; 'prototyping': 0.07; '__name__': 0.09; 'calculating': 0.09; 'pointers': 0.09; 'stack,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'random': 0.14; 'times,': 0.14; '"python': 0.16; '"run': 0.16; "'__main__':": 0.16; 'beaten': 0.16; 'efficiently,': 0.16; 'for,': 0.16; 'googled': 0.16; 'inputs': 0.16; 'obviously,': 0.16; 'simulate': 0.16; ':-)': 0.16; 'language': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'module': 0.19; 'result.': 0.19; 'thu,': 0.19; 'written': 0.21; 'feb': 0.22; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'paul': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; '>': 0.26; 'somewhere': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'gives': 0.31; 'probability': 0.31; 'anyone': 0.31; 'run': 0.32; 'url:python': 0.33; 'basic': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'c++': 0.36; 'returning': 0.36; 'surely': 0.36; 'url:listinfo': 0.36; 'similar': 0.36; 'url:org': 0.36; 'should': 0.36; 'searching': 0.37; 'so,': 0.37; 'level': 0.37; 'skip:& 10': 0.38; 'does': 0.39; 'sure': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'above,': 0.60; 'slowly': 0.60; 'card': 0.63; 'more': 0.64; 'total': 0.65; 'chance': 0.65; '(that': 0.65; 'techniques': 0.66; 'nobody': 0.68; 'results': 0.69; 'friend': 0.79; '2015': 0.84; 'comparable': 0.84; 'complexity': 0.84; 'maths': 0.84; 'monte': 0.84; 'ridiculously': 0.84; 'simulations': 0.84; '(die': 0.91; 'joel': 0.91; 'simulation': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=qlGSAadkREDy3IJLz4dUvn0r05Bs18rEMBpiKeEhK5A=; b=LMYhY9X0W053gSn6uESS3hets+rObol3jmaLs2gk6eL/yWz3nnKJa/ZIcQjB37xLPc yO0XLHjVWNd0trtDAEYvtFMehVR1ToC0cXddvRHt9tsjiBiPhYJfrjUPKjk7J10+IBRv vbwMj1YEQ5WLjWjKoyRfmeNdcBP1c47pxk4BUXEZMLt486fFoawln9Psvy9Ku0ce/z61 VUsgHLBVXuHjyiUmxb3OqVF4ffn6I7UqQcBQdF5Fwh5zj2Jyn6oXoVOU77T5SLxSSEkg 6r8SKj88Xe+mmScVSHdNnsepCJ0YXqpq8mhN4V9B881keP904M4fglKTcIljaEI8W7jo r8Ow== MIME-Version: 1.0 X-Received: by 10.42.97.66 with SMTP id m2mr7994663icn.48.1423153671329; Thu, 05 Feb 2015 08:27:51 -0800 (PST) In-Reply-To: <9a4636dd-5e9a-4c24-ae1c-ac5b447b3039@googlegroups.com> References: <9a4636dd-5e9a-4c24-ae1c-ac5b447b3039@googlegroups.com> Date: Thu, 5 Feb 2015 11:27:51 -0500 Subject: Re: Monte Carlo probability calculation in Python From: Joel Goldstick Cc: "python-list@python.org" Content-Type: multipart/alternative; boundary=20cf303bfb5e0a5e98050e59c9c2 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 141 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423153674 news.xs4all.nl 2950 [2001:888:2000:d::a6]:53363 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85258 --20cf303bfb5e0a5e98050e59c9c2 Content-Type: text/plain; charset=UTF-8 On Thu, Feb 5, 2015 at 11:20 AM, Paul Moore 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 --20cf303bfb5e0a5e98050e59c9c2 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


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 seei= ng 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= 9;ll have random inputs (die rolls, card draws, etc) and repeatedly simulat= e 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=3D6):
=C2=A0 =C2=A0 total =3D sum(random.randint(1, sides) for i in range(n))
=C2=A0 =C2=A0 return total

def simulate(n, test):
=C2=A0 =C2=A0 "Run the simulation N times, returning the probability t= hat TEST is true"
=C2=A0 =C2=A0 successes =3D 0
=C2=A0 =C2=A0 for i in range(n):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if test():
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 successes =3D successes + 1
=C2=A0 =C2=A0 return successes/n

def check_3d6_gt_15():
=C2=A0 =C2=A0 return die(3) > 15

if __name__ =3D=3D '__main__':
=C2=A0 =C2=A0 print(simulate(100000, check_3d6_gt_15))

Obviously, this is going to run ridiculously slowly as the number of simula= tions or the complexity of the calculation increases, but this gives the id= ea.

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 kn= ow where to look - my understanding of the maths involved is very much at t= he 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 a= bout 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 b= eaten by a C++ program??? :-)

Thanks,
Paul
--
https://mail.python.org/mailman/listinfo/python-list

have you googled "python monte ca= rlo"?

--
--20cf303bfb5e0a5e98050e59c9c2--