Re: Random Normal deviates within compiled function?
- To: mathgroup at smc.vnet.net
- Subject: [mg62302] Re: Random Normal deviates within compiled function?
- From: John Doty <jpd at whispertel.LoseTheH.net>
- Date: Sat, 19 Nov 2005 23:18:29 -0500 (EST)
- References: <dln17e$gf4$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Gareth Russell wrote: > Hi All, > > I have a function which needs to be quick as it's used in simulations, > and it requires the internal generation of a large vector of random > normal deviates (up to 50,000). It appears that a function like > Random[NormalDistribution[0,s]] cannot be compiled. Can anyone suggest > an algorithm for getting such numbers that would work within a Compile > statement and still be quicker than using the non-compiled function? > > The non-compiled function would look as follows: > > f[v_,r_,k_,s_,q_]:=Select[v*Exp[r*(1 - v/k) + > RandomArray[NormalDistribution[0, s], > Length[v]]], # > q &] > > where v is a vector of 50,000 reals, and r, k, s and q are scalar reals. > > (If anyone is interested, this is for population projection in > population viability analysis.) > > Thanks, > > Gareth Russell > NJIT > I've been using the following: fastNoise = Compile[{}, 2.(Random[] + Random[] + Random[] - 1.5)]; It makes variates with mean 0, variance 1, with a roughly normal distribution (for your problem multiply by s to adjust the variance). Whether this is accurate enough depends on the problem: its main defect is that its PDF falls to 0 outside the range [-3,3], so it won't generate the rare extreme values in the normal distribution. Whether this is a problem depends on what you're trying to calculate. -jpd