4
votes

I'm building a monte carlo simulation using python, and have thus far been using numpy to generate my random variates. However, I've just learned that numpy uses the Mersenne Twister algorithm to produce its random numbers, which based on my limited understanding is not desireable in monte carlo simulations. I'd much prefer to use MRG32k3a, but I'd also like to take advantage of numpy's distribution functions. Is there any way to make numpy use a generator of my choice, or is there another library that will give me the same functionality with the option of using my preferred generator?

1
Maybe take a look at this pypi.org/project/randomstate , it says it includes MRG32k3aKays Kadhi
which based on my limited understanding is not desireable in monte carlo simulations not sure why do you think this is true. MT is quite ok for a lot of uses. Anyway, if you want alternative, take a look at github.com/bashtage/randomgenSeverin Pappadeux
Thank you both for your help. Severin, this comes from an instructor for a course I'm taking on monte carlo simulations (the simulation I'm building is for an unrelated project) who said in class that MT is good for most purposes but may not work for monte carlo simulations specifically. The wikipedia page supports this en.wikipedia.org/wiki/Mersenne_Twister#cite_note-39 and while there seem to be ways around the issues that arise, it's not clear to me that numpy automatically uses these workarounds, so a drop-in replacement seems to be the most straightforward route.Nathan Geldner
@KaysKadhi Please submit this as a better answer if it indeeds work for the OP?jtlz2

1 Answers

1
votes

Why don't you code it up yourself in python?

I found an example implementation of the algorithm at http://simul.iro.umontreal.ca/rng/MRG32k3a.c (with many others at http://www-labs.iro.umontreal.ca/~simul/rng)

If you are worried about speed (but profile it first!) you could wrap the C or F code and call it directly from python using e.g. CFFI, f2py etc - see e.g.

https://scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html

But given it's only a few lines of C coding it in python shouldn't be too challenging, right?

You could also submit a pull request at https://github.com/bashtage/randomgen (or ask the author nicely to include your chosen generator).

I'm sorry this doesn't address your need for distributions, but perhaps it's a start and a better answer will come along.

Also perhaps work out why Mersenne is no good for your own purposes - have you tested it and what figures of merit are you using to decide?

https://scicomp.stackexchange.com/questions/23547/parallel-mersenne-twister-for-monte-carlo might be a useful reference for you.

Can you do any precomputation?

Hope that helps in some small way.