Verify distribution uniformity/Naive: Difference between revisions
Content deleted Content added
→{{header|VBScript}}: Added new VBScript section |
|||
Line 866: | Line 866: | ||
0 10003 1 9851 2 10058 3 10193 4 10126 5 10002 6 9852 7 9964 8 9957 9 9994 |
0 10003 1 9851 2 10058 3 10193 4 10126 5 10002 6 9852 7 9964 8 9957 9 9994 |
||
<span style="color:red">distribution potentially skewed for 0: expected around 50000, got 94873</span> |
<span style="color:red">distribution potentially skewed for 0: expected around 50000, got 94873</span> |
||
=={{header|VBScript}}== |
|||
<lang vb>Option Explicit |
|||
sub verifydistribution(calledfunction, samples, delta) |
|||
Dim i, n, maxdiff |
|||
'We could cheat via Dim d(7), but "7" wasn't mentioned in the Task. Heh. |
|||
Dim d : Set d = CreateObject("Scripting.Dictionary") |
|||
wscript.echo "Running """ & calledfunction & """ " & samples & " times..." |
|||
for i = 1 to samples |
|||
Execute "n = " & calledfunction |
|||
d(n) = d(n) + 1 |
|||
next |
|||
n = d.Count |
|||
maxdiff = 0 |
|||
wscript.echo "Expected average count is " & Int(samples/n) & " across " & n & " buckets." |
|||
for each i in d.Keys |
|||
dim diff : diff = abs(1 - d(i) / (samples/n)) |
|||
if diff > maxdiff then maxdiff = diff |
|||
wscript.echo "Bucket " & i & " had " & d(i) & " occurences" _ |
|||
& vbTab & " difference from expected=" & FormatPercent(diff, 2) |
|||
next |
|||
wscript.echo "Maximum found variation is " & FormatPercent(maxdiff, 2) _ |
|||
& ", desired limit is " & FormatPercent(delta, 2) & "." |
|||
if maxdiff > delta then wscript.echo "Skewed!" else wscript.echo "Smooth!" |
|||
end sub</lang> |
|||
Demonstration with included [[Seven-sided dice from five-sided dice#VBScript]] code: |
|||
<lang vb>verifydistribution "dice7", 1000, 0.03 |
|||
verifydistribution "dice7", 100000, 0.03</lang> |
|||
Which produces this output: |
|||
Running "dice7" 1000 times... |
|||
Expected average count is 142 across 7 buckets. |
|||
Bucket 2 had 150 occurences difference from expected=5.00% |
|||
Bucket 7 had 147 occurences difference from expected=2.90% |
|||
Bucket 6 had 146 occurences difference from expected=2.20% |
|||
Bucket 5 had 141 occurences difference from expected=1.30% |
|||
Bucket 1 had 152 occurences difference from expected=6.40% |
|||
Bucket 4 had 115 occurences difference from expected=19.50% |
|||
Bucket 3 had 149 occurences difference from expected=4.30% |
|||
Maximum found variation is 19.50%, desired limit is 3.00%. |
|||
Skewed! |
|||
Running "dice7" 100000 times... |
|||
Expected average count is 14285 across 7 buckets. |
|||
Bucket 5 had 14420 occurences difference from expected=0.94% |
|||
Bucket 4 had 14298 occurences difference from expected=0.09% |
|||
Bucket 2 had 14202 occurences difference from expected=0.59% |
|||
Bucket 7 had 14201 occurences difference from expected=0.59% |
|||
Bucket 6 had 14237 occurences difference from expected=0.34% |
|||
Bucket 3 had 14263 occurences difference from expected=0.16% |
|||
Bucket 1 had 14379 occurences difference from expected=0.65% |
|||
Maximum found variation is 0.94%, desired limit is 3.00%. |
|||
Smooth! |
|||
{{omit from|GUISS}} |
{{omit from|GUISS}} |