pdf_sampler

nbi_stat.pdf_sampler(f, x, dx=None)[source]

Creates a function to sample a PDF

Examples

>>> def f(x,mu,sigma):
...     from numpy import pi, sqrt, exp
...     return 1/(sqrt(2*pi)*sigma)*exp(-.5*((x-mu)/sigma)**2)
...
>>> from numpy import linspace
>>> from numpy.random import uniform
>>>
>>> sampler = pdf_sampler(f,linspace(-3,3,30))
>>> sample  = sampler(uniform(size=1000))
Parameters:
  • f (callable) – PDF to integrate

  • x (array-like) – Points to evalute the PDF at

  • dx (array-like or None) – If given, it should specify the area of each integration point, with the first equal to zero. This is useful if the PDF is a function of several variables. If not given, assume we can calculate it using the difference of x

Returns:

sampler – A function which takes a single argument - random numbers between 0 and 1 and samples the PDF passed to this function.

Return type:

callable