Anonymous user
Statistics/Normal distribution: Difference between revisions
m
→{{header|REXX}}: increased decimal digits, added whitespace and comments, elided 2nd graph, optimized some functions.
SqrtNegInf (talk | contribs) (Added Perl example) |
m (→{{header|REXX}}: increased decimal digits, added whitespace and comments, elided 2nd graph, optimized some functions.) |
||
Line 2,377:
=={{header|REXX}}==
The REXX language doesn't have any "higher math" BIF functions like SIN
<br>so we hoi polloi programmers have to roll our own.
<lang rexx>/*REXX program generates 10,000 normally distributed numbers (Gaussian distribution).*/
numeric digits 20 /*use 20 decimal digits for accuracy.*/
parse arg n seed . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n=
if datatype(seed, 'W') then call random ,,seed
call pi /*call subroutine to define pi constant*/
do g=1 for n; #.g= sqrt( -2 * ln( rand() ) ) * cos( 2 * pi * rand() )
end /*g*/ /* [↑] uniform random number ───► #.g */
s= 0
mn= #.1;
ss= 0
do j=1 for n; _=#.j; s=s+_; ss=ss+_*_ /*the sum, and the sum of squares. */
mn= min(mn, _);
end /*j*/
!.= 0
say 'number of data points = ' aa(n )
say ' minimum = ' aa(mn )
Line 2,398 ⟶ 2,399:
say ' arithmetic mean = ' aa(s/n)
say ' standard deviation = ' aa(sqrt( ss/n - (s/n) **2) )
?mn= !.1; ?mx= ?mn
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.*/
swE= sw - 1 /* " " " " width.*/
$= 1 / max(1, mx-mn) * sdE
do i=1 for n; ?= trunc( (#.i-mn) * $)
!.?= !.? + 1 /*bump the counter. */
?mn= min(?mn, !.?); ?mx= max(?mx, !.?)
end /*i*/
f=swE/?mx /*limit graph to 1 full screen*/
do h=0 for sdE; _= !.h
if _>noise then say copies('─', trunc(_*f) ) /*display a bar of histogram. */
end /*h*/ /*[↑] use a hyphen for histo.*/
Line 2,414 ⟶ 2,415:
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
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;
pi: pi= 3.1415926535897932384626433832795028841971693993751058209749445923078164062862;
r2r: return arg(1) // (pi() * 2)
rand: return random(1, 1e5) /
.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;
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
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
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
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
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*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; numeric digits d; return g/1</lang>
Line 2,435 ⟶ 2,437:
{{out|output|text= when using the default input:}}
<pre>
number of data points = 10000
minimum = -3.
maximum = 3.
arithmetic mean = -0.
standard deviation = 0.
─
─
───
────
─────
─────
────────
───────────
──────────────
─────────────────────
──────────────────────
──────────────────────────────────
────────────────────────────────────────
───────────────────────────────────────────────
─────────────────────────────────────────────────────
─────────────────────────────────────────────────────────────────────────
─────────────────────────────────────────────────────────────────
─────────────────────────────────────────────────────────────────────────────────────
──────────────────────────────────────────────────────────────────────────────────────────────────
──────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
──────────────────────────────────────────────────────────────────────────────────────────────────────────
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────────────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────
───────────────────────────────────────────────────────────────────────
───────────────────────────────────────────────────────────────────
─────────────────────────────────────────────────────
─────────────────────────────────────────────────
─────────────────────────────────
──────────────────────────────────
───────────────────────
──────────────────────
──────────────────
───────────
──────────
──────
───
────
──
─
</pre>
|