       Re: How to sample a 2-dim. r.v. with known density function?

• To: mathgroup at smc.vnet.net
• Subject: [mg65274] Re: How to sample a 2-dim. r.v. with known density function?
• From: "Valeri Astanoff" <astanoff at yahoo.fr>
• Date: Thu, 23 Mar 2006 06:58:36 -0500 (EST)
• References: <dvrbl7\$a1l\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Hi Kees,
Here is a quick and dirty way to do yourself,
for a bivariate non-conventional distribution,
what Mathematica's RandomArray does for conventional distributions :

In:=<<Statistics`

In:=randArray[pdf_, {x_, y_},
(* discretize x : *) {xmin_, xmax_, dx_},
(* discretize y : *) {ymin_, ymax_, dy_},
sampleSize_]:=
Module[{cells, cellSampleSize, cellSample},
cells = Table[{x,y}, {x,xmin,xmax,dx},
{y,ymin,ymax,dy}] // Flatten[#,1]&;
cellSampleSize[xx_, yy_] := sampleSize*
(pdf /. x -> xx+dx/2 /. y -> yy+dy/2)*dx*dy;
cellSample[{xx_, yy_}] :=
Table[{xx+Random[]*dx, yy+Random[]*dy},
{cellSampleSize[xx,yy] // Round}];
Flatten[cellSample /@ cells,1]
];

Check example with a bivariate normal distribution :

In:=distr=MultinormalDistribution[{10,20},{{5,1},{1,5}}];

In:=pdf = PDF[distr,{x,y}];

Choice of discretization ranges and sample size is critical.
Sample size has to be sufficiently large, i.e. far greater
than the number of discretization cells :

In:=samp = randArray[pdf,{x,y},{0,20,1},{10,30,1},10000];

In:=Mean[distr]
Out={10,20}

In:=Mean[samp]
Out={10.0045,20.0012}

In:=StandardDeviation[distr]
Out={Sqrt, Sqrt}

In:=StandardDeviation[samp]
Out={2.24731,2.24718}

hth

V.Astanoff

```

• Prev by Date: Re: Subscripted variables and function definitions
• Next by Date: Re: Integration Bug
• Previous by thread: Re: How to sample a 2-dim. r.v. with known density function?
• Next by thread: Re: How to sample a 2-dim. r.v. with known density function?