home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / etc / scams / dicebias.txt < prev    next >
Encoding:
Text File  |  2003-06-11  |  34.3 KB  |  749 lines

  1.  
  2. The following information is intended for distribution over Internet,
  3. and outside of that may be copied for personal use only. 
  4. (c) Glen L. Barnett, 1992. All rights reserved.
  5.  
  6. I recently responded to a thread on rec.games.frp.dnd about testing dice,
  7. in order to fix up a few misconceptions and make some suggestions. On the
  8. suggestion of Coyt Watters I'm putting this on r.g.f.archives, which I think
  9. is a good idea. What follows has some major additions, however.
  10.  
  11. In this post, I will begin by discussing various posts on the subject
  12. of testing dice, then show how to do the test under discussion in the
  13. thread (the chi-squared goodness-of-fit test) properly, and then talk 
  14. about more appropriate tests.
  15.  
  16. ---------------------------------------------------------------------
  17.  
  18. In article <BxHJLy.Jq0@watserv1.uwaterloo.ca>,
  19. alongley@cape.UWaterloo.ca (Allan Longley) writes:
  20.  [..stuff deleted..]
  21. >testing dice for bias.  Well, here is a test to use.  I haven't actually 
  22. >tested this yet, but it should -- in theory -- work.  And no, this is not
  23. >a copy out of the old Dragon magazine, but it is the same test -- its a
  24. >pretty standard test.  I will use simple terms -- so all you math/stat
  25. >people out there, don't correct the fine points, I know.
  26.  [description of chi-squared goodness-of-fit test deleted]
  27.  
  28. Since Allan asks for no correction of fine points, I will attempt to
  29. limit myself to major problems. This is not intended as a flame on
  30. Allan, but this is fairly important stuff, and should be explained 
  31. correctly. If at any stage I get less than pleasant, please accept 
  32. my apology in advance.
  33.  
  34. While the calculations that Allan describes give the correct value
  35. of a chi-square goodness-of-fit statistic (which he calls "Indicator"),
  36. you should be *very* wary of interpreting the results in the way 
  37. he describes, as I will explain:
  38.  
  39. Let us assume you have 40 dice that (unknown to you) are all perfectly 
  40. fair, and you wish to test all of them, to see if any are "biased".
  41.  
  42. The way Allan has set his test up, you'd expect 2 of them to give
  43. results below "Probably Fair", which he says indicates the die is 
  44. probably unbiased. That is, you have 40 fair dice, and you will 
  45. expect to regard only *two* of them as probably O.K.! Similarly,
  46. you will expect to consider two of your "purely fair" dice as
  47. probably unfair. Of the remaining 36, you will expect 18 scores
  48. between "Probably Fair" and "Maybe" and 18 more between "Maybe"
  49. and "Probably biased". For these 36, you have to do the test again,
  50. under Allan's scheme. If you get both results below "Maybe" (you expect
  51. 9 of these) you say "Probably Fair". Similarly you expect 9 above
  52. "Maybe" on both trials.
  53.  
  54.  
  55. So we have (after repeating the test for 90% of the dice):
  56.  
  57. Number of Fair Dice: 40
  58. Expected number "probably fair": 11
  59. Expected number "probably biased": 11
  60. Expected number which we don't know about: 18.
  61.  
  62. So over a quarter of perfectly fair dice will be called "probably biased".
  63. If we continue testing those remaining 18 we are still undecided about,
  64. the problem gets worse.
  65.  
  66.  
  67. Other problems:
  68.  
  69. Allan says:
  70.  
  71. "The column titled "Maybe" are the Indicator values where there is
  72. a 50% chance that the die is fair and a 50% chance that the die is 
  73. biased." (A) 
  74.  
  75. This is just plain wrong.
  76.  
  77. The column he refers to is the value that a test on a *fair* die will 
  78. exceed 50% of the time. This is very different, and probably explains 
  79. why Allan misunderstands the whole interpretation of the results. (B)
  80.  
  81. If any of you can't see why what I said (B), and what Allan said (A) are 
  82. totally different, don't despair. This stuff is not always obvious from 
  83. the start. If you can follow the rules of an average RPG, you are smart 
  84. enough to understand a few non-trivial statistical ideas. I'm quite happy 
  85. to provide further clarification to the net if the demand is there.
  86.  
  87. > >From Table 1, it appears that the d4 tested may be "Fair" but another test
  88. > should be done.
  89.  
  90. I'd say not. A reasonable interpretation of the result is "There is
  91. no reason to doubt that the die is O.K.".
  92.  
  93. Incidentally, the test statistic of 2.00 obtained in the example is
  94. only 2/3 of what you'd expect with a fair die. The value of 2.00 will 
  95. be exceeded almost 60% of the time by a test on a fair die.
  96.  
  97.  
  98. ---------------------------------------------------------------------
  99.  
  100. In article <Bxu26A.3F6@watserv1.uwaterloo.ca>,
  101. alongley@cape.UWaterloo.ca (Allan Longley), in response
  102. to Michael Wright, says:
  103.  
  104. |In article <wright.721879981@latcs1.lat.oz.au|wright@latcs2.lat.oz.au
  105. | (Michael G. Wright) writes:
  106. |>dks@acpub.duke.edu writes:
  107. |>
  108. |>>     This is called a chi-square test, and an article with the
  109. |>>procedure and numbers for it appeared way back in Dragon issue 
  110. |>>#74...  Thank you, Mr. Longley, for reposting it (or did you come
  111. |>>up with it in isolation? =) ) for the benefit of those who don't
  112. |>>have the issue (probably most readers).
  113. |
  114. |Yes, I seen the issue.  The chi-square test is a standard statistical test
  115. |for determinig if a data set matches a particular distribution -- so, no, I
  116. |did not come up with the test in isolation, its been around for a lot longer 
  117. |than D&D.  I don't actually have the issue, so I didn't copy it for the net.
  118. |
  119. |I've been playing with the chi-square test and you know what I found out --
  120. |ALL DICE ARE BIASED!!  Well, that's not true -- all except d4's and d6's are
  121. |biased.  Of course, this really shouldn't be a surprise.  So, I've been 
  122. |looking at modifiying the chi-square test for "real world" dice -- more on
  123. |this in a later post.
  124.  
  125. Allan is correct that the test has been around a lot longer than D&D.
  126. The test, due to Karl Pearson, is nearly 100 years old. 
  127.  
  128. Its no surprise that Allan finds that all real dice are biased:
  129.  i) Its impossible to make a truly fair die (obviously). Its just
  130.     that most are close enough that we don't care too much. The
  131.     chance of getting "close to fair" will decrease with the number
  132.     of sides.
  133.  
  134. ii) Allan's testing method will call more than a quarter of fair
  135.     dice (assuming they existed) biased. Even the fairest dice you
  136.     could buy have a good chance of being called biased.
  137.  
  138.  
  139. |>Actually, I use a program I made to roll dice for stats. Unfortunately, nobody
  140. |>in the party wants to use it, because dice rolls invariably end up better. I
  141. |>think this must be because of the pseudo-randomness of the program. 
  142. |>Anyone out there that knows better?
  143. |
  144. |I wouldn't want to use a computer generated die-value while playing AD&D.
  145. |THe thrill of the "rolling die" is part of the game.  Also, with reference
  146. |to the above, most players will have a favourite die/dice due to the
  147. |inherent bias found in real dice.  The trick is to find the dice that are
  148. |biased beyond reasonable playability.
  149.  
  150. In response to Michael G Wright:
  151.  
  152. Michael's discovery that hand-rolled dice often come out better
  153. could occur for a couple of reasons: 
  154.  
  155.  i) Players tend to hang on to "favourite" dice that "roll well"
  156.     (i.e. come up with good results). So biased dice have some 
  157.     chance of concentrating into the hands of players. As long as
  158.     this isn't too extreme, it probably doesn't matter too much.
  159.     (Allan quite correctly identifies this reason).
  160.  
  161. ii) Players don't really roll randomly. I had a fairly long email 
  162.     discussion with Sea Wasp on this topic just recently. Even 
  163.     unconciously, you can pick up the dice in a "non-random" fashion,
  164.     so that a good roll will tend to be followed by another good
  165.     roll if you don't roll tooo vigorously. You may notice that
  166.     after a bad roll players tend to throw harder. This may be more
  167.     of a problem, as some players are *much* better at it than others.
  168.     If it becomes too noticeable, you may wish to invest in a dice
  169.     cup, or mock up a craps-table affair.
  170.  
  171. Allan's comment that "The trick is to find the dice that are biased
  172. beyond reasonable playability" is spot on. Exactly correct. 
  173. Remember it, because I'll come back to it later.
  174.  
  175.     
  176. I think the above discussion also answers Paul Kinsler's questions.
  177.  
  178. [in article <BxvuzM.CsI@bunyip.cc.uq.oz.au>, kinsler@physics.uq.oz.au 
  179.  (Paul Kinsler) asked for clarification of Allan's comment that all
  180.  dice are biased].
  181.  
  182. ---------------------------------------------------------------------
  183.  
  184. In article <1992Nov12.194015.1602@Princeton.EDU>,
  185.  dagolden@phoenix.Princeton.EDU (David Alexandre Golden) writes:
  186.  
  187. [stuff deleted]
  188. >I once did something along those lines to test whether my DM's die was
  189. >fair.  (It would roll 20's a lot.  Often on command.)
  190. >
  191. >To test the die, I made a histogram (I believe is the term for it) like this:
  192. >
  193. >x  x  x        x        x
  194. >x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x
  195. >x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x
  196. >1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
  197. >
  198. >With an "x" being each time that number came up.  A totally fair die would
  199. >have a straight line across, assuming it was rolled enough times.  The
  200. >question is, what is enough times?  I rolled a d20 about 380 times (not bad
  201. >if one person rolls and the other makes an x in the column) and while it
  202. >gave a fairly good idea that the die was biased, the biased numbers had only
  203. >occured a couple times more than the average.  (If I remember correctly).
  204. >So I wrote a computer program to do the same thing until the deviation between
  205. >the highest and lowest number of occurances was less than about 15% of the
  206. >average number of occurances.  (i.e a reasonably smooth profile).  The 
  207. >computer required SEVERAL THOUSAND ROLLS to do this. 
  208.  
  209. Your idea of making a histogram is a good one. In fact all the
  210. chi-squared test does is the same as drawing a line across the 
  211. histogram where your expected "straight line across" would go, 
  212. looking at the deviations from that, squaring (to get all positives)
  213. and adding the squared deviations up and dividing by that expected 
  214. number. This gives a single overall measure of deviation from uniformity. 
  215. The advantage of looking at the histogram is you see where
  216. the the differences are, but you can't tell how big they "ought" to be
  217. for a fair die. The actual number in the cell will be approximately normally
  218. distributed with mean equal to the expected number in each cell and standard
  219. deviation approximately the square root of the mean. In the above example,
  220. we'd expect Dave to get 19 in each cell, so the standard deviation is about
  221. 4.35. That is, we'd expect to get about 2/3 of the cells with counts in
  222. the range 15 to 23, and about a 2/3 chance of all but 1 or 2 of the values
  223. inside the range 11 to 27.
  224.  
  225.    [ some of my own discussion deleted - see the section "Tests based
  226.      on the histogram" below]
  227.  
  228. >                           ...         Still, the point is that I'm skeptical
  229. >that the "fairness" of a die can be determined in only a hundred or so rolls.
  230. >(d4 maybe... d20 no way!)
  231.  
  232. Well, in fact Allan's suggestion was to use 20 rolls per cell, so he'd use
  233. 400 rolls for a d20 and 80 rolls for a d4. But in any case you can never
  234. decide that a die is actually fair. If you do a test and get a result
  235. close to what you expected if the die was fair, you have a lack of evidence
  236. against the hypothesis of fairness (which is the default assumption for
  237. a statistical test of biasedness in a die - the "null hypothesis").
  238.  
  239. What you get is either a higher degree of evidence against the hypothesis 
  240. of fairness (by getting a result that is very unlikely with a fair die),
  241. or a low degree of evidence against fairness. It's like in a court case,
  242. (a criminal case), where the defendant is assumed innocent until proven
  243. guilty (innocence is the null hypothesis), but evidence against the
  244. defendant is presented by the prosecution. The jury then decides either
  245. "guilty" if there is strong enough evidence, or "Not guilty" if there
  246. is not. They don't declare innocence.
  247.  
  248. So we can't determine "fairness" anyway. The question we need to ask
  249. is: If the die is biased, will a hundred rolls (or whatever number)
  250. be enough for us to have a good chance to pick up that difference,
  251. while at the same time, not "convicting the innocent" too often? 
  252. Whether it is enough depends on how big a difference you think it is 
  253. important to pick up.
  254.  
  255. -----------------------------------------------------------------------
  256.  
  257.  
  258. In article <1992Nov12.231019.1204@mcs.kent.edu>,
  259.  adray@mcs.kent.edu (Adam Dray) writes (in response to Dave Golden):
  260.  
  261. >In other words, a histogram shows very little.  Random doesn't mean
  262. >necessarily that you'll get an even distribution.  It just mean the
  263. >probability that you won't get an even distribution is proportional to
  264. >the number of sides on the die, and the number of times you roll it.
  265.  
  266. I disgree with the first sentence. The final sentence above is wrong.
  267. The more you roll it, the more even the distribution will be, as long
  268. as the die is fair. It doesn't really depend on the number of sides.
  269. (except as far as the negative dependence between cell counts is
  270.  reduced for more sides).
  271.  
  272. >Notes about the fairness of dice:
  273. >
  274. >Sharp-edged dice are better than smooth-edged dice.  They're also more
  275.  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  276.  Not always, but this may be true more often than not.
  277.  
  278. >expensive, however.  Rounded dice are often inked by coating the
  279. >entire die with ink, then tossing the die in a "tumbler" (similar to
  280. >tumblers for smoothing rocks) until all the die on the outside is
  281. >gone.  Thus, the ink is left in the crevices where the numbers are.
  282. >
  283. >Theoretically, the grooves for the numbers can make one side a more
  284. >likely outcome.  Official casino dice don't have inset pips.
  285.  
  286. If you do it right any effect will be swamped by other manufacturing 
  287. defects anyway.
  288.  
  289. [some stuff deleted]
  290. >
  291. >GameScience did tests on other manufacturers' dice.  They found
  292. >certain numbers to be more likely.  I've heard that the real 100-sided
  293. >die tends to roll certain numbers more often.
  294.  
  295. It is impossible (both effectively and theoretically) to get a fair 100-
  296. sided die. The practical problems are more important than the theoretical
  297. ones.
  298.  
  299. >Filing corners off your dice can make certain outcomes more probable. 
  300. >Natural wear can do the same thing.
  301. >
  302. >For most people, none of this matters one damn bit.  =)
  303.  
  304. In general, no, it doesn't matter. It's encouraging to see that so
  305. many people (just about all posters on the topic) realise this.
  306.  
  307. ---------------------------------------------------------------------------
  308.  
  309. Interpreting the test statistic (Allan's "Indicator")
  310.  
  311. Carry out the calculations as described by Allan*, but use any number of
  312. throws per cell (possible outcome) you like (I'd suggest 10 as a minimum,
  313. because otherwise the tabled distribution is out a bit). The more rolls
  314. you do, the better chance you have of picking up a difference of a given
  315. size. The value of 20 that Allan suggested may well be a reasonable choice
  316. in most circumstances. Allan gives the calculations for two different
  317. numbers of throws (20 and 10 per cell, but in different posts), so you
  318. ought to be able to generalise.
  319.  
  320. *  The calculations given by Allan may no longer be available to you, so
  321.    an indication of how to do the calculations is given here:
  322.  
  323.    Roll the die many times, say 20 times per face. Record each result
  324.    (I suggest you make up a tally sheet). Calculate the difference between
  325.    the number of times each face came up and the expected number (20 in this
  326.    case). Square these values and add them. Divide by the expected number 
  327.    per face. This is your chi-squared statistic.
  328.    E.g.    d4:   Roll 20 times per face = 80 rolls  
  329.            Face:    1   2   3   4 
  330.          No times  23  18  15  24
  331.          expected  20  20  20  20
  332.         difference  3   2   5   4
  333.         diff^2      9   4  25  16  Sum = 54, chi-squared value = 54/20 = 2.7
  334.  
  335.  
  336. If the result is less than the final column of Allan's table 2 (which 
  337. are the tabulated values for a 5% significance level), you shouldn't 
  338. worry too much, there is not very strong evidence of bias - in fact 1 
  339. in 20 tests on a fair die will score worse than this. If the result is 
  340. much bigger than the value you have some cause for concern. A result
  341. bigger than the 1% column below is quite unusual if the die is fair 
  342. (a result at least this big only occuring 1% of the time), so it gives 
  343. us good reason to suspect bias.
  344.  
  345. A small table of the chi-squared distribution:
  346.  
  347.         5%      1%     df
  348.  d4    7.81    11.34    3
  349.  d6   11.07    15.09    5
  350.  d8   14.07    18.48    7 
  351. d10   16.92    21.67    9
  352. d12   19.68    24.72   11
  353. d20   30.14    36.19   19
  354.  
  355. (these results came from a computer approximation to the chi-squared
  356.  distribution. They should be accurate to the figures given.)
  357.  
  358. If you want a more "cookbook" approach; if the result exceeds the 1%
  359. value, its probably biased. If its between the 1% and 5% values, there
  360. is a moderate degree of evidence that its biased, but it still might be
  361. OK. If its less than the 5% value, you don't have any reason to think
  362. its biased on the basis of the test.
  363.  
  364.  
  365. You will find more extensive tables in most elementary statistics books.
  366.   (references for the chi-squared and Kolmogorov-Smirnov tests are
  367.      at the end of this article).
  368. You look up the df (degress-of-freedom) that are one less than the
  369. number of faces on the die (e.g. d4 -> 3 df).
  370.  
  371. A note on pronunciation: The Greek letter chi (the capital looks like
  372. an X, and the lower-case has one of the two crossed lines a bit curly)
  373. is pronounced with a hard "ch" like Charisma, and the word rhymes with
  374. pie. Note that mathematical symbols come from *ancient* Greek, so no
  375. arguments from any modern Greeks please.
  376.  
  377. This will provide a reasonable all-round test for bias in a die.
  378.  
  379.  
  380. -------------------------------------------------------------------
  381.  
  382. Why you probably don't want to do the chi-squared test:
  383. (at least for d8 and above)
  384.  
  385. The chi-squared test will pick up any kind of deviation from a purely even
  386. distribution. However, we are much more worried about some kind of deviations
  387. than others. For example, I'd be more interested in knowing that "20" came
  388. up too often on a d20 than knowing "10" came up too often. The first could
  389. affect play substantially, the second probably only a little. We should use a
  390. test with a better chance to pick up the kind of deviations from fairness
  391. that are most important to us (which will trade off with less chance of
  392. picking up deviations we are less concerned with).
  393.  
  394. Let us consider a more complete example:
  395.  
  396. Imagine we have two d20's we'd like to test, and that in fact 
  397. (but unknown to us) they have the following (percentage) probabilities:
  398.  
  399. (the rows are: Face number
  400.                % prob 1st die
  401.                % prob 2nd die )
  402.  
  403.  
  404.   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20
  405.   5  4.5  6  3.5  7  2.5  8  1.5  7   5   5   7  1.5  8  2.5  7  3.5  6  4.5  5
  406.  1.5 1.5 2.5 2.5 3.5 3.5 4.5 4.5  5   5   5   5   6   6   7   7   7   7   8   8
  407.  
  408. A fair die would have 5% right across, of course. These 2 dice can be obtained
  409. from each other by relabelling the faces. The first die will be reasonable in
  410. play, because, of course, we don't try to roll 'exactly 8' or 'exactly 9',
  411. but 'less than 8' or 'greater than 11'. The first die is never out by
  412. more than one twentieth of the required probablility (e.g. probability of
  413. a 2 or less is 9.5% instead of 10%) in either direction. It has the correct
  414. average, and almost the correct standard deviation (the difference is tiny).
  415.  
  416. The second die would be very unbalancing in play: it has about a 2/3 chance
  417. (66%) of rolling 11 or higher, and a 20 is more than 6 times as likely as 
  418. a 1. The mean is almost 13. The standard deviation is also out, but that's
  419. relatively unimportant.
  420.  
  421. The chi-squared will rate them as equally bad!  
  422.  
  423. So a good test should be likely to identify the second die, but we might
  424. be prepared to sacrifice some of our ability to pick up the first, since
  425. it will make little practical difference in play. (I said I'd come back to
  426. this point!)
  427.  
  428. Note that almost any deviation on a d4 will be important (there are only 4
  429. different values), and to a lesser extent a d6. I'd stick with the chi-
  430. squared test on those.
  431.  
  432. There are many tests that will do what we want. I will present only one
  433. such test*. (This is not to say that a properly applied chi-squared is
  434. not good, just that a test more closely tailored to our specific 
  435. question of interest will be even better.)
  436.  
  437. * two tests if you count "Tests based on histograms", below.
  438.  
  439. The Kolmogorov-Smirnov test:
  440.  
  441. Collect data as for the chi-squared test, up to the point where you
  442. start doing calculations.
  443.  
  444. That is, lay out like this (you could run down rather than across):
  445.  
  446.  
  447. Roll:        1   2   3   4   5  ......
  448. Count:      17  19  22  27  24  ......
  449. Expected:   20  20  20  20  20  ......
  450.  
  451. Now add up your counts and expected counts, writing the partial 
  452. totals as you go:
  453.  
  454. Roll:        1   2   3   4   5  ......
  455. Count:      17  19  22  27  24  ......
  456. Expected:   20  20  20  20  20  ......
  457. Sum Count:  17  36  58  85  109 ......
  458. Sum Exp:    20  40  60  80  100 ......
  459.  
  460. Now find the differences (without sign):
  461.  
  462. Sum Count:  17  36  58  85  109 ......
  463. Sum Exp:    20  40  60  80  100 ......
  464. Difference:  3   4   2   5    9 ......
  465.  
  466. The last difference will be zero, so you don't have to work out the
  467. final column (I still would as a check).
  468.  
  469. Divide the largest difference (9 is the largest difference above, for
  470. the calculations you can see) by the number of rolls you made altogether.
  471.  
  472. This is your test statistic. Let's call the value D.
  473.  
  474. You can look it up in most books on nonparametrics, which will
  475. have tables. However, you would be better to use the table below, for 
  476. reasons I'll discuss in a second.
  477.  
  478. You multiply D by the square root of the number of rolls
  479. (equivalently, divide the largest difference by the square root of 
  480.  the number of rolls), and compare with:
  481.  
  482.   5%   1%    d#
  483.  1.08 1.35   4
  484.  1.10 1.37   6     These values apply pretty well irrespective of
  485.  1.11 1.38   8     the total number of rolls, but I would use at 
  486.  1.12 1.39  10     least 10 rolls per face.
  487.  1.12 1.40  12     Note also that these values come from simulation,
  488.  1.14 1.42  20     and are hence not exact. This doesn't really matter.
  489.  
  490. and interpret as I suggested for the chi-square test.
  491.  
  492. You may find the following values in tables:
  493.  
  494.    5%    1%
  495.   1.36  1.63  (irrespective of the number of sides on the die)
  496.  
  497. the reason these are larger is that they are based on the assumption that
  498. the distribution the data are from is continuous (effectively, a *very*
  499. large number of faces on the die would give these values). If you use the
  500. textbook values, the test will be conservative (a fair die will reject 
  501. slightly less often than the supposed 5% and 1% for the above table), due 
  502. to the distribution of values being discrete (d20 generates only integers,
  503. not anything between). 
  504.  
  505. So, for our above example, assume there are no larger differences
  506. than 9, and that we made 400 rolls on a d20 (hence the expected number
  507. in each cell is 20, as above). Then D is 9/400 = .0225, which if you
  508. can get tables you'd look up. We made 400 rolls, so we could use the
  509. table above: the square root of 400 is 20, so D x 20  (= 9/20) = .45.
  510. This is much less than the 5% value, so there is little evidence that
  511. the die is unfair.
  512.  
  513. There are tests which are probably even more appropriate, but these
  514. two (chi-squared and K-S) will be enough for you to get a good idea
  515. of any suspect dice.
  516.  
  517. Note:    If you suspect a die, and decide to test it, don't use the
  518.          rolls that made you suspect it in the test. Generate a new
  519.          set. e.g. if you are all recording your rolls as you play,
  520.          and one players' results look funny, don't then test those
  521.          recorded values - you have to generate a new set.
  522.  
  523. -------------------------------------------------------------------------
  524.  
  525. Testing a die based on the histogram of rolls:
  526.  
  527. The histogram approach can be turned into a test of sorts as follows:
  528. After drawing the histogram, 2 lines can be drawn either side of the
  529. expected (mean) result. If all histogram bars lie within the inner
  530. lines, there is no strong evidence of bias. If any of the bars go outside
  531. the outer lines, there is fairly clear evidence of bias. If one of the
  532. bars lies between the inner and outer lines, then there is some (mild)
  533. evidence of bias, but its is not really clear. You may wish to then
  534. perform a further test on the probability for that individual side,
  535. as described in the next section (Testing an individual face). If 
  536. several of the bars lie between the inner and outer lines, we have a 
  537. stronger indication of bias. 
  538.  
  539. Where do we draw the lines? 
  540.  
  541. I have worked out values for rolling 20 times for each face (as in the
  542. other examples, 80 times for a d4, 400 times for a d20). The bars on the
  543. histogram must actually go past these values. You could think of these
  544. values as giving "Acceptable Ranges" (literally, 95% and 99% acceptance 
  545. regions) for the histogram. A fair die will give histograms with one or 
  546. more bars outside these ranges 5% and 1% of the time respectively 
  547. (actually just under).
  548.  
  549.  
  550.     Table for 20 
  551.    throws per face          Approximate formula:
  552.  
  553.    d#    5%    1%           Let N be the total number of rolls.
  554.     4   8-34  6-37          Let c be the number of faces on the die.
  555.     6   9-33  7-36          Let e be the expected number of times
  556.     8   9-33  8-35            each face will come up ( e = N/c ).
  557.    10  10-32  8-35          Then the lines go at e +/- A x sqrt[e (c-1)/c],
  558.    20  11-30  9-32          and the value for 'A' comes from the table below. 
  559.                             All fractions should be rounded up. 
  560.                             e.g. N=160, c=8, e=20 give: 
  561.                                  5%: 20 +/- 2.73 sqrt (20 x 7/8) or 8-32
  562.                                  1%: 20 +/- 3.22 sqrt (20 x 7/8) or 7-34
  563.  
  564.                               Table to go with 
  565.                              approximate formula:
  566.  
  567.                                 d#   5%  1%
  568.                                  4 2.49 3.02       Due to being in the
  569.                                  6 2.63 3.14       extreme tails of the
  570.                                  8 2.73 3.22       distribution, combined
  571.                                 10 2.80 3.29       with slight asymmetry,
  572.                                 12 2.86 3.34       the ranges we get are
  573.                                 20 3.02 3.48       sometimes out a bit.
  574.                                                    This is not a big deal.
  575.  
  576.  
  577. Example: We throw a d20 400 times, and record the results 
  578.  and from the table above, we draw the inner lines at 11 & 30,
  579.  and the outer lines at 9 & 32, as well as a reference line at 20. 
  580.  (in the histogram below, "." =1 count, ":" =2 counts. The horizontal
  581.   lines aren't quite in the correct positions; they ought to be about 
  582.   a quarter to half a character position lower.)
  583.  
  584.   Counts                         (34)                                
  585.     35 +                        >   <                                
  586.        |__________________________:__________________________________ (32)
  587.        |__________________________:__________________________________ (30)
  588.        |                          :                                  
  589.        |                          :                                  
  590.     25 +                          :                       .          
  591.        |           .              :                       :  :       
  592.        |__:__._____:_____:________:____________________:__:__:_______ (20)
  593.        |  :  :     :  .  :  :     :     .              :  :  :       
  594.        |  :  :  :  :  :  :  :     :  .  :     :        :  :  :  :  : 
  595.     15 +  :  :  :  :  :  :  :  :  :  :  :  .  :  .     :  :  :  :  : 
  596.        |  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  : 
  597.        |--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:- (11)
  598.        |--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:- ( 9)
  599.        |  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  : 
  600.      5 +  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  : 
  601.        |  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  : 
  602.        |  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  :  : 
  603.        `--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- 
  604. face:     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 
  605.  
  606. counts:  22 21 18 23 19 22 20 16 34 17 19 15 18 15 14 22 25 24 18 18
  607.                                  ^^
  608.  
  609. One value (34) goes outside the 1% values (technically, you could say
  610. goes outside a 99% acceptance region), so it seems our dice is biased.
  611. More particularly, it rolls too many 9's. Whether this will affect a
  612. game very much is another point. (If it had been "1" or "20", however,
  613. perhaps this would result in a large effect on the game).
  614.  
  615.  
  616.  
  617. Testing an individual face:
  618.  
  619. When you suspect a particular face is coming up with the wrong 
  620. frequency, or only wish to test a particular face (e.g. 20 on a
  621. d20), throw as before, but you can compare with a narrower range,
  622. as below. However, you can't use the data that made you suspect
  623. the face in this test, you must generate a new set of rolls. 
  624. E.g. If you a histogram and find the results for 7 look odd,
  625. you can't use those numbers in this test. In that case the ranges
  626. given above (for testing an entire histogram) are appropriate.
  627.  
  628. For the tables below, as for those above, you need to exceed the
  629. range given to call the die 'probably biased'.
  630.  
  631. When you don't know the direction already (two-tailed test):
  632. (If you are in doubt, use this table rather than the next one)
  633.  
  634.     Table for 20 
  635.    throws per face          Approximate formula (reasonably accurate):
  636.  
  637.    d#    5%    1%           Let N be the total number of rolls.
  638.     4  13-28 11-30          Let c be the number of faces on the die.
  639.     6  12-28 10-31          Let e be the expected number of times
  640.     8  12-29 10-31            each face will come up ( e = N/c ).
  641.    10  12-29 10-32          Then the lines go at e +/- 1.96 x sqrt[e (c-1)/c],
  642.    12  12-29 10-32            (5%) and for 1% at e +/- 2.58 x sqrt[e (c-1)/c].
  643.    20  12-29 10-32          All fractions should be rounded up.
  644.                             e.g. N=160, c=8, e=20 give:
  645.                            5%: 20 +/- 1.96 sqrt(20 x 7/8) or 12-29 (rounded up)
  646.                            1%: 20 +/- 2.58 sqrt(20 x 7/8) or 10-31 (rounded up)
  647.  
  648.  
  649.  
  650. When you think a particular face is coming up too often, or if you
  651. think a particular face isn't coming up enough (one-tailed test):
  652.  
  653.     Table for 20
  654.    throws per face          Approximate formula (reasonably accurate):
  655.  
  656.    d#    5%    1%           Let N be the total number of rolls.
  657.     4  14/26 11/30          Let c be the number of faces on the die.
  658.     6  13/27 11/30          Let e be the expected number of times
  659.     8  13/27 11/30            each face will come up ( e = N/c ).
  660.    10  13/27 11/30          Then the lines go at e +/- 1.65 x sqrt[e (c-1)/c],
  661.    12  13/27 11/30            (5%) and for 1% at e +/- 2.33 x sqrt[e (c-1)/c].
  662.    20  13/27 11/31          All fractions should be rounded up.
  663.                             e.g. N=160, c=8, e=20 give:
  664.                            5%: 20 +/- 1.65 sqrt(20 x 7/8) or 14-27 (rounded up)
  665.                            1%: 20 +/- 2.33 sqrt(20 x 7/8) or 11-30 (rounded up)
  666.  
  667. These values are given as either/or i.e. since you have already specified
  668. a particular direction, you will compare with only the higher values or the 
  669. lower values, not both.
  670.  
  671.  
  672. Example 1: You decide to test you new d20 to see if it rolls the
  673.            correct number of 20's, but you don't believe it to
  674.            be biased in a particular direction. You roll 400
  675.            times, and 35 times you get a "20". From the "two-tailed"
  676.            table above, you can see that's outside the outer (1%) range.
  677.            It seems your d20 rolls too many 20's.
  678.  
  679. Example 2: Another player seems to be rolling a lot of 1's on her d4.
  680.            You decide to test it whether 1 comes up too often. 
  681.            You roll it 80 times, and get the following:
  682.  
  683.                1   2   3   4
  684.               13  26  25  16
  685.  
  686.            Since you decided to test if there were too many 1's, you
  687.            can only see if the number of 1's exceeds 26, which it does
  688.            not. You can't say, after generating the data "Oh, actually,
  689.            perhaps it rolls too few ones", or "perhaps it rolls too many
  690.            2's" without generating a new set of data for the new hypothesis.
  691.            You must never base what you are testing for on what you spy
  692.            in the set of data you use in the test. Our only conclusion
  693.            on this test: the d4 doesn't roll too many 1's. 
  694.  
  695.            You may like to then generate a new set of rolls to see if it
  696.            rolls too few 1's.
  697.  
  698. ---------------------------------------------------------------------
  699. As further examples, here are the chi-squared test and Kolmogorov-Smirnov (KS)
  700. tests performed on the same data.
  701.  
  702. chi squared test:
  703.  
  704. counts:  22 21 18 23 19 22 20 16 34 17 19 15 18 15 14 22 25 24 18 18
  705. diff 
  706. from 20:  2  1  2  3  1  2  0  4 14  3  1  5  2  5  6  2  5  6  2  2
  707. diff^2:   4  1  4  9  1  4  0 16 196 9  1 25  4 25 36  4 25 36  4  4
  708. sum diff^2: 408
  709. chi-squared statistic: 408/20 = 20.4 
  710. (far less than the 5% value of 30.14)
  711.  
  712.  
  713. Kolmogorov-Smirnov test:
  714.  
  715. (Calculations have been run down the page because I can't fit 20 3 digit 
  716.  numbers, with spaces and labels across an 80-column screen).
  717.  
  718.  counts sum expected diff
  719.    22    22    20     2
  720.    21    43    40     3
  721.    18    61    60     1
  722.    23    84    80     4
  723.    19   103   100     3
  724.    22   125   120     5
  725.    20   145   140     5
  726.    16   161   160     1
  727.    34   195   180    15 <-- max diff, D, is 15. Well short of significance
  728.    17   212   200    12     at the 5% level. e.g. calc D/sqrt(n) = 15/20
  729.    19   231   220    11     or .75; where the 5% value from the simulations
  730.    15   246   240     6     is 1.14
  731.    18   264   260     4
  732.    15   279   280     1
  733.    14   293   300     7
  734.    22   315   320     5
  735.    25   340   340     0
  736.    24   364   360     4
  737.    18   382   380     2
  738.    18   400   400     0
  739.  
  740. ----------------------------------------------------------------------------
  741.  
  742.  
  743. Conover, W.J. (1980): Practical nonparametric statistics,
  744. 2nd Ed., Wiley, New York.
  745.  
  746. Neave, H.R. and Worthington, P.L.B. (1988): Distribution-free tests,
  747. Unwin Hyman, London.
  748.  
  749.