Re: 'erf' function in C

From: Deane Yang (yang2w_at_earthlink.net)
Date: 02/25/04


Date: Wed, 25 Feb 2004 03:20:45 GMT

Yes, the paper cited below is the one I used to implement
the inverse cumulative normal function. In that paper
you recommended that such a function be used to generate
random normally distributed numbers, instead of the Box-Muller
method.

1) Would you still recommend this?
2) The approach suggested below seems to be aiming more
for accuracy, rather than speed. On the other hand,
the published algorithm is machine-dependent and therefore
a bit of a headache to maintain.

Is there another fast but portable algorithm with sufficient
accuracy available?

George Marsaglia wrote:
> "Deane Yang" <deane_yang@yahoo.com> wrote in message
> news:36ab6f9c.0402221143.13262bfa@posting.google.com...
>
>>And what about the inverse cumulative normal distribution?
>>I used to implement an algorithm you published,
>>but do not like it because it is machine-dependent.
>>What in your opinion is the best algorithm?
>
>
> With the improvement on my method for
> evaluating cPhi(x) as R(x)*phi(x),
> when the Taylor series for R(x) is about zero,
> (suggested by Bill Daly), the simple C function
>
> double Phi(double x)
> { long double t=0,b=1,s=x,pwr=x;
> int i;
> for(i=2;s!=t;i+=2)
> { b/=(i+1); pwr=pwr*x*x; t=s; s+=pwr*b;}
> return .5+s*exp(-.5*x*x-.91893853320467274178L);
> }
>
> should serve very well for solving the equation
> Phi(X)=U
> for positive X, given U>1/2, by Newton's method:
> Start with an initial estimate
>
> x=sqrt(-1.6*ln(1-U^2));
>
> then repeat
>
> x=x-(Phi(x)-U)/phi(x);
>
> until you get no further changes in x.
>
>
> The paper that you refer to must be where I want
> to generate a normal X by means of solving
> Phi(X)=U, given a random U, uniform in [0,1),
> by getting an integer j, formed from certain
> bits of the exponent part of the floating point
> representation of U. Then use j to access tabled
> values A[j] and B[j] to initalize the Taylor series
> for Phi inverse, using the fractional part of U
> as the argument in the series. It is designed for
> speed in generating a normal variate directly
> as Phi^(-1)(U), but only provides accuracy to 6/7 digits,
> (As I recall, the Taylor series was easy and fast only for the first few
> terms.)
>
> It is described in
> ``Normal (Gaussian) random variables in supercomputers'', (1991)
> Journal of Supercomputing}, v 5, 49--55,
> where the method is particularly suited for parallel computation.
>
> George Marsaglia
>
>
>



Relevant Pages

  • Re: erf function in C
    ... > And what about the inverse cumulative normal distribution? ... > I used to implement an algorithm you published, ... should serve very well for solving the equation ... values Aand Bto initalize the Taylor series ...
    (comp.lang.c)
  • Re: 10 ten things any good programmer can/has done?
    ... > For learning the basics of functional programming, I recommend SICP: ... If anyone is interested in learning functional programming then I recommend ... Implement an algorithm from graphics. ...
    (comp.programming)
  • Re: Locating Nearest Neighbors in space (fast)
    ... The brute force algorithm that you described is O, ... so the nearest neighbour distribution is very wide. ... If your distribution of nearest neighbour separations is heterogeneous (like ... stars) then I'd recommend an octree or k-D tree (adaptively subdividing ...
    (comp.programming)
  • Re: Algorithm for calculating matrix exponential
    ... I am a programmer and I will like most of all to see an algorithm, ... Taylor series approach is probably a good one. ... diagonalizing the matrix should be considered. ...
    (sci.math)
  • Re: Best exe encryption software or method ?
    ... supported solution. ... I'd recommend some algorithm that obscures the password ...
    (borland.public.delphi.thirdpartytools.general)