Statistics/Basic: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: added/changed comments and whitespace, used templates for the OUTPUTs, simplified some parts of the program, expanded the width of the histograms (bars).) |
|||
Line 3,278: | Line 3,278: | ||
@.j=random(0, 99999) / 100000 /*express random number as a fraction. */ |
@.j=random(0, 99999) / 100000 /*express random number as a fraction. */ |
||
_=substr(@.j'00', 3, 1) /*determine which bin the number is in,*/ |
_=substr(@.j'00', 3, 1) /*determine which bin the number is in,*/ |
||
#._=#._+ |
#._=#._ + 1 /* ··· and bump its count. */ |
||
end /*j*/ |
end /*j*/ |
||
do k=0 for 10; kp=k + 1 /*show a histogram of the bins. */ |
|||
lr='0.'k ; if k==0 then lr= "0 " /*adjust for the low range. */ |
|||
hr='0.'kp ; if k==9 then hr= "1 " /* " " " high range. */ |
|||
barPC=right( strip( left( format( 100*#.k / size, , 2), 5)), 5) /*compute the %. */ |
|||
say lr"──►"hr' ' barPC copies("─", barPC * 2 % 1 ) /*show histogram.*/ |
|||
end /*k*/ |
|||
say |
say |
||
say 'sample size = ' size; say |
say 'sample size = ' size; say |
||
Line 3,293: | Line 3,293: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
mean: |
mean: arg N; $=0; do m=1 for N; $=$ + @.m; end; return $/N |
||
stdDev: |
stdDev: arg N; $=0; do s=1 for N; $=$ + (@.s-avg)**2; end; return sqrt($/N) /1 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6 |
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6 |
||
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_ % 2 |
numeric digits; 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*/; return g</lang> |
|||
⚫ | |||
return g/1</lang> |
|||
⚫ | |||
<pre> |
<pre> |
||
0 ──►0.1 12.00 |
0 ──►0.1 12.00 ──────────────────────── |
||
0.1──►0.2 |
0.1──►0.2 12.00 ──────────────────────── |
||
0.2──►0.3 |
0.2──►0.3 10.00 ──────────────────── |
||
0.3──►0.4 |
0.3──►0.4 8.00 ──────────────── |
||
0.4──►0.5 |
0.4──►0.5 12.00 ──────────────────────── |
||
0.5──►0.6 |
0.5──►0.6 8.00 ──────────────── |
||
0.6──►0.7 |
0.6──►0.7 11.00 ────────────────────── |
||
0.7──►0.8 |
0.7──►0.8 11.00 ────────────────────── |
||
0.8──►0.9 |
0.8──►0.9 6.00 ──────────── |
||
0.9──►1 |
0.9──►1 10.00 ──────────────────── |
||
sample size = 100 |
sample size = 100 |
||
mean = 0. |
mean = 0.4711358000 |
||
stdDev = 0. |
stdDev = 0.2920169478 |
||
</pre> |
</pre> |
||
{{out|output|text= when using the default input of: <tt> 1000 </tt>}} |
|||
<pre> |
<pre> |
||
0 ──►0.1 |
0 ──►0.1 9.50 ─────────────────── |
||
0.1──►0.2 |
0.1──►0.2 9.90 ─────────────────── |
||
0.2──►0.3 |
0.2──►0.3 11.70 ─────────────────────── |
||
0.3──►0.4 |
0.3──►0.4 8.80 ───────────────── |
||
0.4──►0.5 |
0.4──►0.5 8.40 ──────────────── |
||
0.5──►0.6 |
0.5──►0.6 10.20 ──────────────────── |
||
0.6──►0.7 10. |
0.6──►0.7 10.30 ──────────────────── |
||
0.7──►0.8 |
0.7──►0.8 11.40 ────────────────────── |
||
0.8──►0.9 |
0.8──►0.9 9.10 ────────────────── |
||
0.9──►1 10. |
0.9──►1 10.70 ───────────────────── |
||
sample size = 1000 |
sample size = 1000 |
||
mean = 0. |
mean = 0.5037752500 |
||
stdDev = 0. |
stdDev = 0.2886365539 |
||
</pre> |
</pre> |
||
{{out|output|text= when using the default input of: <tt> 10000 </tt>}} |
|||
<pre> |
<pre> |
||
0 ──►0.1 |
0 ──►0.1 9.61 ─────────────────── |
||
0.1──►0.2 |
0.1──►0.2 10.45 ──────────────────── |
||
0.2──►0.3 |
0.2──►0.3 9.96 ─────────────────── |
||
0.3──►0.4 |
0.3──►0.4 10.56 ───────────────────── |
||
0.4──►0.5 9. |
0.4──►0.5 9.91 ─────────────────── |
||
0.5──►0.6 10. |
0.5──►0.6 10.13 ──────────────────── |
||
0.6──►0.7 10. |
0.6──►0.7 10.12 ──────────────────── |
||
0.7──►0.8 |
0.7──►0.8 9.84 ─────────────────── |
||
0.8──►0.9 9. |
0.8──►0.9 9.61 ─────────────────── |
||
0.9──►1 |
0.9──►1 9.81 ─────────────────── |
||
sample size = 10000 |
sample size = 10000 |
||
mean = 0. |
mean = 0.4968579550 |
||
stdDev = 0. |
stdDev = 0.2863756713 |
||
</pre> |
</pre> |
||
{{out|output|text= when using the default input of: <tt> 100000 </tt>}} |
|||
<pre> |
<pre> |
||
0 ──►0.1 10. |
0 ──►0.1 10.13 ──────────────────── |
||
0.1──►0.2 |
0.1──►0.2 9.84 ─────────────────── |
||
0.2──►0.3 9. |
0.2──►0.3 9.91 ─────────────────── |
||
0.3──►0.4 |
0.3──►0.4 9.94 ─────────────────── |
||
0.4──►0.5 |
0.4──►0.5 10.19 ──────────────────── |
||
0.5──►0.6 10. |
0.5──►0.6 10.08 ──────────────────── |
||
0.6──►0.7 |
0.6──►0.7 10.12 ──────────────────── |
||
0.7──►0.8 |
0.7──►0.8 9.78 ─────────────────── |
||
0.8──►0.9 10. |
0.8──►0.9 10.07 ──────────────────── |
||
0.9──►1 9. |
0.9──►1 9.95 ─────────────────── |
||
sample size = 100000 |
sample size = 100000 |
||
mean = 0. |
mean = 0.4999883642 |
||
stdDev = 0. |
stdDev = 0.2884109515 |
||
</pre> |
</pre> |
||
{{out|output|text= when using the default input of: <tt> 1000000 </tt>}} |
|||
<pre> |
<pre> |
||
0 ──►0.1 9. |
0 ──►0.1 9.94 ─────────────────── |
||
0.1──►0.2 10. |
0.1──►0.2 10.03 ──────────────────── |
||
0.2──►0.3 |
0.2──►0.3 10.03 ──────────────────── |
||
0.3──►0.4 9. |
0.3──►0.4 9.98 ─────────────────── |
||
0.4──►0.5 10. |
0.4──►0.5 10.00 ──────────────────── |
||
0.5──►0.6 10. |
0.5──►0.6 10.03 ──────────────────── |
||
0.6──►0.7 9. |
0.6──►0.7 9.99 ─────────────────── |
||
0.7──►0.8 10.03 |
0.7──►0.8 10.03 ──────────────────── |
||
0.8──►0.9 9. |
0.8──►0.9 9.97 ─────────────────── |
||
0.9──►1 9.99 |
0.9──►1 9.99 ─────────────────── |
||
sample size = 1000000 |
sample size = 1000000 |
||
mean = 0. |
mean = 0.5000687045 |
||
stdDev = 0. |
stdDev = 0.2885125537 |
||
</pre> |
</pre> |
||