[SciPy-User] scipy.stats: Sampling from an arbitrary probability distribution

Sturla Molden sturla@molden...
Mon Jun 4 08:21:53 CDT 2012


On 03.06.2012 13:20, Daniel Sabinasz wrote:
> Hi all,
>
> I need to sample a random number from a distribution whose probability
> density function I specify myself. Is that possible using scipy.stats?

Sampling a general distribution is typically an MCMC problem, that e.g. 
can be solved with the Metropolis-Hastings sampler.

http://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm

Because of its recursive nature, a Markov chain like this is better 
written in Cython, or you can use NumPy to run multiple chains in 
parallel. (I depends on how many samples you need, of course, anything 
below a million should be fast enough in Python.)

You might also take a look at PyMCMC:
https://github.com/rdenham/pymcmc


Sturla


More information about the SciPy-User mailing list