Re: strange problems with Random

*To*: mathgroup at smc.vnet.net*Subject*: [mg47830] Re: strange problems with Random*From*: "Jens-Peer Kuska" <kuska at informatik.uni-leipzig-de>*Date*: Thu, 29 Apr 2004 00:33:48 -0400 (EDT)*Organization*: Uni Leipzig*References*: <c6l8je$iud$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Hi Andrzej, as to see from Statistics`NormalDistribution` Mathamtica use the direct method with the two uniform variables U1=Random[] and U2=Random[] and compute X1=Sqrt[- 2 Log[U1]]*Cos[2 Pi U2] while RamdomArray[] use the pairs X1=Sqrt[- 2 Log[U1]]*Cos[2 Pi U2], X2=Sqrt[- 2 Log[U1]]*Sin[2 Pi U2] but always Random[] without any paramters is used. May be that using always the version with Cos[] in the equations above cause your problems (but it should not). The mean, variance, skewness of Mathematicas normal distribution look fine and the histogram for 10^6 values show also a fine Gauss curve. Regards Jens "Andrzej Kozlowski" <akoz at mimuw.edu.pl> schrieb im Newsbeitrag news:c6l8je$iud$1 at smc.vnet.net... > Recently Mark Coleman wrote a message with the subject "Differences in > Random Numbers" in which he described his experience of getting > statistically incorrect answers when using > Random[UniformDistribution[0,1]] instead of simple Random[]. Jens Kuska > and Bill Rowe pointed out that the package ContinuousDistributions > contains the code > > UniformDistribution/: Random[UniformDistribution[min_:0, max_:1]] := > Random[Real, {min, max}] > > and that seemed to be it. However, I have now encountered a problem > that makes me feel strongly that there is more to this problem than it > seemed to me at first. > > I have been doing some Monte Carlo computations of values of of > financial options for the Internet based course on this subject I am > teaching for Warsaw University. In the case of the simplest options, > the so called vanilla european options, an exact formula (due to black > and Scholes) is known. Neverheless, computations with Mathematica using > the function > > Random[NormalDistribution[mu,sigma]] (for some fixed mu and sigma) have > been giving answers which are consistently higher than the > Black-Scholes value. On the other hand, when > Random[NormalDistribution[mu,sigma]] is replaced by the following code > using a well known algorithm due to Marsaglia: > > RandomNormal=Compile[{mu, sigma}, Module[{va = 1., vb, rad = > 2.0, den = 1.}, While[rad ³ 1.00, (va = 2.0*Random[] - 1.0; > vb = 2.0*Random[] - 1.0; > rad = va*va + vb*vb)]; > den = Sqrt[-2.0*Log[rad]/rad]; > mu + sigma*va*den]]; > > the answers agree very closely with the Black-Scholes ones. > I am not sure if this problem is related to the one mark mentioned but > it seems quite likely. One could in principle test it by replacing > Random[] by Random[Uniformdistribution[0,1]] (after loading the > Statistics`ContinuousDistributions`) package, but if one does that the > code will no longer compile. That makes the function to slow for tests > that are sufficiently precise, so I have not tested it. > > In any case, I am sure that using Random[Normal ] consistently gives > wrong answers. I looked at the Statistics`NormalDistributionPackage` > but everything looks O.K. to me. I now tend to believe that the problem > lies with Random itself, that is that Random called with any > parameters or perhaps some particular parameters may not be behaving > correctly. Random[] called with no parameters does not seem to suffer > from any such problems. > > > Andrzej Kozlowski > Chiba, Japan > http://www.mimuw.edu.pl/~akoz/ >