Statistics/Basic: Difference between revisions
Content deleted Content added
→{{header|Haskell}}: Circumvented formatting issue (half of code in red), supplied missing import |
|||
Line 2,493: | Line 2,493: | ||
0.8 ==================================================== |
0.8 ==================================================== |
||
0.9 ==================================================</pre> |
0.9 ==================================================</pre> |
||
=={{header|Phix}}== |
|||
{{trans|CoffeeScript}} |
|||
To do a trillion samples, I would change the existing generate loop into an inner 100_000_000 loop that still uses the fast native types, with everything outside that changed to bigatom, and of course add an outer loop which sums into them. |
|||
<lang Phix>function generate_statistics(integer n) |
|||
sequence hist = repeat(0,10) |
|||
atom sum_r = 0, |
|||
sum_squares = 0.0 |
|||
for i=1 to n do |
|||
atom r = rnd() |
|||
sum_r += r |
|||
sum_squares += r*r |
|||
hist[floor(10*r)+1] += 1 |
|||
end for |
|||
atom mean = sum_r / n |
|||
atom stddev = sqrt((sum_squares / n) - mean*mean) |
|||
return {n, mean, stddev, hist} |
|||
end function |
|||
procedure display_statistics(sequence x) |
|||
atom n, mean, stddev |
|||
sequence hist |
|||
{n, mean, stddev, hist} = x |
|||
printf(1,"-- Stats for sample size %d\n",{n}) |
|||
printf(1,"mean: %g\n",{mean}) |
|||
printf(1,"sdev: %g\n",{stddev}) |
|||
for i=1 to length(hist) do |
|||
integer cnt = hist[i] |
|||
string bars = repeat('=',floor(cnt*300/n)) |
|||
printf(1,"%.1f: %s %d\n",{i/10,bars,cnt}) |
|||
end for |
|||
end procedure |
|||
for n=2 to 5 do |
|||
display_statistics(generate_statistics(power(10,n+(n=5)))) |
|||
end for</lang> |
|||
{{Out}} |
|||
<pre style="float:left; font-size: 10px"> |
|||
-- Stats for sample size 100 |
|||
mean: 0.530925 |
|||
sdev: 0.303564 |
|||
0.1: ======================== 8 |
|||
0.2: ======================================= 13 |
|||
0.3: ============================== 10 |
|||
0.4: ================== 6 |
|||
0.5: ===================== 7 |
|||
0.6: ================================= 11 |
|||
0.7: ================================= 11 |
|||
0.8: ===================== 7 |
|||
0.9: ======================================= 13 |
|||
1.0: ========================================== 14 |
|||
</pre> |
|||
<pre style="float:left; font-size: 10px"> |
|||
-- Stats for sample size 1000 |
|||
mean: 0.50576 |
|||
sdev: 0.288862 |
|||
0.1: ============================ 95 |
|||
0.2: ============================== 103 |
|||
0.3: ============================= 98 |
|||
0.4: =========================== 93 |
|||
0.5: ============================== 101 |
|||
0.6: ============================= 99 |
|||
0.7: =============================== 105 |
|||
0.8: ============================= 97 |
|||
0.9: ================================ 108 |
|||
1.0: ============================== 101 |
|||
</pre> |
|||
<pre style="float:left; font-size: 10px"> |
|||
-- Stats for sample size 10000 |
|||
mean: 0.498831 |
|||
sdev: 0.28841 |
|||
0.1: ============================= 987 |
|||
0.2: =============================== 1060 |
|||
0.3: ============================ 953 |
|||
0.4: ============================= 980 |
|||
0.5: ============================== 1013 |
|||
0.6: ============================= 997 |
|||
0.7: ================================ 1089 |
|||
0.8: ============================ 948 |
|||
0.9: ============================= 974 |
|||
1.0: ============================= 999 |
|||
</pre> |
|||
<pre style="font-size: 10px"> |
|||
-- Stats for sample size 1000000 |
|||
mean: 0.499937 |
|||
sdev: 0.288898 |
|||
0.1: ============================== 100071 |
|||
0.2: ============================== 100943 |
|||
0.3: ============================= 99594 |
|||
0.4: ============================= 99436 |
|||
0.5: ============================= 99806 |
|||
0.6: ============================= 99723 |
|||
0.7: ============================== 100040 |
|||
0.8: ============================== 100280 |
|||
0.9: ============================== 100264 |
|||
1.0: ============================= 99843 |
|||
</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |