# xikka

A collection of the things I have made. Some of them useful, most of them not.

## Rolling in the deep

Sunday, 21st October 2018 ◆ Sandwich spells troll's end (4)

I don't like traditional four-sided dice. When you toss them, they just thud to the table without really rolling. Reading the number is also awkward. All in all, bad.

I have collected a couple of variant d4s which I find much more satisfying. On each, the number is clearly visible after rolling, and the dice actually roll on the table once you release them. I knocked together a couple of models in Blender to show the shapes I have:

Tetrahedron (the standard shape)
Rounded d6
Almost cuboid

I'm curious if these variants are actually fair, so I decided to roll each type 250 times. Here are the results:

1 2 3 4 Mean Std. dev.
63 70 55 62 2.464 1.12
63 59 71 57 2.488 1.215
52 73 57 68 2.564 1.1

In order to determine if these results indicate fair dice, we turn to a major player in the world of statistics: the hypothesis test. The general principle of a hypothesis test is that we assume the dice are fair – this assumption is called the null hypothesis. Then we calculate the probability of seeing data like ours, or more extreme, were this hypothesis true. If this probability is low, it means that our assumption must be wrong and that the dice were not fair in the first place. A typical critical value is 5%, which means we reject the null hypothesis if the probability of seeing our results is less than 5%.

First we need to calculate the probability of seeing a given set of results, $$\{ x_1, x_2, x_3, x_4 \}$$, where $$x_i$$ is the number of times we rolled $$i$$. $$N$$ represents the total number of rolls, which in my case is $$250$$. Note that $$N = x_1 + x_2 + x_3 + x_4$$.

$$\mathbb{P}(\{ x_1, x_2, x_3, x_4 \}) = \frac{N!}{x_1!\ x_2!\ x_3!\ x_4!} \cdot \frac{1}{4^N}$$

This is an example of the multinomial distribution. The derivation of this formula is some interesting combinatorics, but I won't go into it here.

For example, the tetrahedron has results of $$\{63, 70, 55, 62\}$$. What is the probability of seeing these results with 250 rolls of a perfectly fair die?

$$\mathbb{P}(\{ 63, 70, 55, 62 \}) = \frac{250!}{63!\ 70!\ 55!\ 62!} \cdot \frac{1}{4 ^ {250}} \approx 3.05 \times 10^{-151}$$

This probability is so small because it's the probability of getting exactly what we saw. The hypothesis test demands we find the probability of getting this or anything less likely. To do this, we go through every combination of four numbers which add up to 250, find the probability of each of them, and then add together all probabilities which are equal to or less than what we found just above. This total probability is called the p-value.

To find all of these probabilities, I wrote a script which will loop through every combination, find the probability, and add it to the total only if it's equal to our lower than the value we just found. In pseudo-code, it would be something like this:

N = 250
p0 = probability(63, 70, 55, 62)
pvalue = 0
for (x1 = 0 to N) {
for (x2 = 0 to (N - x1)) {
for (x3 = 0 to (N - x1 - x2)) {
x4 = N - x1 - x2 - x3
p = probability(x1, x2, x3, x4)
if (p <= p0) {
pvalue += p
}
}
}
}

Doing this with the results of each of the dice, I calculate the following p-values:

p-value
61.4%
62.6%
21.1%

In each of these cases, the probability is greater than 5%. That means we don't have sufficient evidence to reject the hypothesis, and we assume the die are fair. Hurrah, this means I can continue to use my dice!

Happy rolling!