Verify distribution uniformity/Naive: Difference between revisions

→‎{{header|REXX}}: changed comments and whitespace, changed some variable names to be more descriptive, added commas to output for easier reading the results.
No edit summary
(→‎{{header|REXX}}: changed comments and whitespace, changed some variable names to be more descriptive, added commas to output for easier reading the results.)
Line 1,459:
=={{header|REXX}}==
<lang rexx>/*REXX program simulates a number of trials of a random digit and show it's skew %. */
parse arg ffunc ttimes ddelta sseed . /*obtain arguments (options) from C.L. */
if f func=='' | f func=="," then f func= 'RANDOM' /*function not specified? Use default.*/
if ttimes=='' | ttimes=="," then ttimes= 1000000 /*times " " " " */
if ddelta=='' | ddelta=="," then d delta= 1/2 /*delta% " " " " */
if s\==datatype(seed, 'W' ) then call random ,,s seed /*use some RAND seed for repeatability.*/
highDig=9 /*use this var for the highest digit. */
!.=0 /*initialize all possible random trials*/
do t times /* [↓] perform a bunch of trials. */
if ffunc=='RANDOM' then ?=random(0,highDig) /*use the RANDOM BIF function.*/
else interpret '?='ffunc"(0,"highDig')' /*use the" (specified) " */
!.?=!.? +1 1 /*bump the invocation counter.*/
end /*t*/ /* [↑] store trials ───► pigeonholes. */
/* [↓] compute the digit's skewness. */
g=ttimes / (1 + highDig) /*calculate number of each digit throw.*/
OK?w=max(9, 'OK skewed' length( commas(times) ) ) /*words to showmaximum length "skewed"of ornumber if of "OK"trials.*/
w=max(8, length(t)) /*maximum length of number of trials.*/
pad=left('', 9) /*this is used for output indentation. */
say pad 'digit' center(" hits", w) ' skew ' "skew %" 'result' /*header. */
say pad '─────' center('', w, '─') '──────' "───────────" '──────' /*separator.*/
/** [↑] show header and the separator.*/
do k=0 to highDig /*process each of the possible digits. */
skew=g - !.k /*calculate the skew for the digit. */
skewPC=(1- (g-abs(skew)) / g) * 100 /* " " " percentage for dig*/
ok=say pad center(word(ok?k, 15) + (skewPC>d)), 6) /*it's gotta be one of skewed or xx%*/right( commas(!.k), w) right(skew, 6) ,
say pad centerright(k,5) rightformat(!.kskewPC,w) , 3), right(skew,6) center( formatword('ok skewed', 1+(skewPC>delta)),,3 6) ok
end /*k*/
 
say pad '─────' center('', w, '─') '──────' "─────" '──────' /*separator. */
y=5+1+w+1+6+1+6+1+6 /*the width. */
say pad center(" (with " commas(times) t' ' trials)' , y) /*# trials. */
say pad center(" (skewed when exceeds " d'%)',y) delta'%)' , y) /*skewed note.*/
exit /*stick a fork in it, we're all done. */</lang>
/*──────────────────────────────────────────────────────────────────────────────────────*/
Execution note: &nbsp; quite a few runs were needed and the skew'''%''' lowered before a skewed result was obtained.
commas: procedure; parse arg _; n=_'.9'; #=123456789
e=verify(n, #'0', , verify(n, #"0.",'M') ) - 4
do j=e to verify(n, #, "M") by -3; _=insert(',', _, j); end; return _</lang>
Execution note: &nbsp; quite a few runs were needed and the skew'''%''' lowered before a skewed result was obtained.
<br><br>
'''output''' when using the default inputs:
<pre>
digit hits skew skew % result
───── ───────────────── ────── ─────────── ──────
0 9975799,739 243261 0.243261 OKok
1 100226 99,819 -226 181 0.226181 OKok
2 100605100,463 -605463 0.605463 skewedok
3 100005 99,787 -5213 0.005213 OKok
4 9967099,632 330368 0.330368 OKok
5 100011 100,787 -11787 0.011 787 OKskewed
6 100100 99,704 -100 296 0.100296 OKok
7 9951399,605 487395 0.487395 OKok
8 100,488 99884 -488 116 0.116488 OKok
9 100229 99,976 -229 24 0.229024 OKok
───── ───────────────── ────── ───── ──────
(with 1000000 1,000,000 trials)
(skewed when exceeds 0.5%)
</pre>