Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Random Normal deviates within compiled function?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg62655] Re: Random Normal deviates within compiled function?
  • From: "Ray Koopman" <koopman at sfu.ca>
  • Date: Wed, 30 Nov 2005 00:07:33 -0500 (EST)
  • Organization: http://groups.google.com
  • References: <dln17e$gf4$1@smc.vnet.net><dmh9fg$8ul$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Lee Newman wrote:
> I use the following two functions. The first generates a single value
> from a normal distribution with mean and stdev specified in the
> arguments.  It avoids the use of Mathematica's built in distribution
> functions.   It will generate 10^6 values (using Table or Do) with
> timing of about 4 seconds on my machine (2.8GHz P4 with 1GB RAM).   The
> second function is really fast.  It uses the built-in NormalDistribution
> function, and for some reason using RandomArray makes it quite fast.  It
> generates an array of 10^6 values in about 0.5seconds on my machine.
>
>
> GaussianNoise = Compile[{{mean, _Real}, {stdev, _Real}},
>        Module[{r = 0.0, x = 0.0, y = 0.0},
>          While[r == 0.0 || r > 1,
>            x = Random[Real, {-1, 1}];
>            y = Random[Real, {-1, 1}];
>            r = x*x + y*y;
>            ];
>          (x*Sqrt[-2Log[r]/r])*stdev + mean
>          ]
>        ];
>
> GaussianArray =
>      RandomArray[NormalDistribution[#1, #2], #3] &;
> (* the first argument is mean, second is the stdev and the third
> specifies the length of the array. *)
>
> Cheers,
> Lee Newman

See my June 16, 2003, post "Warning -- another Random[] failure".
Substituting your GaussianNoise[0,1] for Random[] in my code, and
using n1 = n2 = 7, reps = 10^6, gives a residual vector z = {-3.41982,
-4.12281, -.859585, -1.25571, -1.20193, -1.36457, .863248, -1.0381,
.884148, 1.54233, 2.8457, 2.28917, 2.75297, 2.75567, 2.65074, 2.31981,
1.50249, 2.49637, -.324406, -.211185, -3.19893, -2.32533, -1.65605,
-3.64346, -2.50297, -2.5255, -1.12445, -1.9046, -.786623, .151306,
-.844143, -.570679, .6032, 1.3326, 1.1453, 1.50075, 1.76664,
3.94393, 3.67223, 1.75058, 2.30061, 1.93212, -1.00784, -.53217,
-1.49742, .0818344, -.308665, -3.80073, -3.30265, -4.65006},
chisqr = 240.303, df = 49, p = 4.8*^-27. The plot is far from random.


  • Prev by Date: Re: Importing tab-delimited data files?
  • Next by Date: 3D Plot of frequency data
  • Previous by thread: Re: Random Normal deviates within compiled function?
  • Next by thread: Newbie with simple questions (take 2)