Re: Random sampling of an arbitrary distribution
- To: mathgroup at smc.vnet.net
- Subject: [mg59787] Re: [mg59765] Random sampling of an arbitrary distribution
- From: ggroup at sarj.ca
- Date: Sun, 21 Aug 2005 03:51:35 -0400 (EDT)
- References: <200508200714.DAA12585@smc.vnet.net>
- Reply-to: ggroup at sarj.ca
- Sender: owner-wri-mathgroup at wolfram.com
On Saturday, August 20, 2005 at 03:14 GMT -0400, crowds cheered as
Owen, Hl (Hywel) unveiled:
> I would like to generate a number of sample values which are distributed
> according to an arbitrary probability distribution, e.g. not Uniform,
> Normal, or any of the ones in the ContinuousDistributions package. Given
> a pure function distfn,
<snip>
Basically, you just need to compute the cumulative density
function (CDF), which is the integral of your distribution (PDF).
Two features we're going to make use of:
1. The CDF is uniformly distributed over the interval 0 to 1.
2. The CDF is a function of a random variable distributed according to
your PDF.
So to generate your number, you set your CDF (cdf[x]) equal to
Random[] and solve for x.
--------------------------------------------------------
(* An example is a negative exponential distribution: *)
pdf[x_]=Exp[-x]
(* Define the upper and lower bounds *)
lb=0; ub=Infinity;
(* Since this distribution is normalized, define the CDF *)
cdf[x_]=Integrate[pdf[t],{t,lb,x}, Assumptions->lb<x<ub]
(* Now create your generating function *)
genx[u_]=x/.Solve[cdf[x]==u,x][[1]]
(* And now you can create your random array: *)
rand=Table[genx[Random[]],{10000}]
(* Now to check that everything worked: *)
Needs["Graphics`Graphics"]
pl1=Histogram[rand,HistogramScale->1,DisplayFunction->Identity];
pl2=Plot[pdf[x],{x,lb,10},DisplayFunction->Identity];
Show[pl1,pl2,DisplayFunction->$DisplayFunction]
--------------------------------------------------------
* This example is based loosely on one from the text book:
Bayesian Logical Data Analysis for the Physical Sciences: A
Comparative Approach with Mathematica Support
by P.C. Gregory
- References:
- Random sampling of an arbitrary distribution
- From: "Owen, HL \(Hywel\)" <h.l.owen@dl.ac.uk>
- Random sampling of an arbitrary distribution