HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Generating values from a probability density function

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
densityfunctiongeneratingprobabilityvaluesfrom

Problem

If I have a probability density function, some f(x) which is >= 0 everywhere and integrates to 1, is there a method to generate random numbers using that PDF?

I know there is the box-muller transform to convert uniform random numbers to gaussian, but is there anything for the general case, or at least a more general case of PDFs?

Solution

You can integrate the PDF to a CDF F(x), then uniformly generate a random number x between 0 and 1 and choose a y such that F(y)=x as your sample. This is more or less difficult, depending on your PDF. Some PDFs have analytical solutions, others need numerical methods. For some distribution Rejection sampling works well, some distributions can be sampled from using a suitably constructed Markov chain.

See also this set of slides for more details.

Context

StackExchange Computer Science Q#79393, answer score: 5

Revisions (0)

No revisions yet.