pymontecarlo
(Currently under development!) A small Python library for creating simple Monte Carlo simulations.
Installation
pip install montecarlo
Example
(See examples directory for more examples)
from montecarlo import montecarlo
from random import randint
def flip_is_heads(g):
return randint(0, 1) == 0
mc = montecarlo(flip_is_heads)
mc.run()
Output:
Iteration #10000: 0.5079
Iteration #20000: 0.50485
Iteration #30000: 0.5059
Iteration #40000: 0.50295
...
Iteration #970000: 0.499608247423
Iteration #980000: 0.499712244898
Iteration #990000: 0.499737373737
Iteration #1000000: 0.499728
======================
======= FINAL ========
======================
Iteration #1000000: 0.499728
Usage
The setup
function passed should return a dictionary which is then passed into your test function.
The func
function passed should have a single required parameter, a dictionary, which is generated by setup
or empty otherwise. It should return a boolean (True for success or False otherwise).
Currently, teardown
doesn't do much besides get called.
The montecarlo.run() method will also accept optional parameters iterations
and print_every
. The simulation will print the current probability every print_every
iterations and will run for iterations
iterations.