Statistics/Normal distribution: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed whitespace in the output section. used a template for the output.)
m (→‎{{header|REXX}}: changed comments and whitespace, optimized some computations.)
Line 2,314: Line 2,314:
if datatype(seed,'W') then call random ,,seed /*seed is for repeatable RANDOM numbers*/
if datatype(seed,'W') then call random ,,seed /*seed is for repeatable RANDOM numbers*/
call pi /*call subroutine to define pi constant*/
call pi /*call subroutine to define pi constant*/
do g=1 for n /*generate N uniform random numbers. */
do g=1 for n; #.g= sqrt( -2 * ln( rand() ) ) * cos( 2 * pi * rand() )
#.g=sqrt(-2*ln(rand()))*cos(2*pi*rand()) /*assign a uniform random number to #. */
end /*g*/ /* [↑] uniform random number ───► #.g */
s=0
end /*g*/
mn=#.1; mx=mn; noise=n * .0005 /*calculate the noise: 1/20th % of N.*/

ss=0
mn=#.1; mx=mn; s=0; ss=0; noise=n*.0005 /*calculate the noise: 1/20th % of N.*/

do j=1 for n; _=#.j; s=s+_; ss=ss+_*_ /*the sum, and the sum of squares. */
do j=1 for n; _=#.j; s=s+_; ss=ss+_*_ /*the sum, and the sum of squares. */
mn=min(mn,#.j); mx=max(mx,#.j) /*find the minimum and the maximum. */
mn=min(mn, _); mx=max(mx, _) /*find the minimum and the maximum. */
end /*j*/
end /*j*/
!.=0
!.=0
Line 2,328: Line 2,327:
say ' maximum = ' aa(mx )
say ' maximum = ' aa(mx )
say ' arithmetic mean = ' aa(s/n)
say ' arithmetic mean = ' aa(s/n)
say ' standard deviation = ' aa(sqrt(ss/n - (s/n)**2) )
say ' standard deviation = ' aa(sqrt( ss/n - (s/n) **2) )
?mn=!.1; ?mx=?mn /*define minimum & maximum value so far*/
?mn=!.1; ?mx=?mn /*define minimum & maximum value so far*/
parse value scrSize() with sd sw . /*obtain the (true) screen size of term*/ /*◄──not all REXXes have this BIF*/
parse value scrSize() with sd sw . /*obtain the (true) screen size of term*/ /*◄──not all REXXes have this BIF*/
sdE=sd-4 /*the effective (useable) screen depth.*/
sdE= sd - 4 /*the effective (useable) screen depth.*/
swE=sw-1 /* " " " " width.*/
swE= sw - 1 /* " " " " width.*/
$=1 / max(1,mx-mn) * sdE /*$ is used for scaling depth of histo*/
$=1 / max(1, mx-mn) * sdE /*$ is used for scaling depth of histo*/
do i=1 for n; ?=trunc((#.i-mn) * $) /*calculate the relative line.*/
do i=1 for n; ?=trunc( (#.i-mn) * $) /*calculate the relative line.*/
!.?=!.?+1 /*bump the counter. */
!.?= !.? + 1 /*bump the counter. */
?mn=min(?mn, !.?); ?mx=max(?mx, !.?) /*find the minimum and maximum*/
?mn=min(?mn, !.?); ?mx=max(?mx, !.?) /*find the minimum and maximum*/
end /*i*/
end /*i*/
f=swE/?mx /*limit graph to 1 full screen*/
f=swE/?mx /*limit graph to 1 full screen*/
do h=0 for sdE; _=!.h /*obtain a data point. */
do h=0 for sdE; _=!.h /*obtain a data point. */
if _>noise then say copies('─', trunc(_*f)) /*display a bar of histogram. */
if _>noise then say copies('─', trunc(_*f) ) /*display a bar of histogram. */
end /*h*/ /*[↑] use a hyphen for histo.*/
end /*h*/ /*[↑] use a hyphen for histo.*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
aa: parse arg a; return left('',(a>=0)+2*datatype(a,'W'))a /*prepend a blank if #>=0, add two blanks if its whole*/
aa: parse arg a; return left('', (a>=0) + 2 * datatype(a, 'W'))a /*prepend a blank if #>=0, add 2 blanks if whole.*/
e: e =2.7182818284590452353602874713526624977572470936999595749669676277240766303535; return e
e: e =2.7182818284590452353602874713526624977572470936999595749669676277240766303535; return e
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862; return pi
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862; return pi
rand: return random(, 1e5) / 1e5 /*REXX generates uniform random postive integers.*/
r2r: return arg(1) // (pi() * 2) /*normalize the given angle (in radians) to ±2pi.*/
r2r: return arg(1) // (pi()*2) /*normalize the given angle (in radians) to ±2pi.*/
rand: return random(1, 1e5) / 1e5 /*REXX generates uniform random postive integers.*/
.sincos: parse arg z,_,i; x=x*x; p=z; do k=2 by 2; _=-_*x/(k*(k+i)); z=z+_; if z=p then leave; p=z; end; return z
.sincos: parse arg z,_,i; x=x*x; p=z; do k=2 by 2; _=-_*x/(k*(k+i)); z=z+_; if z=p then leave; p=z; end; return z
ln: procedure; parse arg x,f; call e; ig=x>1.5; is=1-2*(ig\==1); ii=0; xx=x; return .ln()
ln: procedure; parse arg x,f; call e; ig=x>1.5; is=1-2*(ig\==1); ii=0; xx=x; return .ln()
.ln: do while ig&xx>1.5|\ig&xx<.5;_=e; do k=-1;iz=xx*_**-is;if k>=0&(ig&iz<1|\ig&iz>.5) then leave; _=_*_;izz=iz; end
.ln: do while ig&xx>1.5|\ig&xx<.5;_=e; do k=-1;iz=xx*_**-is;if k>=0&(ig&iz<1|\ig&iz>.5) then leave; _=_*_;izz=iz; end
xx=izz;ii=ii+is*2**k;end; x=x*e**-ii-1;z=0;_=-1;p=z; do k=1;_=-_*x;z=z+_/k;if z=p then leave;p=z;end; return z+ii
xx=izz;ii=ii+is*2**k;end; x=x*e**-ii-1;z=0;_=-1;p=z; do k=1;_=-_*x;z=z+_/k;if z=p then leave;p=z;end; return z+ii
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/

cos: procedure; parse arg x; x=r2r(x); a=abs(x); hpi=pi*.5; numeric fuzz min(6,digits()-3); if a=pi() then return -1
cos: procedure; parse arg x; x=r2r(x); a=abs(x); hpi=pi*.5; numeric fuzz min(6,digits()-3); if a=pi() then return -1
if a=hpi|a=hpi*3 then return 0; if a=pi()/3 then return .5; if a=pi()*2/3 then return -.5; return .sinCos(1,1,-1)
if a=hpi|a=hpi*3 then return 0; if a=pi()/3 then return .5; if a=pi()*2/3 then return -.5; return .sinCos(1,1,-1)
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/

sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric digits; numeric form; h=d+6
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric digits; numeric form; h=d+6
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2; do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2; do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/