Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!torn!utcsri!rpi!uwm.edu!spool.mu.edu!sdd.hp.com!decwrl!csus.edu!sfsuvax1.sfsu.edu!emclean
From: emclean@sfsuvax1.sfsu.edu (Emmett McLean)
Subject: C prog for cumulative normal dist translated into J
Message-ID: <1993Feb3.073347.19716@csus.edu>
Sender: news@csus.edu
Organization: San Francisco State University
Distribution: NA
Date: Wed, 3 Feb 1993 07:33:47 GMT
Lines: 133

NB. A new J programmer would like the following C program 
NB. translated so he can sleep at night -:)
NB. 
NB. /**************************************************************************/
NB. /**                                                                      **/
NB. /**   N(x) the univariate cummulative normal distribution                **/
NB. /**                                                                      **/
NB. /**************************************************************************/
NB. 
NB. double N(double d)
NB. {
NB.     double C0 =  0.2316419,
NB.     C1 =  0.31938153,		/*  Constants for distribution  */
NB.     C2 =  -0.356563782,
NB.     C3 =  1.781477937,
NB.     C4 = -1.821255978,
NB.     C5 =  1.330274429;
NB. 
NB.     double x, y, z;		/* Intermediate results*/
NB. 
NB.     x     = exp (- pow(d,2.) / 2.) / sqrt( 2. * PI);
NB.     y     = (C0 * fabs(d)) + 1.;
NB.     y     = 1. / y;
NB.     z     = C1*y + C2*pow(y,2.) + C3*pow(y,3.) + C4*pow(y,4.) + C5*pow(y,5.);
NB. 
NB.     if (d < 0) return(x * z);
NB.     else return(1. - x * z);
NB. 
NB. }
 
   NB. Here are four translations 

   NB. Ntacit1,Ntacit0,Nexplicit0 and Nexplicit1

   NB. Ntacit1 is an entirely a tacit solution 
   NB. BTW, how did you get C0 and C12345 ?
   C0 =. 0.231642
   C12345 =. '0.319382 _0.356564 1.781478 _1.821256 1.330274':11
   xt =. ^@-@-:@*: % %:@+:@o.@1:
   yt =. %@>:@(C0"_ * |)
   zt =. +/@(C12345 * 1 2 3 4 5&(^~)@yt)
   Ntacit0 =. (1: - xt * zt)`(xt * zt)@.(0&>)

   NB. if we want to define Ntacit0 in indigenous J only then
   Ntacit1 =. (1: - (xt f.) * (zt f.))`((xt f.) * (zt f.))@.(0&>)
 
   p =. i. 0 0
   p =. p,' NB. N0 is an explicit verb preserving the original way of'
   p =. p,' NB. of thinking of the problem while thinking in J'
   p =. p,' NB. N0 returns the univariate cumulative normal value of x'
   p =. p,' NB. emclean 2/2/93'
   p =. p,' d =. y.'
   p =. p,' C0 =. 0.231642'
   p =. p,' C12345 =. 0.319382 _0.356564 1.781478 _1.821256 1.330274'
   p =. p,' SQRT2PI =. %: +: o. 1'
   p =. p,' x =. (^@-@-:@*: y.) % SQRT2PI'
   p =. p,' NB. x =. (^@-@-:@*: % %:@+:@o.@1:) d'
   p =. p,' y =. % >: C0 * | d'
   p =. p,' z =. +/ C12345 * (y^1 2 3 4 5)'
   p =. p,' $. =. s1 }.~ 0 > d'
   p =. p,' s1) y. =. x*z'
   p =. p,'     y. =. 1 - x*z'
   Nexplicit0 =. p : ''
   
   p =. i. 0 0
   p =. p,' NB. N is an explicit literal translation of the C code'
   p =. p,' NB. N returns the univariate cumulative normal value of x'
   p =. p,' NB. emclean 2/2/93'
   p =. p,' d  =. y.'
   p =. p,' C0 =.  0.2316419'
   p =. p,' C1 =.   0.31938153'
   p =. p,' C2 =.  -0.356563782'
   p =. p,' C3 =.  -0.356563782'
   p =. p,' C3 =.  1.781477937'
   p =. p,' C4 =. -1.821255978'
   p =. p,' C5 =.  1.330274429'
   p =. p,' PI =.  3.141593'
   p =. p,' exp =. pow =. ^   '
   p =. p,' fabs =. |  '
   p =. p,' sqrt =. %: '
   p =. p,' pow =. ^  '
   p =. p,' sumscan =. +/'
   p =. p,' NB. x =   exp  (- pow(d,2.) / 2.   /  sqrt( 2. * PI); '
   p =. p,'     x =. (exp (- (d pow 2) % 2.0)) % (sqrt 2.0 * PI) '
   p =. p,' NB. y =  (C0 * fabs(d)) + 1.;'
   p =. p,'     y =. (C0 * fabs d) + 1.0' 
   p =. p,' NB.  y     =  1.0 / y;'
   p =. p,'      y     =. 1.0 % y'
   p =. p,' NB. z= C1*y+C2*pow(y,2.)+C3*pow(y,3.)+C4*pow(y,4.)+C5*pow(y,5.);'
   p =. p,' z =. sumscan (C1,C2,C3,C4,C5) * y ^ 1 2 3 4 5'
   p =. p,'NB.   if (d < 0) return(x * z);'
   p =. p,'NB.   else return(1. - x * z); '
   p =. p,'y. =. (x*z) (1: - [)`([) @.  (0&>@]) d'
   Nexplicit1 =. p : ''
   4!:55 <'p'

   ((,.Ntacit0"0);(,.Ntacit1"0);(,.Nexplicit0"0);(,.Nexplicit1"0))1-0.2*i.@-10
NB. +-------------+-------------+-------------+-------------+
NB. |_0.8 0.211855|_0.8 0.211855|_0.8 0.788145|_0.8 0.211855|
NB. |_0.6 0.274253|_0.6 0.274253|_0.6 0.725747|_0.6 0.274253|
NB. |_0.4 0.344578|_0.4 0.344578|_0.4 0.655422|_0.4 0.344578|
NB. |_0.2  0.42074|_0.2  0.42074|_0.2  0.57926|_0.2  0.42074|
NB. |   0      0.5|   0      0.5|   0      0.5|   0      0.5|
NB. | 0.2  0.57926| 0.2  0.57926| 0.2  0.57926| 0.2  0.57926|
NB. | 0.4 0.655422| 0.4 0.655422| 0.4 0.655422| 0.4 0.655422|
NB. | 0.6 0.725747| 0.6 0.725747| 0.6 0.725747| 0.6 0.725747|
NB. | 0.8 0.788145| 0.8 0.788145| 0.8 0.788145| 0.8 0.788145|
NB. |   1 0.841345|   1 0.841345|   1 0.841345|   1 0.841345|
NB. +-------------+-------------+-------------+-------------+
NB. NeXT avg cpu time over 15 executions (yes looks like tacit is faster)
NB. 
NB.    (15) 6!:2 'Ntacit0"0 (1-0.2*i.@-10)'
NB. 0.082185
NB.    (15) 6!:2 'Ntacit1"0 (1-0.2*i.@-10)'
NB. 0.079114
NB.    (15) 6!:2 'Nexplicit0"0 (1-0.2*i.@-10)'
NB. 0.230665
NB.    (15) 6!:2 'Nexplicit1"0 (1-0.2*i.@-10)'
NB. 0.354288

NB.   ULTRIX avg cpu time over 15 executions 
NB.       (15) 6!:2 'Ntacit0"0 (1-0.2*i.@-10)'
NB. 0.0933333
NB.       (15) 6!:2 'Ntacit1"0 (1-0.2*i.@-10)'
NB. 0.102
NB.       (15) 6!:2 'Nexplicit0"0 (1-0.2*i.@-10)'
NB. 0.215333
NB.       (15) 6!:2 'Nexplicit1"0 (1-0.2*i.@-10)'
NB. 0.370667
NB. Looks like the differences between Ntacit0 and Ntacit1 may
NB. not be statistically significant
    <' Emmett'
 
